SubclassWnd.h
上传用户:liudazhe
上传日期:2007-01-02
资源大小:51k
文件大小:2k
源码类别:

菜单

开发平台:

Visual C++

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