如何在嵌入式ActiveX控件接收并失去键盘焦点时收到通知?

时间:2014-05-02 23:49:41

标签: c++ winapi mfc activex activexobject

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

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

    return CWinApp::PreTranslateMessage(pMsg);
}

但无论我做什么,WM_SETFOCUS似乎都没有被发送出去。

知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是使用流程范围window procedure hook

首先,您需要从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;
}