我有一个基于 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/