📄 myhookdll.cpp
字号:
// MyHookDll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <afxdllx.h>
#include "MyHookDll.h"
// 定义共享数据
#pragma data_seg("My_Mouse_Shared")
HWND g_hPrevTarWnd = NULL; // 上次鼠标所指的窗口句柄
HWND g_hDisplayWnd = NULL; // 显示目标窗口标题编辑框的句柄
HHOOK g_hHook=NULL; // 安装的鼠标钩子句柄
HINSTANCE g_hInstance=NULL; // DLL实例句柄
//LPMOUSEHOOKSTRUCT pMouseHook;
#pragma data_seg()
// 通过程序指定链接选项,真正使得数据能够共享
#pragma comment(linker, "/SECTION:My_Mouse_Shared,RWS")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static AFX_EXTENSION_MODULE MyHookDllDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("MYHOOKDLL.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(MyHookDllDLL, 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(MyHookDllDLL);
g_hInstance=hInstance; //插入保存DLL实例句柄
//g_hInstance=AfxGetInstanceHandle(); //插入保存DLL实例句柄
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("MYHOOKDLL.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(MyHookDllDLL);
}
return 1; // ok
}
CMyHookDll::CMyHookDll()
{
}
CMyHookDll::~CMyHookDll()
{
UnInstallHook();
}
BOOL CMyHookDll::InstallHook(HWND hWnd)
{
BOOL bResult=FALSE;
g_hHook=SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, NULL);
if(g_hHook!=NULL)
{
bResult=TRUE;
}
g_hDisplayWnd = hWnd; //设置显示目标窗口标题编辑框的句柄
return bResult;
}
BOOL CMyHookDll::UnInstallHook()
{
BOOL bResult=FALSE;
if(g_hHook)
{
bResult= UnhookWindowsHookEx(g_hHook);
if(bResult)
{
g_hPrevTarWnd=NULL;
g_hDisplayWnd=NULL; //清变量
g_hHook=NULL;
}
}
return bResult;
}
LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *)lparam;
//pMouseHook=(MOUSEHOOKSTRUCT FAR *)lparam;
if (nCode>=0 && WM_RBUTTONDOWN == wparam)
{
HWND hTargetWnd=pMouseHook->hwnd; //取目标窗口句柄
HWND ParentWnd=hTargetWnd;
while (ParentWnd !=NULL)
{
hTargetWnd=ParentWnd;
ParentWnd=GetParent(hTargetWnd); //取应用程序主窗口句柄
}
if(hTargetWnd!=g_hPrevTarWnd) //(wparam == WM_LBUTTONDOWN)//
{
char szCaption[100];
GetWindowText(hTargetWnd,szCaption,100); //取目标窗口标题
if(IsWindow(g_hDisplayWnd))
{
SendMessage(g_hDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szCaption);
}
g_hPrevTarWnd=hTargetWnd; //保存目标窗口
}
}
//if(nCode >= 0 && wparam==WM_LBUTTONDOWN)
// AfxMessageBox("haha");
return CallNextHookEx(g_hHook,nCode,wparam,lparam); //继续传递消息
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -