日期:2014-05-18 浏览次数:20749 次
using System; using System.Windows.Forms; using System.Runtime.InteropServices; class Test : Form { const int MF_BYPOSITION = 0x0400; const int MF_REMOVE = 0x1000; [DllImport("user32.dll",EntryPoint="GetSystemMenu")] extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); [DllImport("user32.dll",EntryPoint="RemoveMenu")] extern static int RemoveMenu(IntPtr hMenu, int nPos, int flags); Test() { Text = "不能移动和改变大小的窗口"; FormBorderStyle = FormBorderStyle.FixedSingle; MaximizeBox = false; MinimizeBox = false; RemoveMenu(GetSystemMenu(Handle,IntPtr.Zero),1,MF_BYPOSITION|MF_REMOVE); } static void Main() { Application.Run(new Test()); } }
------解决方案--------------------
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if ((int)m.Result == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
return;
break;
}
base.WndProc(ref m);
}
------解决方案--------------------
-_-b
是没标题栏不能移动吧……
按下面的方法,你想要在什么地方移动窗体都行:
private const int WM_NCLBUTTONDOWN = 0xA1; //消息:左键点击 private const int HT_CAPTION = 0x2; //标题栏 [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern int SendMessage(System.IntPtr hWnd, int Msg, int wParam, int lParam); //发送消息 [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool ReleaseCapture(); private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); //释放鼠标捕捉 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的消息至该窗体(标题栏) } }