[ DllImport ( "USER32.DLL" ) ]
public static extern int GetSystemMenu(int hwnd, int bRevert);
[ DllImport ( "USER32.DLL" ) ]
public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
/// <summary>
/// 返回值,非零表示成功,零表示失败。
/// </summary>
/// <param name="iHWND">窗口的句柄</param>
/// <returns>是否成功</returns>
public int RemoveXButton(int iHWND)
{
int iSysMenu;
const int MF_BYCOMMAND = 0x400; //0x400-关闭
iSysMenu = GetSystemMenu(this.Handle.ToInt32(), 0);
return RemoveMenu(iSysMenu, 6, MF_BYCOMMAND);
}
private void Form1_Load(object sender, EventArgs e)
{
//通过函数返回值就可以判断目的是否达到,在构造函数里面可以这样判断:
int iReturn = RemoveXButton(this.Handle.ToInt32());
if (iReturn == 0) //返回值非零表示成功,零表示失败。
{
MessageBox.Show("Error!");
}
else
{
MessageBox.Show("Success!");
}
}