How to get WM_POWERBROADCAST message in CWinApp?

随声附和 提交于 2020-01-14 03:04:23

问题


I create the class that inherited CWinApp and this class has a timer (use a window timer).

When PC go sleep mode and wake-up, timer callback is called exact time of wake-up. I want to make to not call the timer callback when PC is resuming from suspend.

So I tried to use WM_POWERBROADCAST message. But this message didn't catch in PreTranslateMessage() API. Also I tried SetWindowLong() with my own API but still didn't catch the WM_POWERBROADCAST message.

Is there any way to get WM_POWERBROADCAST in CWinApp?


回答1:


In a Visual Studio C++ MFC application you will need to add an ON_MESSAGE() to your message map looking for the WM_POWERBROADCAST message as in this example:

BEGIN_MESSAGE_MAP(CFrameworkWndApp, CWinApp)
    //{{AFX_MSG_MAP(CFrameworkWndApp)
    ON_WM_CHAR()
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_POWERBROADCAST, OnPowerMsgRcvd)
END_MESSAGE_MAP()

Then you will need to add the message handler function along with the class definition change to declare the member function for the message handler so that you can check the wParam variable for the message type as in this skeleton. Remember to return an LRESULT value indicating if you handled the message or not.

// Handle the WM_POWERBROADCAST message to process a message concerning power management
// such as going to Sleep or Waking Up.
LRESULT CFrameworkWndApp::OnPowerMsgRcvd(WPARAM wParam, LPARAM lParam)
{
    LRESULT  lrProcessed = 0;  // indicate if message processed or not

    switch (wParam) {
        case PBT_APMPOWERSTATUSCHANGE:
            TRACE0("PBT_APMPOWERSTATUSCHANGE  received\n");
            break;
        case PBT_APMRESUMEAUTOMATIC:
            TRACE0("PBT_APMRESUMEAUTOMATIC  received\n");
            break;
        case PBT_APMRESUMESUSPEND:
            TRACE0("PBT_APMRESUMESUSPEND  received\n");
            break;
        case PBT_APMSUSPEND:
            TRACE0("PBT_APMSUSPEND  received\n");
            break;
    }

    // indicate if framework needs to handle message or we did ourselves.
    return lrProcessed;
}

See Microsoft documentation - Power Management as well as the particular subsection of that documentation Microsoft documentation - WM_POWERBROADCAST message for details on handling the message.

See also the SetThreadExecutionState() function which affects how Windows determines whether an application is active or not and whether sleep mode should be entered or not.

See also the following Stack Overflow postings:

  • WM_POWERBROADCAST message not caught in MFC Dlg
  • WM_POWERBROADCAST not received by message-only window in Windows XP
  • How to receive WM_POWERBROADCAST inside of a thread?
  • How can I detect suspend on Windows Mobile?
  • How to detect Windows suspend message?



回答2:


It is a Windows message. It gets sent to all Top-Level windows. So, in order to catch this message, create a handler in your main window



来源:https://stackoverflow.com/questions/14765534/how-to-get-wm-powerbroadcast-message-in-cwinapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!