C++ 当嵌入式ActiveX控件接收到并失去键盘焦点时,如何获得通知?

C++ 当嵌入式ActiveX控件接收到并失去键盘焦点时,如何获得通知?,c++,winapi,mfc,activex,activexobject,C++,Winapi,Mfc,Activex,Activexobject,我有一个基于C++/MFC对话框的小型应用程序,带有嵌入式Internet Explorer ActiveX控件。我想知道嵌入式控件何时接收和丢失键盘焦点。我想这样做: BOOL CWinAppDerivedClass::PreTranslateMessage(MSG* pMsg) { if(pMsg->message == WM_SETFOCUS) { //if(checkIEWindow(pMsg->hwnd)) {

我有一个基于C++/MFC对话框的小型应用程序,带有嵌入式Internet Explorer ActiveX控件。我想知道嵌入式控件何时接收和丢失键盘焦点。我想这样做:

BOOL CWinAppDerivedClass::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_SETFOCUS)
    {
        //if(checkIEWindow(pMsg->hwnd))
        {
            //Process it
        }
    }

    return CWinApp::PreTranslateMessage(pMsg);
}
但无论我做什么,WM_SETFOCUS似乎根本没有被发送出去


你知道怎么做吗?

一种方法是在整个流程中使用

首先,您需要将钩子安装在GUI应用程序主线程的某个位置。如果是MFC对话框窗口,好的位置是
OnInitDialog
通知处理程序:

//hHook is "this" class member variable, declared as HHOOK. Set it to NULL initially.
hHook = ::SetWindowsHookEx(WH_CALLWNDPROC, CallWndProcHook, AfxGetInstanceHandle(), ::GetCurrentThreadId());
然后,可以将挂钩程序设置为:

static LRESULT CALLBACK CallWndProcHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION) 
    {
        CWPSTRUCT* pCWS = (CWPSTRUCT*)lParam;

        //Check if this is the message we need
        if(pCWS->message == WM_SETFOCUS ||
            pCWS->message == WM_KILLFOCUS)
        {
            //Check if this is the window we need
            if(pCWS->hwnd == hRequiredWnd)
            {
                //Process your message here
            }
        }
    }

    return ::CallNextHookEx(NULL, nCode, wParam, lParam);
}
还记得要脱钩。它的一个好地方是
PostNcDestroy
处理程序:

if(hHook != NULL)
{
    ::UnhookWindowsHookEx(hHook);
    hHook = NULL;
}