我?guī)湍阕隽诉@個(gè)簡(jiǎn)易的鉤子,基本實(shí)現(xiàn)了你的功能?
這個(gè)是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);
//?低級(jí)鼠標(biāo)鉤子函數(shù)
LRESULT?CALLBACK?LowLevelMouseProc(
????????????????????int?nCode,?????//?hook?code
????????????????????WPARAM?wParam,?//?message?identifier
????????????????????LPARAM?lParam??//?pointer?to?structure?with?message?data
??????????????????)
{
????if?(nCode?==?HC_ACTION)
??{
????//?如果是滾動(dòng)滑輪,彈出消息框,卸載鉤子函數(shù)
????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
因?yàn)樵趙inuser.h中只有定義了上面的才定義的WH_MOUSE_LL
然后您在另外建一對(duì)話框工程
加一個(gè)按鈕
在工程目錄下加進(jìn)上面的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