C# 中怎样不能拖动窗体,使其固定在一个位置。
第1种: 用API去掉系统菜单的“移动”菜单项, 完美的解决方案
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());
}
}
第2种: 去掉标题栏的系统菜单, 点右键自然无效, 不推荐
第三种,重载WndProc