您的位置:首页 > 编程语言 > C#

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

2009-11-27 16:38 99 查看
对于程序实现缩小到托盘,Form_Closing 事件是让它hide();
但这样就无法关机了.因为关机时,系统给你的程序发送一个关机消息,你的程序调用FormClosing事件委托函数,而此时你的isExitApp不为true,因而只是隐藏,不退出。
应该截获关机消息处理之。
开始代码为:
1. #region 关闭窗体
2. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
3. {
4. e.Cancel = true;
5. HideWindow();
6. }
7. #endregion
8.
9. #region 隐藏窗口
10. private void HideWindow()
11. {
12. this.WindowState = FormWindowState.Minimized;
13. this.Hide();
14. this.ShowInTaskbar = false;
15. this.notifyIcon1.Visible = true;
16. }
17. #endregion
修改后:
1. private bool isExitApp = false;
2. private const int WM_QUERYENDSESSION = 17; //0x0011
1. #region 关闭窗体
2. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
3. {
4. if (!isExitApp)
5. {
6. e.Cancel = true;
7. HideWindow();
8. }
9. }
10. #endregion
11.
12. #region 隐藏窗口
13. private void HideWindow()
14. {
15. this.WindowState = FormWindowState.Minimized;
16. this.Hide();
17. this.ShowInTaskbar = false;
18. this.notifyIcon1.Visible = true;
19. }
20. #endregion
1. #region 截获消息
2. ///
3. /// 截获消息
4. ///
5. ///
6. protected override void WndProc(ref Message message)
7. {
8. switch (message.Msg)
9. {
10. case WM_QUERYENDSESSION:
11. isExitApp = true;
12. break;
13. }
14. base.WndProc(ref message);
15. }
16. #endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: