⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mousehookdll.cpp

📁 vc++编程
💻 CPP
字号:
// MouseHookDll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "MouseHookDLl.h"
#include <afxdllx.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static AFX_EXTENSION_MODULE MouseHookDllDLL = { NULL, NULL };

#pragma data_seg("ShareData")
HHOOK hMouseHook=NULL;			//mouse hook
HINSTANCE glhInstance=NULL;		//globle instance
HWND glhPrevTarWnd=NULL; 
HWND glhDisplayWnd=NULL;
BOOL bHookProc = FALSE;
#pragma data_seg()

LRESULT WINAPI MouseHookProc(int nCode,WPARAM wParam ,LPARAM lParam);

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("MOUSEHOOKDLL.DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(MouseHookDllDLL, 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(MouseHookDllDLL);
		glhInstance = hInstance;
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("MOUSEHOOKDLL.DLL Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(MouseHookDllDLL);
	}
	return 1;   // ok
}


BOOL __stdcall StartHook(HWND hWnd)
{			//install hoook
	hMouseHook=SetWindowsHookEx(WH_MOUSE,MouseHookProc,glhInstance,0);
			//mouse hook
	if(hMouseHook)
	{
		glhDisplayWnd=hWnd;	//设置显示目标窗口标题编辑框的句柄 
		return TRUE;
	}
	return FALSE;
}

BOOL __stdcall StopHook()
{			//unstall hook
	BOOL mHook=UnhookWindowsHookEx(hMouseHook);
	if(mHook)
		return TRUE;
	return FALSE;
}

LRESULT WINAPI MouseHookProc(int nCode,WPARAM wParam ,LPARAM lParam)
{		//mouse hook procedure
	LPMOUSEHOOKSTRUCT lpMouse=(MOUSEHOOKSTRUCT FAR*)lParam;

	if (nCode>=0)  
	{  
		HWND glhTargetWnd=lpMouse->hwnd; //取目标窗口句柄  
		HWND ParentWnd=glhTargetWnd;  
		while (ParentWnd !=NULL)  
		{  
			glhTargetWnd=ParentWnd;  
			ParentWnd=GetParent(glhTargetWnd); //取应用程序主窗口句柄  
		}  
		if(glhTargetWnd!=glhPrevTarWnd)  
		{  
			char szCaption[100];  
			GetWindowText(glhTargetWnd,szCaption,100); //取目标窗口标题  
			if(IsWindow(glhDisplayWnd))  
			SendMessage(glhDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szCaption);  
			glhPrevTarWnd=glhTargetWnd; //保存目标窗口  
		}  
	}  

	return CallNextHookEx(hMouseHook,nCode,wParam,lParam);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -