关于用钩子方法实现 如标题内容
http://hi.baidu.com/laoyoujinew/blog/item/b665d461f0710ed68cb10d5c.html
为什么我按那上面的方法实现总是会出现错误
1.
BOOL CAlertDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类 m_tooltip.RelayEvent(pMsg);
return true;
//return CDialog::PreTranslateMessage(pMsg); 这句话不注释就崩溃了
}
2.注释掉后用return true;可始终不显示提示信息3..关于安装全局钩子,要在DLL中安装
g_hHook = ::SetWindowsHookEx(WH_GETMESSAGE, HookProc, 0, ::GetCurrentThreadId());
具体应该在哪个函数中安装,我现在是安装在
CtestdllPretransApp::InitInstance()
这个函数里了,可始终没有实现想要的效果!
请各位指教,如要测试代码可联系我
QQ:254452118
Email:[email protected]
http://hi.baidu.com/laoyoujinew/blog/item/b665d461f0710ed68cb10d5c.html
为什么我按那上面的方法实现总是会出现错误
1.
BOOL CAlertDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类 m_tooltip.RelayEvent(pMsg);
return true;
//return CDialog::PreTranslateMessage(pMsg); 这句话不注释就崩溃了
}
2.注释掉后用return true;可始终不显示提示信息3..关于安装全局钩子,要在DLL中安装
g_hHook = ::SetWindowsHookEx(WH_GETMESSAGE, HookProc, 0, ::GetCurrentThreadId());
具体应该在哪个函数中安装,我现在是安装在
CtestdllPretransApp::InitInstance()
这个函数里了,可始终没有实现想要的效果!
请各位指教,如要测试代码可联系我
QQ:254452118
Email:[email protected]
由于在DLL窗体中需要使用CToolTipCtrl来实现提示功能,
所以要重载PreTranslateMessage,代码如下BOOL CMyWnd::PreTranslateMessage(MSG* pMsg)
{
m_toolTip.RelayEvent(pMsg);
return CGameWnd::PreTranslateMessage(pMsg);
}但是由于CMyWnd是在DLL中,所以重载PreTranslateMessage无效,
具体原因我忘记了,反正网上都可以搜索到。不过解决方案都给的
不是很明确,在这里我把我的解决方法记录下来,省得以后忘掉了。
主要是在创建CMyWnd实例的时候把窗口指针存下来,然后使用全局
消息钩子执行CMyWnd::PreTranslateMessage,代码如下1.定义全局变量
HHOOK g_hHook = 0;
CMyWnd* g_pMyWnd = NULL;2.安装全局钩子,要在DLL中安装
g_hHook = ::SetWindowsHookEx(WH_GETMESSAGE, HookProc, 0, ::GetCurrentThreadId());3.创建窗体的时候保留指针
m_pMyWnd = new CMyWnd();
g_pMyWnd = m_pMyWnd;4.钩子函数
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MSG* pMsg = (MSG*)lParam;
if(g_pMyWnd && (pMsg->hwnd == g_pMyWnd->m_hWnd))
{
g_pMyWnd->PreTranslateMessage(pMsg);
}
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}5.退出的时候别忘了卸载钩子
if(g_hHook)
UnhookWindowsHookEx(g_hHook);
AFX_MANAGE_STATE(AfxGetStaticModuleState ());