1、用API去掉Move
[DllImport("user32.dll",EntryPoint="GetSystemMenu")] extern static System.IntPtr GetSystemMenu(System.IntPtr hWnd , System.IntPtr bRevert);
[DllImport("user32.dll",EntryPoint="RemoveMenu")] extern static int RemoveMenu (IntPtr hMenu, int nPos, int flags);
static int MF_BYPOSITION = 0x400; static int MF_REMOVE = 0x1000;
System.IntPtr hdl= GetSystemMenu(this.Handle,System.IntPtr.Zero); int nflag =MF_BYPOSITION | MF_REMOVE; int npos =1; RemoveMenu(hdl,npos,nflag);
2、去掉系统菜单(不推荐)
private const int WS_SYSMENU = 0x00080000;
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style = cp.Style & ~WS_SYSMENU; return cp; } }
示范代码如下
using System; using System.Windows.Forms; using System.Runtime.InteropServices; public class Test : Form {
[DllImport("user32.dll",EntryPoint="GetSystemMenu")] extern static System.IntPtr GetSystemMenu(System.IntPtr hWnd , System.IntPtr bRevert);
[DllImport("user32.dll",EntryPoint="RemoveMenu")] extern static int RemoveMenu (IntPtr hMenu, int nPos, int flags);
static int MF_BYPOSITION = 0x400; static int MF_REMOVE = 0x1000;
Test() { Text = "不让拖动的标题栏"; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false;
//method 1 /*System.IntPtr hdl= GetSystemMenu(this.Handle,System.IntPtr.Zero); int nflag =MF_BYPOSITION | MF_REMOVE; int npos =1; RemoveMenu(hdl,npos,nflag); */ } private const int WM_NCHITTEST = 0x84; private const int HTCAPTION = 0x2; private const int HTCLIENT = 0x1;
protected override void WndProc(ref Message m) { base.WndProc(ref m); if(m.Msg == WM_NCHITTEST && m.Result == (IntPtr)HTCAPTION) { m.Result = (IntPtr) HTCLIENT; } } //method 2 private const int WS_SYSMENU = 0x00080000;
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style = cp.Style & ~WS_SYSMENU; return cp; } }
static void Main() { Application.Run(new Test()); } }