MenuBarXP.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // MenuBarXP.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MenuBarXP.h"
  5. #include "MenuXP.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMenuBarXP
  13. CMenuBarXP::CMenuBarXP()
  14. {
  15. m_pMenu = NULL;
  16. m_bTrack = FALSE;
  17. m_nItemCount = 0;
  18. m_nPressed = 0;
  19. m_ptMouse.x = 0;
  20. m_ptMouse.y = 0;
  21. }
  22. CMenuBarXP::~CMenuBarXP()
  23. {
  24. if (m_pMenu)
  25. {
  26. delete m_pMenu;
  27. }
  28. }
  29. BEGIN_MESSAGE_MAP(CMenuBarXP, CToolBarCtrl)
  30. //{{AFX_MSG_MAP(CMenuBarXP)
  31. ON_WM_LBUTTONDOWN()
  32. ON_WM_MEASUREITEM()
  33. ON_WM_MENUCHAR()
  34. ON_WM_INITMENUPOPUP()
  35. //}}AFX_MSG_MAP
  36. ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, CToolBarXP::OnCustomDraw)
  37. ON_MESSAGE(MB_POPUPMENU, OnPopupMenu)
  38. ON_WM_EXITMENULOOP()
  39. ON_WM_ENTERMENULOOP()
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CMenuBarXP message handlers
  43. /////////////////////////////////////////////////////////////////////////////
  44. //Load from a menu, can be a CMenuXP instance or just a CMenu
  45. //the pMenu object must be constructed on the heap, it will
  46. //automatically be destoyed when the MenuBar being destroyed
  47. BOOL CMenuBarXP::LoadMenu(CMenu *pMenu)
  48. {
  49. if (!m_hWnd)
  50. return FALSE;
  51. if (!pMenu)
  52. return FALSE;
  53. if (m_pMenu)
  54. delete m_pMenu;
  55. m_pMenu = pMenu;
  56. TRACE(_T("MainMenu Handle: 0x%x\n"), m_pMenu->GetSafeHmenu());
  57. TBBUTTON tbb;
  58. int i,nStr;
  59. BOOL bRet = FALSE;
  60. TCHAR szText[_MAX_PATH];
  61. memset(&tbb, 0, sizeof(TBBUTTON));
  62. tbb.fsState = TBSTATE_ENABLED;
  63. tbb.fsStyle = TBSTYLE_BUTTON|TBSTYLE_AUTOSIZE;
  64. m_nItemCount = m_pMenu->GetMenuItemCount();//Count the main menu items
  65. SetBitmapSize(CSize(0, 0));//no icons for the main menu
  66. m_ilIcons.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1);
  67. for(i=0; i<m_nItemCount; i++)
  68. {
  69. memset(szText, 0, _MAX_PATH);
  70. MENUITEMINFO info;
  71. memset(&info, 0, sizeof(MENUITEMINFO));
  72. info.cbSize = sizeof(MENUITEMINFO);
  73. info.fMask = MIIM_DATA | MIIM_TYPE;
  74. m_pMenu->GetMenuItemInfo(i, &info, TRUE);
  75. CMenuXPItem *pData = (CMenuXPItem *)info.dwItemData;
  76. if (pData && (info.fType & MFT_OWNERDRAW))
  77. {
  78. lstrcpy(szText, pData->m_strText);
  79. if (pData->m_hIcon)
  80. {
  81. tbb.iBitmap = m_ilIcons.Add(pData->m_hIcon);
  82. }
  83. else
  84. tbb.iBitmap = -1;
  85. }
  86. else
  87. {
  88. m_pMenu->GetMenuString(i, szText, 80, MF_BYPOSITION);
  89. }
  90. nStr = AddStrings(szText);
  91. tbb.dwData = NULL;
  92. tbb.iString = nStr;
  93. tbb.idCommand = i;
  94. bRet = AddButtons(1, &tbb);
  95. if(!bRet)
  96. return FALSE;
  97. }
  98. if (m_ilIcons.GetImageCount()>0)
  99. SetImageList(&m_ilIcons);
  100. return bRet;
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. CMenuBarXP* g_pMenuBar = NULL;
  104. HHOOK g_hMsgHook = NULL;
  105. ////////////////////////////////////////////////////////////////////////////////
  106. // The hook, used to process message when menu is visible
  107. LRESULT CMenuBarXP::MenuInputFilter(int nCode, WPARAM wParam, LPARAM lParam)
  108. {
  109. MSG* pMsg = (MSG*)lParam;
  110. if(!g_pMenuBar || nCode!=MSGF_MENU)
  111. return CallNextHookEx(g_hMsgHook,nCode,wParam,lParam);
  112. if(g_pMenuBar->OnMenuInput(pMsg))
  113. return TRUE;
  114. else
  115. return CallNextHookEx(g_hMsgHook,nCode,wParam,lParam);
  116. }
  117. //////////////////////////////////////////////////////////////////////////
  118. // Show popupmenu
  119. void CMenuBarXP::TrackPopup()
  120. {
  121. CMenu *pSubMenu = m_pMenu->GetSubMenu(m_nPressed);
  122. if(pSubMenu == NULL)return;
  123. TRACE(_T("Tracking Menu handle: 0x%x\n"), pSubMenu->GetSafeHmenu());
  124. m_bTrack = TRUE;
  125. PressButton(m_nPressed,TRUE);
  126. //Get the rect of the button
  127. CRect rc;
  128. GetItemRect(m_nPressed,&rc);
  129. ClientToScreen(&rc);
  130. //get the in-screen part of the button
  131. CRect rcScreen;
  132. GetDesktopWindow()->GetWindowRect(rcScreen);
  133. rc.IntersectRect(rc, rcScreen);
  134. TPMPARAMS tpm;
  135. tpm.cbSize = sizeof(tpm);
  136. tpm.rcExclude = rc;
  137. //Install the hook
  138. g_pMenuBar = this;
  139. g_hMsgHook = SetWindowsHookEx(WH_MSGFILTER,
  140. MenuInputFilter, NULL, GetCurrentThreadId());
  141. //Show menu
  142. TrackPopupMenuEx(pSubMenu->GetSafeHmenu(),
  143. TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_VERTICAL,
  144. rc.left, rc.bottom, m_hWnd, &tpm);
  145. PressButton(m_nPressed,FALSE);
  146. UnhookWindowsHookEx(g_hMsgHook);//Uninstall the hook
  147. g_hMsgHook = NULL;
  148. g_pMenuBar = NULL;
  149. }
  150. /////////////////////////////////////////////////////////////////////////////////
  151. // Actually process messages, called by the hook
  152. BOOL CMenuBarXP::OnMenuInput(MSG* pMsg)
  153. {
  154. BOOL bResult = FALSE;
  155. switch(pMsg->message)
  156. {
  157. case WM_MOUSEMOVE:
  158. {
  159. POINT pt;
  160. pt.x = LOWORD(pMsg->lParam);
  161. pt.y = HIWORD(pMsg->lParam);
  162. ScreenToClient(&pt);
  163. if(m_ptMouse.x == pt.x && m_ptMouse.y == pt.y)
  164. return TRUE;
  165. m_ptMouse.x = pt.x;
  166. m_ptMouse.y = pt.y;
  167. int nTest = HitTest(&pt);
  168. if(nTest>=0 && nTest<m_nItemCount && nTest != m_nPressed)
  169. {
  170. PressButton(m_nPressed,FALSE);
  171. SendMessage(WM_CANCELMODE,0,0);
  172. m_nPressed = nTest;
  173. PostMessage(MB_POPUPMENU,0,0);
  174. bResult = TRUE;
  175. }
  176. }
  177. break;
  178. case WM_LBUTTONDOWN:
  179. {
  180. POINT pt;
  181. pt.x = LOWORD(pMsg->lParam);
  182. pt.y = HIWORD(pMsg->lParam);
  183. ScreenToClient(&pt);
  184. int nTest = HitTest(&pt);
  185. if(nTest<0)
  186. m_bTrack = FALSE;
  187. else if(nTest == m_nPressed)
  188. {
  189. m_bTrack = FALSE;
  190. PostMessage(WM_CANCELMODE,0,0);
  191. bResult = TRUE;
  192. }
  193. }
  194. break;
  195. case WM_KEYDOWN:
  196. {
  197. TCHAR vkey = pMsg->wParam;
  198. if(vkey == VK_LEFT)
  199. {
  200. PressButton(m_nPressed,FALSE);
  201. m_nPressed --;
  202. PostMessage(WM_CANCELMODE,0,0);
  203. PostMessage(MB_POPUPMENU,0,0);
  204. bResult = TRUE;
  205. }
  206. else if(vkey == VK_RIGHT)
  207. {
  208. PressButton(m_nPressed,FALSE);
  209. m_nPressed ++;
  210. PostMessage(WM_CANCELMODE,0,0);
  211. PostMessage(MB_POPUPMENU,0,0);
  212. bResult = TRUE;
  213. }
  214. else if (vkey == VK_ESCAPE)
  215. {
  216. PostMessage(WM_CANCELMODE,0,0);
  217. m_bTrack = FALSE;
  218. bResult = TRUE;
  219. }
  220. }
  221. break;
  222. case WM_MENUSELECT:
  223. GetOwner()->SendMessage(WM_MENUSELECT, pMsg->wParam, pMsg->lParam);
  224. bResult = TRUE;
  225. break;
  226. default:
  227. break;
  228. }
  229. return bResult;
  230. }
  231. void CMenuBarXP::OnLButtonDown(UINT nFlags, CPoint point)
  232. {
  233. // TODO: Add your message handler code here and/or call default
  234. int nTest = HitTest(&point);
  235. if(nTest<0 || nTest>=m_nItemCount)
  236. return;
  237. m_nPressed = nTest;
  238. TrackPopup();
  239. }
  240. LRESULT CMenuBarXP::OnPopupMenu(WPARAM wParam, LPARAM lParam)
  241. {
  242. if(m_nPressed<0)
  243. m_nPressed = m_nItemCount - 1;
  244. if(m_nPressed>=m_nItemCount)
  245. m_nPressed = 0;
  246. TrackPopup();
  247. return 0;
  248. }
  249. void CMenuBarXP::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  250. {
  251. // TODO: Add your message handler code here and/or call default
  252. if (nIDCtl == 0 && m_pMenu)
  253. {
  254. m_pMenu->MeasureItem(lpMeasureItemStruct);
  255. }
  256. }
  257. void CMenuBarXP::OnExitMenuLoop(BOOL bIsTrackPopupMenu)
  258. {
  259. GetOwner()->SendMessage(WM_EXITMENULOOP, (WPARAM)bIsTrackPopupMenu);
  260. }
  261. void CMenuBarXP::OnEnterMenuLoop(BOOL bIsTrackPopupMenu)
  262. {
  263. GetOwner()->SendMessage(WM_ENTERMENULOOP, (WPARAM)bIsTrackPopupMenu);
  264. }
  265. LRESULT CMenuBarXP::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu)
  266. {
  267. return CMenuXP::OnMenuChar(nChar, nFlags, pMenu);
  268. }
  269. //If there's a main menuitem with a mnemonic key nChar, then open it
  270. BOOL CMenuBarXP::OpenMenu(UINT nChar)
  271. {
  272. int nCount = (int)m_pMenu->GetMenuItemCount();
  273. TCHAR szText[80];
  274. for(int i=0; i<m_nItemCount; i++)
  275. {
  276. MENUITEMINFO info;
  277. memset(&info, 0, sizeof(MENUITEMINFO));
  278. info.cbSize = sizeof(MENUITEMINFO);
  279. info.fMask = MIIM_DATA | MIIM_TYPE;
  280. m_pMenu->GetMenuItemInfo(i, &info, TRUE);
  281. CMenuXPItem *pData = (CMenuXPItem *)info.dwItemData;
  282. if (pData && (info.fType & MFT_OWNERDRAW))
  283. {
  284. lstrcpy(szText, pData->m_strText);
  285. }
  286. else
  287. {
  288. m_pMenu->GetMenuString(i, szText, 80, MF_BYPOSITION);
  289. }
  290. CString text = szText;
  291. int iAmpersand = text.Find('&');
  292. if (iAmpersand >=0 && toupper(nChar)==toupper(text[iAmpersand+1]))
  293. {
  294. m_nPressed = i;
  295. PostMessage(MB_POPUPMENU,0,0);
  296. return TRUE;
  297. }
  298. }
  299. return FALSE;
  300. }
  301. void CMenuBarXP::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
  302. {
  303. CToolBarCtrl::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
  304. // TODO: Add your message handler code here
  305. GetOwner()->SendMessage(WM_INITMENUPOPUP, (WPARAM)pPopupMenu->GetSafeHmenu(), MAKELONG(nIndex, bSysMenu));
  306. }