📄 mousedll.cpp
字号:
// mousedll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <afxdllx.h>
#include "MouseHook.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma data_seg(".sdata")
HWND glhWndTarget=NULL;
HWND glhWndDisplay=NULL;
UINT glnAction=NULL;
UINT glnCount=NULL;
HHOOK glhHook=NULL;
HINSTANCE glhInstance=NULL;
MouseEvent * glpMouseEvents=NULL;
LARGE_INTEGER gl_begintime;
BOOL glbHooked=FALSE;
#pragma data_seg()
static AFX_EXTENSION_MODULE MousedllDLL = { NULL, NULL };
HWND GetTargetWindow(HWND hwnd)
{
HWND hWndTarget=NULL;
HWND hWndParent;
while(!hWndTarget)
{
if(GetWindowLong(hwnd,GWL_STYLE)&WS_SYSMENU)
hWndTarget=hwnd;
else{
hWndParent=GetParent(hwnd);
hWndParent?hwnd=hWndParent:hWndTarget=hwnd;
}
}
return hWndTarget;
}
extern "C" _declspec(dllexport) LRESULT WINAPI
MouseHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
LPMOUSEHOOKSTRUCT pMouse=(MOUSEHOOKSTRUCT FAR *)lParam;
HWND hWndTarget=GetTargetWindow(pMouse->hwnd);
if(glbHooked&&(glnCount<512)){
//是目标窗口和左键
if((hWndTarget==glhWndTarget)&&((wParam==WM_LBUTTONDOWN)||(wParam==WM_LBUTTONUP)))
{
if(glnCount==0){
FILETIME filetime;
::GetSystemTimeAsFileTime(&filetime);
memcpy(&gl_begintime,&filetime,sizeof(LARGE_INTEGER));
}
glnCount++;
glpMouseEvents[glnCount].hWnd=pMouse->hwnd;
glpMouseEvents[glnCount].message=wParam;
glpMouseEvents[glnCount].point.x=pMouse->pt.x;
glpMouseEvents[glnCount].point.y=pMouse->pt.y;
ScreenToClient(pMouse->hwnd,&glpMouseEvents[glnCount].point);
//计算时间
FILETIME filetime;
::GetSystemTimeAsFileTime(&filetime);
LARGE_INTEGER llong;
memcpy(&llong,&filetime,sizeof(LARGE_INTEGER));
LONGLONG llong_elaps;
llong_elaps=llong.QuadPart-gl_begintime.QuadPart;
glpMouseEvents[glnCount].time=(LONGLONG)(llong_elaps/10000);
glpMouseEvents[0].point.x=glnCount;
}
}
return ::CallNextHookEx(glhHook,nCode,wParam,lParam);
}
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
HANDLE hMapObject=NULL;
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("MOUSEDLL.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(MousedllDLL, hInstance))
return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(MousedllDLL);
glhInstance=hInstance;
hMapObject=::CreateFileMapping((HANDLE)-1,NULL,
PAGE_READWRITE,0,sizeof(MouseEvent)*512,
_T("Mouse DLL Share Memory"));
ASSERT(hMapObject);
glpMouseEvents=(MouseEvent*)::MapViewOfFile(hMapObject,
FILE_MAP_WRITE,
0,0,0);
ASSERT(glpMouseEvents);
glpMouseEvents[0].point.x=0;;
FILETIME filetime;
::GetSystemTimeAsFileTime(&filetime);
memcpy(&gl_begintime,&filetime,sizeof(LARGE_INTEGER));
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("MOUSEDLL.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(MousedllDLL);
if(glpMouseEvents)
::UnmapViewOfFile(glpMouseEvents);
if(hMapObject)
::CloseHandle(hMapObject);
}
return 1; // ok
}
CMouseHook::CMouseHook()
{
}
CMouseHook::~CMouseHook()
{
BOOL bResult=FALSE;
if(glhHook)
{
bResult=UnhookWindowsHookEx(glhHook);
if(bResult){
glhWndTarget=NULL;
glhWndDisplay=NULL;
glnAction=NULL;
glhHook=NULL;
}
}
}
HHOOK CMouseHook::Start()
{
glhHook=::SetWindowsHookEx(WH_MOUSE,MouseHookProc,glhInstance,0);
glnCount=0;
glbHooked=FALSE;
return glhHook;
}
BOOL CMouseHook::Continue(BOOL bStartAgain)
{
if(glpMouseEvents)
memset(glpMouseEvents,0,sizeof(MouseEvent));
glnCount=0;
glbHooked=bStartAgain;
return TRUE;
}
void CMouseHook::SetTargetWindow(HWND hwnd)
{
glhWndTarget=hwnd;
}
void CMouseHook::SetAction(UINT nAction)
{
glnAction=nAction;
}
void CMouseHook::SetCount(UINT nCount)
{
glnCount=nCount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -