Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.0 KiB

  1. // MsgHook.h: interface for the CMsgHook class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_MSGHOOK_H__DED2E338_D3D5_11D1_82F1_0000F87A3912__INCLUDED_)
  5. #define AFX_MSGHOOK_H__DED2E338_D3D5_11D1_82F1_0000F87A3912__INCLUDED_
  6. #if _MSC_VER >= 1000
  7. #pragma once
  8. #endif // _MSC_VER >= 1000
  9. class CMsgHook; // forward declaration
  10. //////////////////
  11. // The message hook map is derived from CMapPtrToPtr, which associates
  12. // a pointer with another pointer. It maps an HWND to a CMsgHook, like
  13. // the way MFC's internal maps map HWND's to CWnd's. The first hook
  14. // attached to a window is stored in the map; all other hooks for that
  15. // window are then chained via CMsgHook::m_pNext.
  16. //
  17. class CMsgHookMap : private CMapPtrToPtr
  18. {
  19. // Construction/Destruction
  20. public:
  21. CMsgHookMap();
  22. ~CMsgHookMap();
  23. // Operations
  24. public:
  25. static CMsgHookMap& GetHookMap();
  26. void Add(HWND hwnd, CMsgHook* pMsgHook);
  27. void Remove(CMsgHook* pMsgHook);
  28. void RemoveAll(HWND hwnd);
  29. CMsgHook* Lookup(HWND hwnd);
  30. };
  31. class CMsgHook : public CObject
  32. {
  33. DECLARE_DYNAMIC(CMsgHook)
  34. // Construction/Destruction
  35. public:
  36. CMsgHook();
  37. virtual ~CMsgHook();
  38. // Message Hook Operations
  39. public:
  40. // Hook a window. Hook(NULL) to unhook (automatic on WM_NCDESTROY)
  41. BOOL HookWindow(CWnd* pRealWnd);
  42. BOOL IsHooked() { return m_pWndHooked!=NULL; }
  43. friend LRESULT CALLBACK HookWndProc(HWND, UINT, WPARAM, LPARAM);
  44. friend class CMsgHookMap;
  45. // Utility and Helper Functions
  46. public:
  47. HWND GetSafeHwnd() { return (m_pWndHooked?m_pWndHooked->GetSafeHwnd():NULL); }
  48. // Implementation Operations
  49. protected:
  50. // Override this to handle messages in specific handlers
  51. virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
  52. LRESULT Default(); // call this at the end of handler fns
  53. // Implementation Data Members
  54. protected:
  55. CWnd* m_pWndHooked; // the window hooked
  56. WNDPROC m_pOldWndProc; // ..and original window proc
  57. CMsgHook* m_pNext; // next in chain of hooks for this window
  58. };
  59. #endif // !defined(AFX_MSGHOOK_H__DED2E338_D3D5_11D1_82F1_0000F87A3912__INCLUDED_)