调用Windows API试一下
[DllImport("user32.dll")]
internal static extern IntPtr GetSystemMenu(IntPtr hwnd,bool bRevert);
[DllImport("user32.dll")]
internal static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
internal static extern int RemoveMenu(IntPtr hMenu,int uPosition,int uFlags);
///
/// 窗体的关闭按钮失效
///
protected void CloseButtonEnable(){
// 默认窗口去除关闭按钮
const int MF_BYPOSITION = 0x00000400;
IntPtr hWindow = this.Handle;
IntPtr hMenu = GetSystemMenu(hWindow,false);
int count = GetMenuItemCount(hMenu);
RemoveMenu(hMenu,count - 1,MF_BYPOSITION);
RemoveMenu(hMenu,count - 2,MF_BYPOSITION);
}
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("USER32.DLL")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert);
[DllImport("USER32.DLL")]
private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags);
private const UInt32 SC_CLOSE = 0x0000F060;
private const UInt32 MF_BYCOMMAND = 0x00000000;
public Form1()
{
InitializeComponent();
IntPtr hMenu = GetSystemMenu(this.Handle, 0);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
能用属性解决为何写这么多代码?设置controlbox为false就行了