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
但这样就无法关机了.因为关机时,系统给你的程序发送一个关机消息,你的程序调用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
相关文章推荐
- 程序缩小到托盘后系统就无法关机(解决方案)——处理WM_QUERYENDSESSION消息,并把它标识为处理过了
- .Net在写托盘程序中碰到的一些问题的解决方法(无法关机/自启动等)
- 程序缩小到托盘后系统就无法关机(解决方案)
- 无法关机问题的详解
- 关于OutLook2007无法最小化到系统托盘问题
- 关于ubuntu无法正常关机的问题
- 【解决Win8系统下无法彻底关机的问题】
- C#通过窗体属性缩小一定尺寸时,无法再缩小窗体尺寸问题
- 机械革命X6Ti(skylake平台)安装ubuntu 16.04,完美解决无法关机挂起问题
- c# winform 解决PictureBox 无法打印全部图片的问题
- 关于系统托盘图标(systemtray)无法显示问题的解决
- c#托盘程序造成不能关机问题解决
- 【经验】电脑长时间不关机导致的无法开机问题
- 解决Ubuntu使用Flatabulous主题&Ultra-flat图标搜狗输入法托盘图标无法显示问题
- android2.2中默认浏览器无法使用手势放大缩小页面的问题定位
- Ubuntu 8.10内核无法更新问题,无法关机的解决
- 解决 Window 8 系统下无法彻底关机的问题
- FormCloseQuery中加入CanClose = false无法正常关机问题解决
- [未解决]fedora9无法关机问题
- 如何解决无法关机问题