Subclass.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ////////////////////////////////////////////////////////////////
  2. // VCKBASE -- August 2000
  3. // Compiles with Visual C++ 6.0, runs on Windows 98 and probably NT too.
  4. //
  5. // CSubclassWnd is a generic class for hooking another window's messages.
  6. #include "StdAfx.h"
  7. #include "Subclass.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. //////////////////
  14. // The message hook map is derived from CMapPtrToPtr, which associates
  15. // a pointer with another pointer. It maps an HWND to a CSubclassWnd, like
  16. // the way MFC's internal maps map HWND's to CWnd's. The first CSubclassWnd
  17. // attached to a window is stored in the map; all other CSubclassWnd's for that
  18. // window are then chained via CSubclassWnd::m_pNext.
  19. //
  20. class CSubclassWndMap : private CMapPtrToPtr {
  21. public:
  22. CSubclassWndMap();
  23. ~CSubclassWndMap();
  24. static CSubclassWndMap& GetHookMap();
  25. void Add(HWND hwnd, CSubclassWnd* pSubclassWnd);
  26. void Remove(CSubclassWnd* pSubclassWnd);
  27. void RemoveAll(HWND hwnd);
  28. CSubclassWnd* Lookup(HWND hwnd);
  29. };
  30. // This trick is used so the hook map isn't
  31. // instantiated until someone actually requests it.
  32. //
  33. #define theHookMap (CSubclassWndMap::GetHookMap())
  34. IMPLEMENT_DYNAMIC(CSubclassWnd, CWnd);
  35. CSubclassWnd::CSubclassWnd()
  36. {
  37. m_pNext = NULL;
  38. m_pOldWndProc = NULL;
  39. m_hWnd = NULL;
  40. }
  41. CSubclassWnd::~CSubclassWnd()
  42. {
  43. if (m_hWnd)
  44. HookWindow((HWND)NULL); // unhook window
  45. }
  46. //////////////////
  47. // Hook a window.
  48. // This installs a new window proc that directs messages to the CSubclassWnd.
  49. // pWnd=NULL to remove.
  50. //
  51. BOOL CSubclassWnd::HookWindow(HWND hwnd)
  52. {
  53. ASSERT_VALID(this);
  54. if (hwnd) {
  55. // Hook the window
  56. ASSERT(m_hWnd==NULL);
  57. ASSERT(::IsWindow(hwnd));
  58. theHookMap.Add(hwnd, this); // Add to map of hooks
  59. } else if (m_hWnd) {
  60. // Unhook the window
  61. theHookMap.Remove(this); // Remove from map
  62. m_pOldWndProc = NULL;
  63. }
  64. m_hWnd = hwnd;
  65. return TRUE;
  66. }
  67. //////////////////
  68. // Window proc-like virtual function which specific CSubclassWnds will
  69. // override to do stuff. Default passes the message to the next hook;
  70. // the last hook passes the message to the original window.
  71. // You MUST call this at the end of your WindowProc if you want the real
  72. // window to get the message. This is just like CWnd::WindowProc, except that
  73. // a CSubclassWnd is not a window.
  74. //
  75. LRESULT CSubclassWnd::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
  76. {
  77. // ASSERT_VALID(this); // removed for speed
  78. ASSERT(m_pOldWndProc);
  79. return m_pNext ? m_pNext->WindowProc(msg, wp, lp) :
  80. ::CallWindowProc(m_pOldWndProc, m_hWnd, msg, wp, lp);
  81. }
  82. //////////////////
  83. // Like calling base class WindowProc, but with no args, so individual
  84. // message handlers can do the default thing. Like CWnd::Default
  85. //
  86. LRESULT CSubclassWnd::Default()
  87. {
  88. // MFC stores current MSG in thread state
  89. MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
  90. // Note: must explicitly call CSubclassWnd::WindowProc to avoid infinte
  91. // recursion on virtual function
  92. return CSubclassWnd::WindowProc(curMsg.message, curMsg.wParam, curMsg.lParam);
  93. }
  94. #ifdef _DEBUG
  95. void CSubclassWnd::AssertValid() const
  96. {
  97. CObject::AssertValid();
  98. ASSERT(m_hWnd==NULL || ::IsWindow(m_hWnd));
  99. if (m_hWnd)
  100. {
  101. CSubclassWnd* p = NULL;
  102. for ( p = theHookMap.Lookup(m_hWnd); p; p=p->m_pNext) {
  103. if (p==this)
  104. break;
  105. }
  106. ASSERT(p); // should have found it!
  107. }
  108. }
  109. void CSubclassWnd::Dump(CDumpContext& dc) const
  110. {
  111. CObject::Dump(dc);
  112. }
  113. #endif
  114. //////////////////
  115. // Subclassed window proc for message hooks. Replaces AfxWndProc (or whatever
  116. // else was there before.)
  117. //
  118. LRESULT CALLBACK
  119. HookWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
  120. {
  121. #ifdef _USRDLL
  122. // If this is a DLL, need to set up MFC state
  123. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  124. #endif
  125. // Set up MFC message state just in case anyone wants it
  126. // This is just like AfxCallWindowProc, but we can't use that because
  127. // a CSubclassWnd is not a CWnd.
  128. //
  129. MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
  130. MSG oldMsg = curMsg; // save for nesting
  131. curMsg.hwnd = hwnd;
  132. curMsg.message = msg;
  133. curMsg.wParam = wp;
  134. curMsg.lParam = lp;
  135. // Get hook object for this window. Get from hook map
  136. CSubclassWnd* pSubclassWnd = theHookMap.Lookup(hwnd);
  137. ASSERT(pSubclassWnd);
  138. LRESULT lr;
  139. if (msg==WM_NCDESTROY) {
  140. // Window is being destroyed: unhook all hooks (for this window)
  141. // and pass msg to orginal window proc
  142. //
  143. WNDPROC wndproc = pSubclassWnd->m_pOldWndProc;
  144. theHookMap.RemoveAll(hwnd);
  145. lr = ::CallWindowProc(wndproc, hwnd, msg, wp, lp);
  146. } else {
  147. // pass to msg hook
  148. lr = pSubclassWnd->WindowProc(msg, wp, lp);
  149. }
  150. curMsg = oldMsg; // pop state
  151. return lr;
  152. }
  153. ////////////////////////////////////////////////////////////////
  154. // CSubclassWndMap implementation
  155. //
  156. CSubclassWndMap::CSubclassWndMap()
  157. {
  158. }
  159. CSubclassWndMap::~CSubclassWndMap()
  160. {
  161. // This assert bombs when posting WM_QUIT, so I've deleted it.
  162. // ASSERT(IsEmpty()); // all hooks should be removed!
  163. }
  164. //////////////////
  165. // Get the one and only global hook map
  166. //
  167. CSubclassWndMap& CSubclassWndMap::GetHookMap()
  168. {
  169. // By creating theMap here, C++ doesn't instantiate it until/unless
  170. // it's ever used! This is a good trick to use in C++, to
  171. // instantiate/initialize a static object the first time it's used.
  172. //
  173. static CSubclassWndMap theMap;
  174. return theMap;
  175. }
  176. /////////////////
  177. // Add hook to map; i.e., associate hook with window
  178. //
  179. void CSubclassWndMap::Add(HWND hwnd, CSubclassWnd* pSubclassWnd)
  180. {
  181. ASSERT(hwnd && ::IsWindow(hwnd));
  182. // Add to front of list
  183. pSubclassWnd->m_pNext = Lookup(hwnd);
  184. SetAt(hwnd, pSubclassWnd);
  185. if (pSubclassWnd->m_pNext==NULL) {
  186. // If this is the first hook added, subclass the window
  187. pSubclassWnd->m_pOldWndProc =
  188. (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)HookWndProc);
  189. } else {
  190. // just copy wndproc from next hook
  191. pSubclassWnd->m_pOldWndProc = pSubclassWnd->m_pNext->m_pOldWndProc;
  192. }
  193. ASSERT(pSubclassWnd->m_pOldWndProc);
  194. }
  195. //////////////////
  196. // Remove hook from map
  197. //
  198. void CSubclassWndMap::Remove(CSubclassWnd* pUnHook)
  199. {
  200. HWND hwnd = pUnHook->m_hWnd;
  201. ASSERT(hwnd && ::IsWindow(hwnd));
  202. CSubclassWnd* pHook = Lookup(hwnd);
  203. ASSERT(pHook);
  204. if (pHook==pUnHook) {
  205. // hook to remove is the one in the hash table: replace w/next
  206. if (pHook->m_pNext)
  207. SetAt(hwnd, pHook->m_pNext);
  208. else {
  209. // This is the last hook for this window: restore wnd proc
  210. RemoveKey(hwnd);
  211. SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)pHook->m_pOldWndProc);
  212. }
  213. } else {
  214. // Hook to remove is in the middle: just remove from linked list
  215. while (pHook->m_pNext!=pUnHook)
  216. pHook = pHook->m_pNext;
  217. ASSERT(pHook && pHook->m_pNext==pUnHook);
  218. pHook->m_pNext = pUnHook->m_pNext;
  219. }
  220. }
  221. //////////////////
  222. // Remove all the hooks for a window
  223. //
  224. void CSubclassWndMap::RemoveAll(HWND hwnd)
  225. {
  226. CSubclassWnd* pSubclassWnd;
  227. while ((pSubclassWnd = Lookup(hwnd))!=NULL)
  228. pSubclassWnd->HookWindow((HWND)NULL); // (unhook)
  229. }
  230. /////////////////
  231. // Find first hook associate with window
  232. //
  233. CSubclassWnd* CSubclassWndMap::Lookup(HWND hwnd)
  234. {
  235. CSubclassWnd* pFound = NULL;
  236. if (!CMapPtrToPtr::Lookup(hwnd, (void*&)pFound))
  237. return NULL;
  238. ASSERT_KINDOF(CSubclassWnd, pFound);
  239. return pFound;
  240. }