使得OCX控件响应PreTranslateMessage消息的步骤

更多精彩内容,请见:http://www.16boke.com


by zxy,QQ群:168424095

1.首先在OCX的cpp文件中声明一个全局变量:

static HHOOK hHook=NULL;

2.定义hHook处理函数:

LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{

 AFX_MANAGE_STATE(AfxGetStaticModuleState( ));//模块切换时的状态保护指向当前模块状态

 LPMSG lpMsg = (LPMSG) lParam;
 if( (nCode >= 0) && (PM_REMOVE == wParam) &&
  AfxGetApp()->PreTranslateMessage(lpMsg))
 {
  lpMsg->message = WM_NULL;
  lpMsg->lParam = 0L;
  lpMsg->wParam = 0;
 }


 // 将钩子消息传递给当前钩子链中的下一个钩子

 return ::CallNextHookEx(hHook, nCode, wParam, lParam);
}

3.在OCX的"Oncreate"中添加如下代码:

 hHook = ::SetWindowsHookEx(WH_GETMESSAGE, GetMessageProc,
  AfxGetInstanceHandle(),GetCurrentThreadId());

4.释放资源,在OCX的"OnDestroy"消息中添加如下代码:

VERIFY(::UnhookWindowsHookEx(hHook));


更多精彩内容,请见:http://www.16boke.com

你可能感兴趣的:(使得OCX控件响应PreTranslateMessage消息的步骤)