MFC ActiveX里面的非模态对话框是没有消息循环的,必须得用Hook才行。在主窗体中增加hook处理
1.添加全局hook变量和回调函数
HHOOK hHook = NULL;//设立一个消息钩子
LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam);
2.OnCreate函数中初始化hook
// Install the WH_GETMESSAGE hook function.
hHook = ::SetWindowsHookEx(
WH_GETMESSAGE,
GetMessageProc,
NULL,
GetCurrentThreadId());
ASSERT (hHook);
3.设置回调函数
LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// Switch the module state for the correct handle to be used.
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
// If this is a keystrokes message, translate it in controls'
// PreTranslateMessage().
LPMSG lpMsg = (LPMSG) lParam; if( (nCode >= 0) &&
PM_REMOVE == wParam &&
AfxGetApp()->PreTranslateMessage((LPMSG)lParam))
{
// The value returned from this hookproc is ignored, and it cannot
// be used to tell Windows the message has been handled. To avoid
// further processing, convert the message to WM_NULL before
// returning.
//MessageBox(NULL,_T("Test"),NULL,NULL);//测试
//lpMsg->message = WM_NULL;
//lpMsg->lParam = 0L;
lpMsg->wParam = 0;
}
// Passes the hook information to the next hook procedure in
// the current hook chain.
return ::CallNextHookEx(hHook, nCode, wParam, lParam);
}
4.在OnClose函数中卸载hook
VERIFY (::UnhookWindowsHookEx (hHook));
1.添加全局hook变量和回调函数
HHOOK hHook = NULL;//设立一个消息钩子
LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam);
2.OnCreate函数中初始化hook
// Install the WH_GETMESSAGE hook function.
hHook = ::SetWindowsHookEx(
WH_GETMESSAGE,
GetMessageProc,
NULL,
GetCurrentThreadId());
ASSERT (hHook);
3.设置回调函数
LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
// Switch the module state for the correct handle to be used.
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
// If this is a keystrokes message, translate it in controls'
// PreTranslateMessage().
LPMSG lpMsg = (LPMSG) lParam; if( (nCode >= 0) &&
PM_REMOVE == wParam &&
AfxGetApp()->PreTranslateMessage((LPMSG)lParam))
{
// The value returned from this hookproc is ignored, and it cannot
// be used to tell Windows the message has been handled. To avoid
// further processing, convert the message to WM_NULL before
// returning.
//MessageBox(NULL,_T("Test"),NULL,NULL);//测试
//lpMsg->message = WM_NULL;
//lpMsg->lParam = 0L;
lpMsg->wParam = 0;
}
// Passes the hook information to the next hook procedure in
// the current hook chain.
return ::CallNextHookEx(hHook, nCode, wParam, lParam);
}
4.在OnClose函数中卸载hook
VERIFY (::UnhookWindowsHookEx (hHook));
解决方案 »
- SetTextColor(hMemDC,RGB(255,0,255));//设置字体颜色为什么不行?
- 游戏发布:俄罗斯方块MFC版源码,0资源分,散分100!
- 几个蛋疼的头文件
- 有关VC++6.0中全局变量的问题:为什么在Global.h中不能够再加全局变量了呢
- 注册表监控
- sendmessage()和postmessage()有什么区别,请高手指点
- 高分没人要,低分呢?
- 怎样遍历CTreeCtrl内容,找到匹配节点的内容,则选中并删除它?
- 怎样实现点击不同的菜单项打开同一个对话框,而且对话框的某些显示信息要根据所点击的菜单项来显示不同的内容
- 各位帮帮忙啊。我的头都大了
- opencv MeanShift 目标跟踪的定量分析,求指教
- 24位bmp位图结构问题,熟悉图像文件的结构的高手们,求解啊!!!
那就是我每响应一个消息事件,我就必须做一个对应的HOOK来触发它?
我放在调用active的主程序也是一样的,没有进入到控件的on_create中去
我放在调用active的主程序也是一样的,没有进入到控件的on_create中去不是,肯定要放在ActiveX里面呐,放在xxx.cpp或xxxCtrl.cpp里面,我之前的一个项目就是采取这种方式,ActiveX里面的所有非模态对话框都可以响应消息了
我放在调用active的主程序也是一样的,没有进入到控件的on_create中去不是,肯定要放在ActiveX里面呐,放在xxx.cpp或xxxCtrl.cpp里面,我之前的一个项目就是采取这种方式,ActiveX里面的所有非模态对话框都可以响应消息了
现在我的activex里面还没有创建任何窗口,activex ctrl的on_create就没办法触发了
我放在调用active的主程序也是一样的,没有进入到控件的on_create中去不是,肯定要放在ActiveX里面呐,放在xxx.cpp或xxxCtrl.cpp里面,我之前的一个项目就是采取这种方式,ActiveX里面的所有非模态对话框都可以响应消息了
现在我的activex里面还没有创建任何窗口,activex ctrl的on_create就没办法触发了不一定是OnCreate里面。。程序初始化Init时加载hook,exit里面卸载hook