C# winform 缩小到托盘 无法关机?

来源:百度知道 编辑:UC知道 时间:2024/09/23 20:22:54
Form_Closing 事件是让它hide()

但这样就无法关机了,怎么解决呢?
我关闭事件里这样写

if (!this.isExitApp)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Hide();
}

但结果是,关机的时候,其它能关的都关了,唯独这个关不了

关机时,系统给你的程序发送一个关机消息,你的程序调用FormClosing事件委托函数,而此时你的isExitApp不为true,因而只是隐藏,不退出。
应该截获关机消息处理之。
改为:
private bool isExitApp = false;
private const int WM_QUERYENDSESSION = 0x0011;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_QUERYENDSESSION:
isExitApp = true;
break;
default:
base.WndProc(ref m);
break;
}

}
private void Form7_FormClosing(object sender, FormClosingEventArgs e)
{
if (!this.isExitApp)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Hide();
}

}

怎么会呢?
关机的时候什么都结束了啊!

我也觉得不可能...而且你关闭的时候最小化干什么?