none
如何向windows投递暂停关机消息 RRS feed

  • 问题

  •   有些系统程序需要在关闭windows之前执行数据保存后退出,怎么做到呢?获取windows消息容易,但是如何向windows投递一条暂停关机的消息,直到应用系统完成退出操作,再投递一条继续关机的消息?
    2010年8月2日 3:05

答案

  • //constants needed, form level
    
    private const System.Int32 WM_QUERYENDSESSION = 0x11;
    
    
    
    private const System.Int32 WM_CANCELMODE = 0x1f;
    
    //the sub to intercept the windows messages
    
    protected override void WndProc(ref Message ex)
    
    {
    
     if (ex.Msg == WM_QUERYENDSESSION)
    
     {
    
     //cancel the message
    
     Message MyMsg = new Message();
    
     MyMsg.Msg = WM_CANCELMODE;
    
     base.WndProc(MyMsg);
    
     }
    
     else
    
     {
    
     //send the message as normal
    
     base.WndProc(ex);
    
     }
    
    }
    
    
    
    

    小中中的學習筆記

    //constants needed, form level

    private const System.Int32 WM_QUERYENDSESSION = 0x11;



    private const System.Int32 WM_CANCELMODE = 0x1f;

    //the sub to intercept the windows messages

    protected override void WndProc(ref Message ex)

    {

      if (ex.Msg == WM_QUERYENDSESSION)

      {

        //cancel the message

        Message MyMsg = new Message();

        MyMsg.Msg = WM_CANCELMODE;

        base.WndProc(ref MyMsg);

      }

      else

      {

        //send the message as normal

        base.WndProc(ref ex);

      }

    }

    vs2008要修改下就可以了
    • 已标记为答案 ybgjj2009 2010年8月3日 7:36
    2010年8月3日 7:35

全部回复

  • 你可以使窗体类继承Control.WndProc,然后响应WM_QUERYENDSESSION事件,比如

    'constants needed, form level
    Private Const WM_QUERYENDSESSION As System.Int32 = &H11
    Private Const WM_CANCELMODE As System.Int32 = &H1F
    
    'the sub to intercept the windows messages
    Protected Overrides Sub WndProc(ByRef ex As Message)
      If ex.Msg = WM_QUERYENDSESSION Then
        'cancel the message
        Dim MyMsg As New Message
        MyMsg.Msg = WM_CANCELMODE
        MyBase.WndProc(MyMsg)
      Else
        'send the message as normal
        MyBase.WndProc(ex)
      End If
    End Sub 

    2010年8月2日 5:28
  • 你可以使窗体类继承Control.WndProc,然后响应WM_QUERYENDSESSION事件,比如

     

    'constants needed, form level
    
    Private Const WM_QUERYENDSESSION As System.Int32 = &H11
    
    Private Const WM_CANCELMODE As System.Int32 = &H1F
    
    
    
    'the sub to intercept the windows messages
    
    Protected Overrides Sub WndProc(ByRef ex As Message)
    
     If ex.Msg = WM_QUERYENDSESSION Then
    
      'cancel the message
    
      Dim MyMsg As New Message
    
      MyMsg.Msg = WM_CANCELMODE
    
      MyBase.WndProc(MyMsg)
    
     Else
    
      'send the message as normal
    
      MyBase.WndProc(ex)
    
     End If
    
    End Sub 
    
    

     


    vb的代码.........
    2010年8月3日 1:13
  • //constants needed, form level
    private const System.Int32 WM_QUERYENDSESSION = 0x11;
    
    private const System.Int32 WM_CANCELMODE = 0x1f;
    //the sub to intercept the windows messages
    protected override void WndProc(ref Message ex)
    {
      if (ex.Msg == WM_QUERYENDSESSION)
      {
        //cancel the message
        Message MyMsg = new Message();
        MyMsg.Msg = WM_CANCELMODE;
        base.WndProc(MyMsg);
      }
      else
      {
        //send the message as normal
        base.WndProc(ex);
      }
    }
    
    

    小中中的學習筆記
    2010年8月3日 3:22
  • //constants needed, form level
    
    private const System.Int32 WM_QUERYENDSESSION = 0x11;
    
    
    
    private const System.Int32 WM_CANCELMODE = 0x1f;
    
    //the sub to intercept the windows messages
    
    protected override void WndProc(ref Message ex)
    
    {
    
     if (ex.Msg == WM_QUERYENDSESSION)
    
     {
    
     //cancel the message
    
     Message MyMsg = new Message();
    
     MyMsg.Msg = WM_CANCELMODE;
    
     base.WndProc(MyMsg);
    
     }
    
     else
    
     {
    
     //send the message as normal
    
     base.WndProc(ex);
    
     }
    
    }
    
    
    
    

    小中中的學習筆記

    //constants needed, form level

    private const System.Int32 WM_QUERYENDSESSION = 0x11;



    private const System.Int32 WM_CANCELMODE = 0x1f;

    //the sub to intercept the windows messages

    protected override void WndProc(ref Message ex)

    {

      if (ex.Msg == WM_QUERYENDSESSION)

      {

        //cancel the message

        Message MyMsg = new Message();

        MyMsg.Msg = WM_CANCELMODE;

        base.WndProc(ref MyMsg);

      }

      else

      {

        //send the message as normal

        base.WndProc(ref ex);

      }

    }

    vs2008要修改下就可以了
    • 已标记为答案 ybgjj2009 2010年8月3日 7:36
    2010年8月3日 7:35