?? 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 + -