Subclass.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /************************************************************************
  2. * 程序名: 精仿QQ主界面
  3. * 制作人: 李克平, 2011年04月11日
  4. * 版本号: 1.0
  5. ************************************************************************/
  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. CSubclassWnd* p = NULL;
  100. if (m_hWnd) {
  101. for (p = theHookMap.Lookup(m_hWnd); p; p=p->m_pNext) {
  102. if (p==this)
  103. break;
  104. }
  105. ASSERT(p); // should have found it!
  106. }
  107. }
  108. void CSubclassWnd::Dump(CDumpContext& dc) const
  109. {
  110. CObject::Dump(dc);
  111. }
  112. #endif
  113. //////////////////
  114. // Subclassed window proc for message hooks. Replaces AfxWndProc (or whatever
  115. // else was there before.)
  116. //
  117. LRESULT CALLBACK
  118. HookWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
  119. {
  120. #ifdef _USRDLL
  121. // If this is a DLL, need to set up MFC state
  122. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  123. #endif
  124. // Set up MFC message state just in case anyone wants it
  125. // This is just like AfxCallWindowProc, but we can't use that because
  126. // a CSubclassWnd is not a CWnd.
  127. //
  128. MSG& curMsg = AfxGetThreadState()->m_lastSentMsg;
  129. MSG oldMsg = curMsg; // save for nesting
  130. curMsg.hwnd = hwnd;
  131. curMsg.message = msg;
  132. curMsg.wParam = wp;
  133. curMsg.lParam = lp;
  134. // Get hook object for this window. Get from hook map
  135. CSubclassWnd* pSubclassWnd = theHookMap.Lookup(hwnd);
  136. ASSERT(pSubclassWnd);
  137. LRESULT lr;
  138. if (msg==WM_NCDESTROY) {
  139. // Window is being destroyed: unhook all hooks (for this window)
  140. // and pass msg to orginal window proc
  141. //
  142. WNDPROC wndproc = pSubclassWnd->m_pOldWndProc;
  143. theHookMap.RemoveAll(hwnd);
  144. lr = ::CallWindowProc(wndproc, hwnd, msg, wp, lp);
  145. } else {
  146. // pass to msg hook
  147. lr = pSubclassWnd->WindowProc(msg, wp, lp);
  148. }
  149. curMsg = oldMsg; // pop state
  150. return lr;
  151. }
  152. ////////////////////////////////////////////////////////////////
  153. // CSubclassWndMap implementation
  154. //
  155. CSubclassWndMap::CSubclassWndMap()
  156. {
  157. }
  158. CSubclassWndMap::~CSubclassWndMap()
  159. {
  160. // This assert bombs when posting WM_QUIT, so I've deleted it.
  161. // ASSERT(IsEmpty()); // all hooks should be removed!
  162. }
  163. //////////////////
  164. // Get the one and only global hook map
  165. //
  166. CSubclassWndMap& CSubclassWndMap::GetHookMap()
  167. {
  168. // By creating theMap here, C++ doesn't instantiate it until/unless
  169. // it's ever used! This is a good trick to use in C++, to
  170. // instantiate/initialize a static object the first time it's used.
  171. //
  172. static CSubclassWndMap theMap;
  173. return theMap;
  174. }
  175. /////////////////
  176. // Add hook to map; i.e., associate hook with window
  177. //
  178. void CSubclassWndMap::Add(HWND hwnd, CSubclassWnd* pSubclassWnd)
  179. {
  180. ASSERT(hwnd && ::IsWindow(hwnd));
  181. // Add to front of list
  182. pSubclassWnd->m_pNext = Lookup(hwnd);
  183. SetAt(hwnd, pSubclassWnd);
  184. if (pSubclassWnd->m_pNext==NULL) {
  185. // If this is the first hook added, subclass the window
  186. pSubclassWnd->m_pOldWndProc =
  187. (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)HookWndProc);
  188. } else {
  189. // just copy wndproc from next hook
  190. pSubclassWnd->m_pOldWndProc = pSubclassWnd->m_pNext->m_pOldWndProc;
  191. }
  192. ASSERT(pSubclassWnd->m_pOldWndProc);
  193. }
  194. //////////////////
  195. // Remove hook from map
  196. //
  197. void CSubclassWndMap::Remove(CSubclassWnd* pUnHook)
  198. {
  199. HWND hwnd = pUnHook->m_hWnd;
  200. ASSERT(hwnd && ::IsWindow(hwnd));
  201. CSubclassWnd* pHook = Lookup(hwnd);
  202. ASSERT(pHook);
  203. if (pHook==pUnHook) {
  204. // hook to remove is the one in the hash table: replace w/next
  205. if (pHook->m_pNext)
  206. SetAt(hwnd, pHook->m_pNext);
  207. else {
  208. // This is the last hook for this window: restore wnd proc
  209. RemoveKey(hwnd);
  210. SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)pHook->m_pOldWndProc);
  211. }
  212. } else {
  213. // Hook to remove is in the middle: just remove from linked list
  214. while (pHook->m_pNext!=pUnHook)
  215. pHook = pHook->m_pNext;
  216. ASSERT(pHook && pHook->m_pNext==pUnHook);
  217. pHook->m_pNext = pUnHook->m_pNext;
  218. }
  219. }
  220. //////////////////
  221. // Remove all the hooks for a window
  222. //
  223. void CSubclassWndMap::RemoveAll(HWND hwnd)
  224. {
  225. CSubclassWnd* pSubclassWnd;
  226. while ((pSubclassWnd = Lookup(hwnd))!=NULL)
  227. pSubclassWnd->HookWindow((HWND)NULL); // (unhook)
  228. }
  229. /////////////////
  230. // Find first hook associate with window
  231. //
  232. CSubclassWnd* CSubclassWndMap::Lookup(HWND hwnd)
  233. {
  234. CSubclassWnd* pFound = NULL;
  235. if (!CMapPtrToPtr::Lookup(hwnd, (void*&)pFound))
  236. return NULL;
  237. ASSERT_KINDOF(CSubclassWnd, pFound);
  238. return pFound;
  239. }