【问题标题】:How to get notified when an embedded ActiveX control recieves and loses keyboard focus?当嵌入式 ActiveX 控件接收并失去键盘焦点时如何获得通知?
【发布时间】: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 似乎都没有发送出去。

知道怎么做吗?

【问题讨论】:

    标签: c++ winapi mfc activex activexobject


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2012-05-30
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多