在.net里如何锁定窗体,拖动标题栏,使其不移动?

2025-12-25 07:56:34
推荐回答(1个)
回答1:

实现方法如下:
1.引用命名空间
using System.Runtime.InteropServices;
2.声明如下变量:
public class Form1 : System.Windows.Forms.Form
{
//.....
[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);

//.....
}

3.在Form的构造函数里加入代码
public Form1()
{
const int MF_BYPOSITION = 0x0400;
const int MF_REMOVE = 0x1000;

FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
MinimizeBox = false;
RemoveMenu(GetSystemMenu(Handle,IntPtr.Zero),1,MF_BYPOSITION |MF_REMOVE);

}

就OK啦