c#form窗口怎么让它不能关闭,就是右上角点不了
取消左上角的关闭事件执行
privatevoidForm1_Closing(objectsender,System.ComponentModel.CancelEventArgse)
{
e.Cancel=true;
}
//重写OnClosing使点击关闭按键时窗体能够缩进托盘
protectedoverridevoidOnClosing(CancelEventArgse)
{
this.ShowInTaskbar=false;
this.WindowState=FormWindowState.Minimized;
e.Cancel=true;
}
扩展资料
禁用窗体的关闭按钮
privateconstintCP_NOCLOSE_BUTTON=0x200;
protectedoverrideCreateParamsCreateParams
{
get
{
CreateParamsmyCp=base.CreateParams;
myCp.ClassStyle=myCp.ClassStyle|CP_NOCLOSE_BUTTON;
returnmyCp;
}
}
像下面的样子
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
退出按钮事件里,写 Application.Exit(); 就OK了
[ 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!");
}
}
参考资料: http://haitaowang.blog.163.com/blog/static/12802311920106274256248/