如何在CWinApp中获取WM_POWERBROADCAST消息? [英] How to get WM_POWERBROADCAST message in CWinApp?

查看:495
本文介绍了如何在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中捕获.我也尝试用自己的API尝试SetWindowLong(),但仍然没有捕获到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吗?

Is there any way to get WM_POWERBROADCAST in CWinApp?

推荐答案

在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\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;
}

请参见 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 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?

这篇关于如何在CWinApp中获取WM_POWERBROADCAST消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆