需要在APP 代码中添加hock
class CTestApp : public CWinApp
{
public:
CTestApp();
HHOOK m_hHook;
// 重写
public:
static LRESULT CALLBACK GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam);
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
virtual int ExitInstance();
};
```cpp
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
END_MESSAGE_MAP()
// CTestApp 构造
CTestApp::CTestApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的一个 CTestApp 对象
CTestApp theApp;
// CTestApp 初始化
LRESULT CALLBACK CTestApp::GetMessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
LPMSG lpMsg = (LPMSG)lParam;
if (AfxGetApp()->PreTranslateMessage(lpMsg))
{
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(theApp.m_hHook, nCode, wParam, lParam);
}
BOOL CTestApp::InitInstance()
{
CWinApp::InitInstance();
AfxEnableControlContainer();
BOOL bol = AfxOleInit();
//初始化控件环境 AfxEnableControlContainer();
HRESULT rs = CoInitialize(NULL);
m_hHook = ::SetWindowsHookEx(
WH_GETMESSAGE,
GetMessageProc,
AfxGetInstanceHandle(),
GetCurrentThreadId());
return TRUE;
}
int CTestApp::ExitInstance()
{
// TODO: 在此添加专用代码和/或调用基类
UnhookWindowsHookEx((HHOOK)m_hHook);
return CWinApp::ExitInstance();
}
在实际对话框代码中先定义:
CToolTipCtrl m_ToolTip;
在适当的地方初始化
if (m_ToolTip.m_hWnd == NULL)
{
m_ToolTip.Create(this);
m_ToolTip.Activate(TRUE);
m_ToolTip.SetTipBkColor(RGB(255, 255, 255));
m_ToolTip.AddTool(GetDlgItem(IDC_ALG_TOOL_COMBO), _T(""));
m_ToolTip.AddTool(GetDlgItem(IDC_COMBO_IMAGE), _T(""));
}
重写PreTranslateMessage
BOOL CTestDialog::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
switch (pMsg->message)
{
case WM_MOUSEMOVE:
m_ToolTip.RelayEvent(pMsg);
break;
default:
break;
}
// 不要对消息拦截,按实际情况处理
return FALSE;
}