程序运行时最小化到任务栏托盘,可这时候关闭或重启操作系统使如果程序没有退出,则系统不能关闭.那么如何实现关机时自动退出程序呢?其实很简单,当windows操作系统执行关闭动作时,它会发送给各个正在运行的应用程序一个消息WM_QUERYENDSESSION,告诉应用程序要关机了,如果反馈回来的消息值为1,那么windows操作系统就会自动关闭.因此,通过截获WM_QUERYENDSESSION消息,就能实现自动退出程序.

 Code
Code
 1
 /**//// <summary>
/**//// <summary> 
 2 /// 操作系统关闭时,关闭应用程序
        /// 操作系统关闭时,关闭应用程序 
 3 /// </summary>
        /// </summary> 
 4 /// <param name="m">截获系统消息</param>
        /// <param name="m">截获系统消息</param> 
 5 protected override void WndProc(ref Message m)
        protected override void WndProc(ref Message m) 
 6
 
         {
{ 
 7 switch (m.Msg)
            switch (m.Msg) 
 8
 
             {
{ 
 9 case 0x0011://WM_QUERYENDSESSION
                case 0x0011://WM_QUERYENDSESSION 
10 m.Result = (IntPtr)1;
                    m.Result = (IntPtr)1; 
11 break;
                    break; 
12 default :
                default : 
13 base.WndProc(ref m);
                    base.WndProc(ref m); 
14 break;
                    break; 
15 }
            }             
16 }
        }
17
18
 /**//// <summary>
/**//// <summary>
19 /// 重载WndProc消息处理函数
        /// 重载WndProc消息处理函数
20 /// </summary>
        /// </summary>
21 /// <param name="m">windows消息</param>
        /// <param name="m">windows消息</param>
22 protected override void WndProc(ref System.Windows.Forms.Message m)
        protected override void WndProc(ref System.Windows.Forms.Message m)
23
 
         {
{
24 try
            try
25
 
             {
{
26 switch(m.Msg)
                switch(m.Msg)
27
 
                 {
{
28 //系统退出消息处理,WM_QUERYENDSESSION是询问程序是否需要关闭,
                        //系统退出消息处理,WM_QUERYENDSESSION是询问程序是否需要关闭,
29 //要有相应的反回值,0不关闭程序;1关闭程序
                        //要有相应的反回值,0不关闭程序;1关闭程序
30 case WM_QUERYENDSESSION:
                    case WM_QUERYENDSESSION:
31 m.Result = (IntPtr)WM_TRUE;
                        m.Result = (IntPtr)WM_TRUE;
32 return;
                        return;
33 //休眠事件处理
                        //休眠事件处理                    
34 case WM_POWERBROADCAST :
                    case WM_POWERBROADCAST :
35 if (m.WParam == (IntPtr)PBT_APMQUERYSUSPEND)
                        if (m.WParam == (IntPtr)PBT_APMQUERYSUSPEND)
36
 
                         {
{
37 //系统即将休眠消息处理
                            //系统即将休眠消息处理
38 try
                            try
39
 
                             {
{
40 this.BusManager.Close();
                                this.BusManager.Close();
41 m.Result = (IntPtr)WM_TRUE;
                                m.Result = (IntPtr)WM_TRUE;
42 }
                            }
43 catch
                            catch
44
 
                             {
{
45 //捕捉异常,不做处理
                                //捕捉异常,不做处理
46 }
                            }
47 }
                        }
48 break;
                        break;
49 default:
                    default:
50 break;
                        break;
51 }
                }
52 base.WndProc (ref m);
                base.WndProc (ref m);
53 }
            }            
54 catch(Exception e)
            catch(Exception e)
55
 
             {
{
56 MessageBox.Show(e.Message);
                MessageBox.Show(e.Message);
57 }
            }
58 }
        }
59
60
以上是2段代码,任何一个均可实现关机关闭程序!