MsgHook.cpp
上传用户:zhouyunkk
上传日期:2022-07-16
资源大小:98k
文件大小:5k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MsgHook.h"
  3. HHOOK CMsgHook::m_hHook = NULL;
  4. int CMsgHook::m_nInstances = 0;
  5. CMap<HWND, HWND&, CMsgHook*, CMsgHook*&> CMsgHook::m_mapHookedWindows;  // map of windows to hooks
  6. CMsgHook::CMsgHook(void)
  7. : m_bDropDown(false)
  8. ,m_hWnd(NULL)
  9. ,m_hParent(NULL)
  10. ,m_hNotify(NULL)
  11. {
  12. if(m_nInstances == 0)
  13. {
  14. m_hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndRetProc, NULL, GetCurrentThreadId());
  15. ASSERT(m_hHook);
  16. }
  17. m_nInstances++;
  18. }
  19. CMsgHook::~CMsgHook(void)
  20. {
  21. m_nInstances--;
  22. if(m_nInstances == 0)
  23. {
  24. UnhookWindowsHookEx(m_hHook);
  25. }
  26. }
  27. //LRESULT CALLBACK CMsgHook::KeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
  28. //{
  29. // MSG msg;
  30. // PeekMessage(&msg, NULL, 0, 0, 0);
  31. // if(msg.message == WM_KEYDOWN && (wParam == VK_ESCAPE || wParam == VK_RETURN))
  32. // {
  33. // CMsgHook *pHook = NULL;
  34. // HWND hWnd = (HWND)msg.hwnd;
  35. // while(hWnd)
  36. // {
  37. // if(m_mapHookedWindows.Lookup(hWnd, pHook))
  38. // {
  39. // SendMessage(pHook->m_hNotify ? pHook->m_hNotify : pHook->m_hParent, UM_HIDEEDITOR, wParam == VK_RETURN, 0);
  40. // break;
  41. // }
  42. // hWnd = GetParent(hWnd);
  43. // }
  44. // }
  45. // return CallNextHookEx(m_hHook, nCode, wParam, lParam);
  46. //}
  47. LRESULT CALLBACK CMsgHook::CallWndRetProc( int nCode, WPARAM wParam, LPARAM lParam )
  48. {
  49. CWPRETSTRUCT *pMsg = (CWPRETSTRUCT *)lParam;
  50. if(pMsg)
  51. {
  52. CMsgHook *pHook = NULL;
  53. switch(pMsg->message)
  54. {
  55. case WM_KILLFOCUS:
  56. {
  57. HWND hWnd = (HWND)pMsg->hwnd;
  58. while(hWnd)
  59. {
  60. if(m_mapHookedWindows.Lookup(hWnd, pHook))
  61. {
  62. if(!pHook->m_bDropDown)
  63. {
  64. hWnd = (HWND)pMsg->wParam;
  65. while(hWnd && hWnd != pHook->m_hWnd)
  66. {
  67. hWnd = GetParent(hWnd);
  68. }
  69. if(!hWnd)
  70. {
  71. SendMessage(pHook->m_hNotify ? pHook->m_hNotify : pHook->m_hParent, UM_HIDEEDITOR, TRUE, 0);
  72. break;
  73. }
  74. }
  75. break;
  76. }
  77. hWnd = GetParent(hWnd);
  78. }
  79. }
  80. break;
  81. case WM_NOTIFY:
  82. {
  83. LPNMHDR pnmh = (LPNMHDR)pMsg->lParam;
  84. if(pnmh->code == DTN_DROPDOWN || pnmh->code == DTN_CLOSEUP)
  85. {
  86. HWND hWnd = (HWND)pnmh->hwndFrom;
  87. while(hWnd)
  88. {
  89. if(m_mapHookedWindows.Lookup(hWnd, pHook))
  90. {
  91. pHook->m_bDropDown = (DTN_DROPDOWN == pnmh->code);
  92. break;
  93. }
  94. hWnd = GetParent(hWnd);
  95. }
  96. }
  97. }
  98. break;
  99. case WM_COMMAND:
  100. {
  101. WORD code = HIWORD(pMsg->wParam);
  102. if(code == CBN_DROPDOWN || code == CBN_CLOSEUP)
  103. {
  104. HWND hWnd = (HWND)pMsg->lParam;
  105. while(hWnd)
  106. {
  107. if(m_mapHookedWindows.Lookup(hWnd, pHook))
  108. {
  109. pHook->m_bDropDown = (CBN_DROPDOWN == code);
  110. break;
  111. }
  112. hWnd = GetParent(hWnd);
  113. }
  114. }
  115. }
  116. break;
  117. case WM_KEYDOWN:
  118. {
  119. if(pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN)
  120. {
  121. HWND hWnd = (HWND)pMsg->hwnd;
  122. while(hWnd)
  123. {
  124. if(m_mapHookedWindows.Lookup(hWnd, pHook))
  125. {
  126. SendMessage(pHook->m_hNotify ? pHook->m_hNotify : pHook->m_hParent, UM_HIDEEDITOR, pMsg->wParam == VK_RETURN, 0);
  127. break;
  128. }
  129. hWnd = GetParent(hWnd);
  130. }
  131. }
  132. }
  133. break;
  134. case WM_DESTROY:
  135. {
  136. if(m_mapHookedWindows.Lookup(pMsg->hwnd, pHook))
  137. {
  138. pHook->Detach();
  139. }
  140. }
  141. break;
  142. case WM_LBUTTONDOWN:
  143. case WM_RBUTTONDOWN:
  144. case WM_MBUTTONDOWN:
  145. case WM_HSCROLL:
  146. case WM_VSCROLL:
  147. case WM_MENUSELECT:
  148. case WM_MOUSEWHEEL:
  149. {
  150. HWND hWnd = (HWND)pMsg->hwnd;
  151. while(hWnd && !m_mapHookedWindows.Lookup(hWnd, pHook))
  152. {
  153. hWnd = GetParent(hWnd);
  154. }
  155. if(!pHook)
  156. {
  157. POSITION pos = m_mapHookedWindows.GetStartPosition();
  158. HWND    hWnd;
  159. while (pos != NULL)
  160. {
  161. m_mapHookedWindows.GetNextAssoc( pos, hWnd, pHook );
  162. if(IsWindow(hWnd) && !pHook->m_bDropDown)
  163. {
  164. bool dropDown = SendMessage(hWnd, CB_GETDROPPEDSTATE, 0, 0) || DateTime_GetMonthCal(hWnd);
  165. if(!dropDown) SendMessage(pHook->m_hNotify ? pHook->m_hNotify : pHook->m_hParent, UM_HIDEEDITOR, TRUE, 0);
  166. }
  167. }
  168. }
  169. }
  170. break;
  171. }
  172. }
  173. return CallNextHookEx(m_hHook, nCode, wParam, lParam);
  174. }
  175. BOOL CMsgHook::Attach(HWND hWnd, HWND hParent, HWND hNotify)
  176. {
  177. if(IsWindow(hWnd) && IsWindow(hParent))
  178. {
  179. Detach();
  180. m_hWnd = hWnd;
  181. m_hParent = hParent;
  182. m_hNotify = hNotify;
  183. m_mapHookedWindows[hWnd] = this;
  184. m_bDropDown = FALSE;
  185. return TRUE;
  186. }
  187. return FALSE;
  188. }
  189. BOOL CMsgHook::Detach(void)
  190. {
  191. if(m_mapHookedWindows.RemoveKey(m_hWnd))
  192. {
  193. m_hWnd = NULL;
  194. m_hParent = NULL;
  195. m_hNotify = NULL;
  196. m_bDropDown = FALSE;
  197. return TRUE;
  198. }
  199. return FALSE;
  200. }