ExtHook.h
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:9k
源码类别:

界面编程

开发平台:

Visual C++

  1. // This is part of the Professional User Interface Suite library.
  2. // Copyright (C) 2001-2009 FOSS Software, Inc.
  3. // All rights reserved.
  4. //
  5. // http://www.prof-uis.com
  6. // mailto:support@prof-uis.com
  7. //
  8. // This source code can be used, modified and redistributed
  9. // under the terms of the license agreement that is included
  10. // in the Professional User Interface Suite package.
  11. //
  12. // Warranties and Disclaimers:
  13. // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND
  14. // INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  16. // IN NO EVENT WILL FOSS SOFTWARE INC. BE LIABLE FOR ANY DIRECT,
  17. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES,
  18. // INCLUDING DAMAGES FOR LOSS OF PROFITS, LOSS OR INACCURACY OF DATA,
  19. // INCURRED BY ANY PERSON FROM SUCH PERSON'S USAGE OF THIS SOFTWARE
  20. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  21. #if (!defined __EXT_HOOK_H)
  22. #define __EXT_HOOK_H
  23. #if (!defined __EXT_MFC_DEF_H)
  24. #include <ExtMfcDef.h>
  25. #endif // __EXT_MFC_DEF_H
  26. class __PROF_UIS_API CExtHookSink
  27. {
  28. public:
  29. struct HookSinkArray_t;
  30. struct HookChains_t;
  31. friend struct HookChains_t;
  32. bool m_bEnableDetailedWndHooks:1, m_bEatNcDestroy:1;
  33. typedef
  34. CList < HWND, HWND >
  35. HookedWndList_t;
  36. CExtHookSink(
  37. bool bEnableDetailedWndHooks = true
  38. );
  39. virtual ~CExtHookSink();
  40. virtual bool IsAutoDeleteHookWndSink();
  41. virtual LRESULT OnHookWndMsgNextProcCurrent(
  42. WPARAM wParam,
  43. LPARAM lParam
  44. );
  45. virtual LRESULT OnHookWndMsgNextProcInvoke(
  46. UINT nMessage,
  47. WPARAM wParam,
  48. LPARAM lParam
  49. );
  50. virtual LRESULT OnHookWndMsgDefault();
  51. virtual bool OnHookWndMsg(
  52. LRESULT & lResult,
  53. HWND hWndHooked,
  54. UINT nMessage,
  55. WPARAM & wParam,
  56. LPARAM & lParam
  57. );
  58. virtual void OnPostHookWndMsg(
  59. LRESULT lResult,
  60. HWND hWndHooked,
  61. UINT nMessage,
  62. WPARAM wParam,
  63. LPARAM lParam
  64. );
  65. virtual bool OnHookCmdMsg(
  66. LRESULT & lResult,
  67. HWND hWndHooked,
  68. WORD wNotifyCode,
  69. WORD wID,
  70. HWND hWndCtrl
  71. );
  72. virtual bool OnHookNotifyMsg(
  73. LRESULT & lResult,
  74. HWND hWndHooked,
  75. INT nIdCtrl,
  76. LPNMHDR lpnmhdr
  77. );
  78. virtual bool OnHookPaintMsg(
  79. LRESULT & lResult,
  80. HWND hWndHooked,
  81. HDC hDC
  82. );
  83. virtual bool OnHookEraseBackgroundMsg(
  84. LRESULT & lResult,
  85. HWND hWndHooked,
  86. HDC hDC
  87. );
  88. virtual bool OnHookPrintMsg(
  89. LRESULT & lResult,
  90. HWND hWndHooked,
  91. HDC hDC
  92. );
  93. virtual bool OnHookNcPaintMsg(
  94. LRESULT & lResult,
  95. HWND hWndHooked,
  96. HRGN hRgnUpdate
  97. );
  98. virtual void OnHookWndNcDestroy();
  99. virtual void OnHookWndAttach( HWND hWnd );
  100. virtual void OnHookWndDetach( HWND hWnd );
  101. virtual bool SetupHookWndSink(
  102. HWND hWnd,
  103. bool bRemove = false,
  104. bool bAddToHead = false
  105. );
  106. ULONG SetupHookWndSinkToChilds(
  107. HWND hWnd,
  108. UINT * pDlgCtrlIDs = NULL,
  109. ULONG nCountOfDlgCtrlIDs = 0,
  110. bool bDeep = false
  111. );
  112. virtual void RemoveAllWndHooks();
  113. void GetHookedWindows( HookedWndList_t & _list );
  114. bool IsHookedWindow( HWND hWnd );
  115. }; // class CExtHookSink
  116. #define __EHSEF_MOUSE_MOVE 0x00000001
  117. #define __EHSEF_MOUSE_MOVE_NC 0x00000002
  118. #define __EHSEF_MOUSE_CLICK 0x00000004
  119. #define __EHSEF_MOUSE_CLICK_NC 0x00000008
  120. #define __EHSEF_MOUSE_WHEEL 0x00000010
  121. #define __EHSEF_MOUSE_ALL_WITHOUT_WHEEL (__EHSEF_MOUSE_MOVE|__EHSEF_MOUSE_MOVE_NC|__EHSEF_MOUSE_CLICK|__EHSEF_MOUSE_CLICK_NC)
  122. #define __EHSEF_MOUSE_ALL (__EHSEF_MOUSE_ALL_WITHOUT_WHEEL|__EHSEF_MOUSE_WHEEL)
  123. #define __EHSEF_KEYBOARD 0x00000020
  124. #define __EHSEF_ALL_MOUSE_AND_KEYBOARD (__EHSEF_MOUSE_ALL|__EHSEF_KEYBOARD)
  125. #define __EHSEF_GET_MSG 0x00000040
  126. #define __EHSEF_SYS_MSG 0x00000080
  127. #define __EHSEF_ALL_MSG (__EHSEF_GET_MSG|__EHSEF_SYS_MSG)
  128. #define __EHSEF_WND_PROC_IN 0x00000100
  129. #define __EHSEF_WND_PROC_OUT 0x00000200
  130. #define __EHSEF_WND_PROC_ALL (__EHSEF_WND_PROC_IN|__EHSEF_WND_PROC_OUT)
  131. #define __EHSEF_PRE_TRANSLATION_LL 0x20000000
  132. #define __EHSEF_PRE_TRANSLATION 0x40000000
  133. #define __EHSEF_POST_PROCESSING 0x80000000
  134. #define __EHSEF_ADDITIONAL_PROCESSING (__EHSEF_PRE_TRANSLATION_LL|__EHSEF_PRE_TRANSLATION|__EHSEF_POST_PROCESSING)
  135. #define __EHSEF_BASIC_HOOK_FILTERING (__EHSEF_ALL_MOUSE_AND_KEYBOARD|__EHSEF_ALL_MSG|__EHSEF_WND_PROC_ALL)
  136. #define __EHSEF_ALL 0xFFFFFFFF
  137. #define __EHS_IDX_MOUSE 0
  138. #define __EHS_IDX_KEYBOARD 1
  139. #define __EHS_IDX_GET_MSG 2
  140. #define __EHS_IDX_SYS_MSG 3
  141. #define __EHS_IDX_WND_PROC_IN 4
  142. #define __EHS_IDX_WND_PROC_OUT 5
  143. #define __EHS_IDX_COUNT 6
  144. class __PROF_UIS_API CExtHookSpy
  145. {
  146. struct __PROF_UIS_API HookSpyItem_t
  147. {
  148. CTypedPtrList < CPtrList, CExtHookSpy * > m_list;
  149. CMap < CExtHookSpy *, CExtHookSpy *, bool, bool > m_map;
  150. HHOOK m_hHook;
  151. bool m_bListChanged;
  152. DWORD m_dwEHSEF;
  153. HookSpyItem_t( DWORD dwEHSEF );
  154. void Hook( bool bHook );
  155. bool IsRegistered( CExtHookSpy * pHS, DWORD dwEHSEF );
  156. void Register( CExtHookSpy * pHS, DWORD dwEHSEF, bool bAddToHead );
  157. void Unregister( CExtHookSpy * pHS, DWORD dwEHSEF );
  158. static LRESULT CALLBACK SHS_HookMouseProc(
  159. int nCode,      // hook code
  160. WPARAM wParam,  // message identifier
  161. LPARAM lParam   // mouse coordinates
  162. );
  163. static LRESULT CALLBACK SHS_HookKeyboardProc(
  164. int nCode,      // hook code
  165. WPARAM wParam,  // virtual-key code
  166. LPARAM lParam   // keystroke-message information
  167. );
  168. static LRESULT CALLBACK SHS_HookGetMsgProc(
  169. int nCode,
  170. WPARAM wParam,
  171. LPARAM lParam
  172. );
  173. static LRESULT CALLBACK SHS_HookSysMsgProc(
  174. int nCode,
  175. WPARAM wParam,
  176. LPARAM lParam
  177. );
  178. static LRESULT CALLBACK SHS_HookCallWindowProcIn(
  179. int nCode,
  180. WPARAM wParam,
  181. LPARAM lParam
  182. );
  183. static LRESULT CALLBACK SHS_HookCallWindowProcOut(
  184. int nCode,
  185. WPARAM wParam,
  186. LPARAM lParam
  187. );
  188. };
  189. static HookSpyItem_t g_arr[ __EHS_IDX_COUNT ];
  190. DWORD m_dwEHSEF;
  191. public:
  192. bool m_bHookConvertLocationsToClient:1;
  193. CExtHookSpy(
  194. DWORD dwEHSEF = 0 // auto-register flags, zero - unregister
  195. );
  196. virtual ~CExtHookSpy();
  197. void HookSpyRegister(
  198. DWORD dwEHSEF = __EHSEF_ALL,
  199. bool bAddToHead = true
  200. );
  201. static void HookSpyRegister(
  202. CExtHookSpy * pHS,
  203. DWORD dwEHSEF = __EHSEF_ALL,
  204. bool bAddToHead = true
  205. );
  206. void HookSpyUnregister(
  207. DWORD dwEHSEF = __EHSEF_ALL
  208. );
  209. static void HookSpyUnregister(
  210. CExtHookSpy * pHS,
  211. DWORD dwEHSEF = __EHSEF_ALL
  212. );
  213. DWORD HookSpyGetEHSEF() const
  214. {
  215. ASSERT( this != NULL );
  216. return m_dwEHSEF;
  217. }
  218. bool HookSpySetEHSEF( DWORD dwEHSEF )
  219. {
  220. ASSERT( this != NULL );
  221. if( m_dwEHSEF == dwEHSEF )
  222. return false;
  223. m_dwEHSEF = dwEHSEF;
  224. return true;
  225. }
  226. bool HookSpyModifyEHSEF(
  227. DWORD dwRemoveEHSEF,
  228. DWORD dwAddEHSEF
  229. )
  230. {
  231. ASSERT( this != NULL );
  232. DWORD dwOldEHSEF = HookSpyGetEHSEF();
  233. DWORD dwNewEHSEF = dwOldEHSEF & (~(dwRemoveEHSEF));
  234. dwNewEHSEF |= dwAddEHSEF;
  235. return HookSpySetEHSEF( dwNewEHSEF );
  236. }
  237. static bool HookSpyIsRegistered(
  238. const CExtHookSpy * pHS,
  239. DWORD dwEHSEF = __EHSEF_ALL
  240. );
  241. bool HookSpyIsRegistered(
  242. DWORD dwEHSEF = __EHSEF_ALL
  243. ) const
  244. {
  245. return HookSpyIsRegistered( this, dwEHSEF );
  246. }
  247. protected:
  248. virtual bool HSLL_OnMouseWheel(
  249. WPARAM wParam,
  250. LPARAM lParam
  251. );
  252. virtual bool HSLL_OnMouseMove(
  253. HWND hWnd,
  254. UINT nFlags,
  255. CPoint point
  256. );
  257. virtual bool HSLL_OnMouseClick(
  258. HWND hWnd,
  259. UINT nMessage,
  260. UINT nFlags,
  261. CPoint point
  262. );
  263. virtual bool HSLL_OnKey(
  264. UINT nChar,
  265. UINT nRepCnt,
  266. UINT nFlags
  267. );
  268. virtual bool HSLL_OnGetMsgProc(
  269. bool & bCallNextHook,
  270. LRESULT & lResult,
  271. int nCode,
  272. WPARAM wParam,
  273. MSG * pMSG
  274. );
  275. virtual bool HSLL_OnSysMsgProc(
  276. bool & bCallNextHook,
  277. LRESULT & lResult,
  278. int nCode,
  279. WPARAM wParam,
  280. LPARAM lParam
  281. );
  282. virtual bool HSLL_OnCallWindowProcIn(
  283. bool & bCallNextHook,
  284. LRESULT & lResult,
  285. int nCode,
  286. bool bMessageSentByCurrentThread,
  287. PCWPSTRUCT pMessageData
  288. );
  289. virtual bool HSLL_OnCallWindowProcOut(
  290. bool & bCallNextHook,
  291. LRESULT & lResult,
  292. int nCode,
  293. bool bMessageSentByCurrentThread,
  294. PCWPRETSTRUCT pMessageData
  295. );
  296. virtual bool HSLL_PreTranslateMessage(
  297. MSG * pMSG
  298. );
  299. public:
  300. virtual bool OnHookSpyPreTranslateMessage(
  301. MSG * pMSG
  302. );
  303. virtual bool OnHookSpyPostProcessMessage(
  304. MSG * pMSG
  305. );
  306. virtual bool OnHookSpyMouseWheelMsg(
  307. MSG * pMSG
  308. );
  309. virtual bool OnHookSpyMouseMoveMsg(
  310. MSG * pMSG
  311. );
  312. virtual bool OnHookSpyMouseClickMsg(
  313. MSG * pMSG
  314. );
  315. virtual bool OnHookSpyKeyMsg(
  316. MSG * pMSG
  317. );
  318. virtual bool OnHookSpyGetMsgProc(
  319. MSG * pMSG
  320. );
  321. // virtual bool OnHookSpySysMsgProc(
  322. // MSG * pMSG
  323. // );
  324. virtual bool OnHookSpyCallWindowProcIn(
  325. MSG * pMSG
  326. );
  327. virtual bool OnHookSpyCallWindowProcOut(
  328. MSG * pMSG
  329. );
  330. friend struct HookSpyItem_t;
  331. };
  332. #endif // __EXT_HOOK_H