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

Posted

技术标签:

【中文标题】当嵌入式 ActiveX 控件接收并失去键盘焦点时如何获得通知?【英文标题】:How to get notified when an embedded ActiveX control recieves and loses keyboard focus? 【发布时间】:2014-05-02 23:49:41 【问题描述】:

我有一个基于 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 似乎都没有发送出去。

知道怎么做吗?

【问题讨论】:

【参考方案1】:

一种方法是使用进程范围的window procedurehook。

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

【讨论】:

以上是关于当嵌入式 ActiveX 控件接收并失去键盘焦点时如何获得通知?的主要内容,如果未能解决你的问题,请参考以下文章

WPF虚拟键盘如何不获得当前焦点

EXCEL 文本框控件点击文字变很小,失去焦点后又回复正常,这怎么解决?

JAVA中如何判断JTextField的一些问题?求高手解答……谢谢

Android进入一个新页面,EditText失去焦点并禁止弹出键盘

如何创建一个可以将键发送到控件而不会失去焦点的按钮 - 虚拟键盘

当控件即将失去焦点时,是不是存在从 C++ 程序触发的事件?