home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / VFORM.ZIP / Samples / VfbEx / MsgHook.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-10  |  1.7 KB  |  50 lines

  1. ////////////////////////////////////////////////////////////////
  2. // CMsgHook Copyright 1996 Microsoft Systems Journal. 
  3. // If this code works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. //
  6. #ifndef _MSGHOOK_H
  7. #define _MSGHOOK_H
  8.  
  9. //////////////////
  10. // Generic class to hook messages on behalf of a CWnd.
  11. // Once hooked, all messages go to CMsgHook::WindowProc before going
  12. // to the window. Specific subclasses can trap messages and do something.
  13. // To use:
  14. //
  15. // * Derive a class from CMsgHook.
  16. //
  17. // * Override CMsgHook::WindowProc to handle messages. Make sure you call
  18. //   CMsgHook::WindowProc if you don't handle the message, or your window will
  19. //   never get messages. If you write seperate message handlers, you can call
  20. //   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. class CMsgHook : public CObject {
  27. protected:
  28.     DECLARE_DYNAMIC(CMsgHook);
  29.     CWnd*            m_pWndHooked;        // the window hooked
  30.     WNDPROC        m_pOldWndProc;        // ..and original window proc
  31.     CMsgHook*    m_pNext;                // next in chain of hooks for this window
  32.  
  33.     // Override this to handle messages in specific handlers
  34.     virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
  35.     LRESULT Default();                // call this at the end of handler fns
  36.  
  37. public:
  38.     CMsgHook();
  39.     ~CMsgHook();
  40.  
  41.     // Hook a window. Hook(NULL) to unhook (automatic on WM_NCDESTROY)
  42.     BOOL    HookWindow(CWnd* pRealWnd);
  43.     BOOL    IsHooked()            { return m_pWndHooked!=NULL; }
  44.  
  45.     friend LRESULT CALLBACK HookWndProc(HWND, UINT, WPARAM, LPARAM);
  46.     friend class CMsgHookMap;
  47. };
  48.  
  49. #endif
  50.