如何在 CWinApp 中获取 WM_POWERBROADCAST 消息?

我创建了继承CWinApp的类,这个类有一个定时器(使用窗口定时器).

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

当 PC 进入睡眠模式并唤醒时,定时器回调称为唤醒的确切时间.当PC从挂起恢复时,我想不调用定时器回调.

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.

所以我尝试使用 WM_POWERBROADCAST 消息.但是此消息没有在 PreTranslateMessage() API 中捕获.我也尝试了 SetWindowLong() 与我自己的 API,但仍然没有捕捉到 WM_POWERBROADCAST 消息.

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.

有没有办法在CWinApp中获取WM_POWERBROADCAST?

推荐答案

在 Visual Studio C++ MFC 应用程序中,您需要将 ON_MESSAGE() 添加到消息映射中以查找 WM_POWERBROADCAST 消息如本例:

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()

然后您将需要添加消息处理函数以及类定义更改以声明消息处理程序的成员函数,以便您可以检查 wParam 变量的消息类型,如下所示骨骼.请记住返回一个 LRESULT 值,指示您是否处理了该消息.

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
");
            break;
        case PBT_APMRESUMEAUTOMATIC:
            TRACE0("PBT_APMRESUMEAUTOMATIC  received
");
            break;
        case PBT_APMRESUMESUSPEND:
            TRACE0("PBT_APMRESUMESUSPEND  received
");
            break;
        case PBT_APMSUSPEND:
            TRACE0("PBT_APMSUSPEND  received
");
            break;
    }

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

请参阅 Microsoft 文档 - 电源管理 以及该文档的特定小节 Microsoft 文档- WM_POWERBROADCAST 消息 了解有关处理消息的详细信息.

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.

另请参阅 SetThreadExecutionState() 函数 这会影响 Windows 如何确定应用程序是否处于活动状态以及是否应进入睡眠模式.

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.

另请参阅以下 Stack Overflow 帖子:

See also the following Stack Overflow postings:

  • WM_POWERBROADCAST 消息未在 MFC Dlg 中捕获
  • WM_POWERBROADCAST 未收到仅消息Windows XP 中的窗口
  • 如何在线程内接收 WM_POWERBROADCAST?
  • 如何在 Windows Mobile 上检测挂起?
  • 如何检测 Windows 挂起消息?

相关文章