如何捕获windows的关机和休眠命令?
是捕获,拦截下来,然后判断是什么命令,作相应的处理,
不知道是用钩子呢?还是用API注册呢?

解决方案 »

  1.   

    拦截 wm_exit 消息。
    判断。
      

  2.   

     ///  <summary>   /// 重载WndProc消息处理函数   ///  </summary>   ///  <param name="m">windows消息 </param>   protected override void WndProc(ref System.Windows.Forms.Message m)   {   try   {   switch(m.Msg)   {   //系统退出消息处理,WM_QUERYENDSESSION是询问程序是否需要关闭,   //要有相应的反回值,0不关闭程序;1关闭程序   case WM_QUERYENDSESSION:   m.Result = (IntPtr)WM_TRUE;   return;   //休眠事件处理    case WM_POWERBROADCAST :   if (m.WParam == (IntPtr)PBT_APMQUERYSUSPEND)   {   //系统即将休眠消息处理   try   {   this.BusManager.Close();   m.Result = (IntPtr)WM_TRUE;   }   catch   {   //捕捉异常,不做处理   }   }   break;   default:   break;   }   base.WndProc (ref m);   }    catch(Exception e)   {   MessageBox.Show(e.Message);   }   } 
      

  3.   

             ///  <summary>  
             /// 重载WndProc消息处理函数  
              ///  </summary>  
             ///  <param name="m">windows消息 </param>  
              protected override void WndProc(ref System.Windows.Forms.Message m)  
              {  
                  try  
                  {  
                      switch(m.Msg)  
                      {  
                          //系统退出消息处理,WM_QUERYENDSESSION是询问程序是否需要关闭,  
                          //要有相应的反回值,0不关闭程序;1关闭程序  
                          case WM_QUERYENDSESSION:  
                              m.Result = (IntPtr)WM_TRUE;  
                              return;  
                          //休眠事件处理   
                          case WM_POWERBROADCAST :  
                              if (m.WParam == (IntPtr)PBT_APMQUERYSUSPEND)  
                              {  
                                  //系统即将休眠消息处理  
                                  try  
                                  {  
                                      this.BusManager.Close();  
                                      m.Result = (IntPtr)WM_TRUE;  
                                  }  
                                  catch  
                                  {                                     //捕捉异常,不做处理  
                                  }  
                              }  
                              break;  
                          default:  
                              break;  
                      }  
                      base.WndProc (ref m);  
                  }   
                  catch(Exception e)  
                  {  
                      MessageBox.Show(e.Message);  
                  }  
              } 
      

  4.   

    http://topic.csdn.net/u/20071018/17/eb9c5d28-f3ad-4d41-8235-933a0db4e3f3.html
      

  5.   

    API Hook 拦截 ExitWindowsEx
      

  6.   

     MouseHookProcedure = new Win32API.HookProc(MouseHookProc);
                            MousehHook = Win32API.SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, pInstance, 0);我不知道是不是这个意思,但是那些个参数,不知道到底SetWindowsHookEx应该怎么设置,才可以拦截ExitWindowsEx的消息阿
      

  7.   

    要的是拦截,在windows系统响应之前,要不其他窗口被结束了,就留一个。。有什么意思