Mousehook.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:3k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // Mousehook.cpp : Defines the initialization routines for the DLL.
  2. //
  3. #include "stdafx.h"
  4. #include <afxdllx.h>
  5. #include "Mousehook.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #pragma data_seg("mydata")
  12. HWND glhPrevTarWnd=NULL;//上次鼠标所指的窗口句柄
  13. HWND glhDisplayWnd=NULL;//显示目标窗口标题编辑框的句柄
  14. HHOOK glhHook=NULL;//安装的鼠标勾子句柄
  15. HINSTANCE glhInstance=NULL;//DLL实例句柄
  16. #pragma data_seg()
  17. LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam);
  18. static AFX_EXTENSION_MODULE MousehookDLL = { NULL, NULL };
  19. extern "C" int APIENTRY
  20. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  21. {
  22. // Remove this if you use lpReserved
  23. UNREFERENCED_PARAMETER(lpReserved);
  24. if (dwReason == DLL_PROCESS_ATTACH)
  25. {
  26. TRACE0("MOUSEHOOK.DLL Initializing!n");
  27. // Extension DLL one-time initialization
  28. if (!AfxInitExtensionModule(MousehookDLL, hInstance))
  29. return 0;
  30. new CDynLinkLibrary(MousehookDLL);
  31. glhInstance=hInstance;//插入保存DLL实例句柄
  32. }
  33. else if (dwReason == DLL_PROCESS_DETACH)
  34. {
  35. TRACE0("MOUSEHOOK.DLL Terminating!n");
  36. // Terminate the library before destructors are called
  37. AfxTermExtensionModule(MousehookDLL);
  38. }
  39. return 1;   // ok
  40. }
  41. //////////////////////////////////////////////////////////////////////
  42. // Cmousehook Class
  43. //////////////////////////////////////////////////////////////////////
  44. //////////////////////////////////////////////////////////////////////
  45. // Construction/Destruction
  46. //////////////////////////////////////////////////////////////////////
  47. Cmousehook::Cmousehook()
  48. {
  49. }
  50. Cmousehook::~Cmousehook()
  51. {
  52. stophook();
  53. }
  54. BOOL Cmousehook::starthook(HWND hWnd)
  55. {
  56. //安装钩子并设定接收显示窗口句柄
  57. BOOL bResult=FALSE;
  58. glhHook=SetWindowsHookEx(WH_MOUSE,MouseProc,glhInstance,0);
  59. if(glhHook!=NULL)
  60. bResult=TRUE;
  61. //设置显示目标窗口标题编辑框的句柄
  62. glhDisplayWnd=hWnd;
  63. return bResult;
  64. }
  65. BOOL Cmousehook::stophook()
  66. {
  67. //卸载钩子
  68. BOOL bResult=FALSE;
  69. if(glhHook)
  70. bResult= UnhookWindowsHookEx(glhHook);
  71. if(bResult)
  72. {
  73. glhPrevTarWnd=NULL;
  74. glhDisplayWnd=NULL;//清变量
  75. glhHook=NULL;
  76. }
  77. return bResult;
  78. }
  79. LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam)
  80. {
  81. LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *)lparam;
  82. if (nCode>=0)
  83. {
  84. HWND glhTargetWnd=pMouseHook->hwnd;//取目标窗口句柄
  85. HWND ParentWnd=glhTargetWnd;
  86. while (ParentWnd !=NULL)
  87. {
  88. glhTargetWnd=ParentWnd;
  89. ParentWnd=GetParent(glhTargetWnd);//取应用程序主窗口句柄
  90. }
  91. if(glhTargetWnd!=glhPrevTarWnd)
  92. {
  93. char szCaption[100];
  94. GetWindowText(glhTargetWnd,szCaption,100);//取目标窗口标题
  95. if(IsWindow(glhDisplayWnd))
  96. {
  97. SendMessage(glhDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szCaption);
  98. glhPrevTarWnd=glhTargetWnd; //保存目标窗口
  99. }
  100. }
  101. }
  102. return CallNextHookEx(glhHook,nCode,wparam,lparam);//继续传递消息
  103. }