如何在CWinApp中获取WM_POWERBROADCAST消息? [英] How to get WM_POWERBROADCAST message in CWinApp?
问题描述
我创建了继承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消息未在MFC Dlg中捕获
- WM_POWERBROADCAST仅通过消息未收到Windows XP中的窗口
- 如何在线程内部接收WM_POWERBROADCAST? a>
- 如何在Windows Mobile上检测到暂停状态?
- 如何检测Windows暂停消息?
- 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屋!