小四的海市蜃楼
Never surrender to complexity
posts - 21,comments - 59,trackbacks - 0

由于在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);

posted on 2009-01-06 10:24 小四 阅读(1891) 评论(1)  编辑 收藏 引用 所属分类: 算法与数据结构

FeedBack:
# re: DLL窗体中PreTranslateMessage的解决方案
2009-06-09 17:09 | Liucy
有没有例子代码给一份?  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理