Subclass.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef _SUBCLASSW_H
  2. #define _SUBCLASSW_H
  3. ////////////////////////////////////////////////////////////////
  4. // Copyright 1998 Paul DiLascia
  5. // If this code works, it was written by Paul DiLascia.
  6. // If not, I don't know who wrote it.
  7. //
  8. /////////////////////////////////////////////////////////////////////////////
  9. /****************************************************************************
  10. *
  11. * $Date: 10/26/99 10:50p $
  12. * $Revision: 4 $
  13. * $Archive: /CodeJock/Include/Subclass.h $
  14. *
  15. * $History: Subclass.h $
  16. *
  17. * ***************** Version 4 *****************
  18. * User: Kirk Stowell Date: 10/26/99 Time: 10:50p
  19. * Updated in $/CodeJock/Include
  20. * Made class methods virtual for inheritance purposes.
  21. *
  22. * ***************** Version 3 *****************
  23. * User: Kirk Stowell Date: 10/14/99 Time: 12:41p
  24. * Updated in $/CodeJock/Include
  25. * Added source control history to file header.
  26. *
  27. ***************************************************************************/
  28. /////////////////////////////////////////////////////////////////////////////
  29. //////////////////
  30. // Generic class to hook messages on behalf of a CWnd.
  31. // Once hooked, all messages go to CSubclassWnd::WindowProc before going
  32. // to the window. Specific subclasses can trap messages and do something.
  33. //
  34. // To use:
  35. //
  36. // * Derive a class from CSubclassWnd.
  37. //
  38. // * Override CSubclassWnd::WindowProc to handle messages. Make sure you call
  39. // CSubclassWnd::WindowProc if you don't handle the message, or your
  40. // window will never get messages. If you write seperate message handlers,
  41. // you can call Default() to pass the message to the window.
  42. //
  43. // * Instantiate your derived class somewhere and call HookWindow(pWnd)
  44. // to hook your window, AFTER it has been created.
  45. // To unhook, call HookWindow(NULL).
  46. //
  47. // This is a very important class, crucial to many of the widgets Window
  48. // widgets implemented in PixieLib. To see how it works, look at the HOOK
  49. // sample program.
  50. //
  51. class CSubclassWnd : public CObject {
  52. public:
  53. DECLARE_DYNAMIC(CSubclassWnd);
  54. CSubclassWnd();
  55. ~CSubclassWnd();
  56. // Subclass a window. Hook(NULL) to unhook (automatic on WM_NCDESTROY)
  57. virtual BOOL HookWindow(HWND hwnd);
  58. virtual BOOL HookWindow(CWnd* pWnd) { return HookWindow(pWnd->GetSafeHwnd()); }
  59. virtual BOOL IsHooked() { return m_hWnd!=NULL; }
  60. friend LRESULT CALLBACK HookWndProc(HWND, UINT, WPARAM, LPARAM);
  61. friend class CSubclassWndMap;
  62. #ifdef _DEBUG
  63. virtual void AssertValid() const;
  64. virtual void Dump(CDumpContext& dc) const;
  65. #endif
  66. protected:
  67. HWND m_hWnd; // the window hooked
  68. WNDPROC m_pOldWndProc; // ..and original window proc
  69. CSubclassWnd* m_pNext; // next in chain of hooks for this window
  70. // Override this to handle messages in specific handlers
  71. virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
  72. virtual LRESULT Default(); // call this at the end of handler fns
  73. };
  74. #endif // _SUBCLASSW_H