我帮你做了这个简易的钩子,基本实现了你的功能
这个是dll代码
#include "stdafx.h"
#include "GetMouseMessage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CGetMouseMessageApp
BEGIN_MESSAGE_MAP(CGetMouseMessageApp, CWinApp)
//{{AFX_MSG_MAP(CGetMouseMessageApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGetMouseMessageApp construction
CGetMouseMessageApp::CGetMouseMessageApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CGetMouseMessageApp object
CGetMouseMessageApp theApp;
// 定义钩子变量
HHOOK g_hook = NULL;
extern "C" _declspec(dllexport) void EndHook(void);
// 低级鼠标钩子函数
LRESULT CALLBACK LowLevelMouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
)
{
if (nCode == HC_ACTION)
{
// 如果是滚动滑轮,弹出消息框,卸载钩子函数
if (wParam == WM_MOUSEWHEEL)
{
MessageBox(NULL, "Get the mousewheel message", NULL, MB_OK);
EndHook();
}
}
return CallNextHookEx(g_hook, nCode, wParam, lParam);
}
// 安装钩子
extern "C" _declspec(dllexport) void SetHook(void)
{
g_hook = SetWindowsHookEx(
WH_MOUSE_LL, // type of hook to install
LowLevelMouseProc, // address of hook procedure
theApp.m_hInstance, // handle to application instance
0 // identity of thread to install hook for
);
if (g_hook == NULL)
{
MessageBox(NULL, "SetWindowsHookEx Failed", NULL, MB_OK);
return ;
}
return ;
}
// 卸载钩子
extern "C" _declspec(dllexport) void EndHook(void)
{
if (g_hook != NULL)
UnhookWindowsHookEx(g_hook);
}
注意,您必须在stdAfx.h中加上
#define _WIN32_WINNT 0x0500
因为在winuser.h中只有定义了上面的才定义的WH_MOUSE_LL
然后您在另外建一对话框工程
加一个按钮
在工程目录下加进上面的GetMouseMessage.dll和GetMouseMessage.lib
在按钮事件中写
extern "C" _declspec(dllimport) void SetHook(void);
extern "C" _declspec(dllimport) void EndHook(void);
void CTestGetMessageDlg::OnButton1()
{
// TODO: Add your control notification handler code here
SetHook();
}
我试过
完全ok