让窗体的关闭按钮无效
有很多时候我们需要让窗体的关闭按钮无效,所以。。。。
首先引入命名空间。
using System.Runtime.InteropServices;
然后DllImport以及常量声明。
[DllImport("user32", EntryPoint = "GetSystemMenu", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);
[DllImport("user32", EntryPoint = "DeleteMenu", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32", EntryPoint = "GetMenuItemCount", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern int GetMenuItemCount(IntPtr hMenu);
int mf_byposition = 0x00000400;
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);
[DllImport("user32", EntryPoint = "DeleteMenu", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32", EntryPoint = "GetMenuItemCount", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.Winapi)]
private static extern int GetMenuItemCount(IntPtr hMenu);
int mf_byposition = 0x00000400;
然后再Form_Load中写。
IntPtr longVal = GetSystemMenu(this.Handle, 0);
int longCount = GetMenuItemCount(longVal);
DeleteMenu(longVal, longCount - 1, mf_byposition);
int longCount = GetMenuItemCount(longVal);
DeleteMenu(longVal, longCount - 1, mf_byposition);