WM_POWERBROADCAST 消息未在 MFC Dlg 中捕获

2022-01-12 00:00:00 mfc power-management windows-messages

当系统进入睡眠模式时,我尝试捕捉 WM_POWERBROADCAST 消息.

I try to catch WM_POWERBROADCAST message when the system goes into sleep mode.

我正在这样做:

BOOL CPowManApp::PreTranslateMessage(MSG* pMsg) 
{
    if(pMsg->message == WM_POWERBROADCAST || pMsg->message == WM_POWER)
    {
        CString strMessage;

        strMessage.Format(_T("%d WM_POWERB%s wParam %x lParam %x"),
                         pMsg->time,
                         pMsg->message == WM_POWER?_T(""):_T("BRAODCAST"),
                         pMsg->wParam,
                         pMsg->lParam);

        OutputDebugString(strMessage);
    }

    return CWinApp::PreTranslateMessage(pMsg);
}

它根本不起作用.同时,一个 win32 应用程序工作得很好.我试图将消息处理程序放在 Dlg 类中是徒劳的.

It simply doesn't work. Meanwhile a win32 app works just fine. I tried to put the message handler in the Dlg class in vain.

我正在使用 VS6.0 构建应用程序.我哪里错了?

I'm building the app with VS6.0. Where am I wrong?

推荐答案

在你的消息映射中

ON_MESSAGE( WM_POWERBROADCAST, OnPowerBroadcast )

实施

LRESULT CDialogDlg::OnPowerBroadcast(WPARAM wParam, LPARAM lParam)
{
    switch (wParam)
    {
        case PBT_...
    }
}

请务必查看 MSDNwParam 值周围的一些特定于操作系统的情况.

Be sure to check MSDN for some OS-specific cases around the wParam values.

相关文章