c++ - 当嵌入式 ActiveX 控件接收和失去键盘焦点时如何得到通知?

标签 c++ winapi mfc activex activexobject

我有一个基于 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好像根本就没有发出来。

知道怎么做吗?

最佳答案

一种方法是在进程范围内使用 window procedure hook .

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

//hHook is "this" class member variable, declared as HHOOK. Set it to NULL initially.
hHook = ::SetWindowsHookEx(WH_CALLWNDPROC, CallWndProcHook, AfxGetInstanceHandle(), ::GetCurrentThreadId());

然后钩子(Hook)过程可以这样设置:

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

关于c++ - 当嵌入式 ActiveX 控件接收和失去键盘焦点时如何得到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438613/

相关文章:

c++ - 使用 Qt C++ QWebView 会导致 GUI 运行缓慢。

c++ - 使用 setjmp longjmp 时出现段错误

c++ - 如何显示来自另一个进程的模态对话框窗口?

c++ - 高分辨率显示器导致MFC中View文字模糊

c# - 全局捕获按键,但无论焦点如何,仅抑制当前应用程序和子窗​​口

c++ - 给 Label C++ 的首字母加下划线(下划线不显示)

android - 从sd卡动态加载共享库

java - corrupted double-linked list error 第一次运行程序时,后续运行正常

c++ - 通过注册表设置程序自动运行

c++ - 通过 IShellFolder 接口(interface)枚举打印机文件夹导致堆分配泄漏