MFC自制菜单(既绘制菜单的外观)

来源:互联网 发布:linux shell执行exe 编辑:程序博客网 时间:2024/06/05 18:38

建立自制菜单,按如下三步完成:

第一:为菜单项添加MF_OWNERDRAW样式

第二:实现WM_MEASUREITEM消息处理(目的:设置菜单项的宽度和高度)

第三:实现WM_DRAWITEM消息处理(目的:绘制菜单项)


提示:在stdafx.h文件的#include <afxwin.h> 前面添加如下宏(目的:使用系统预定义的位图)

#define OEMRESOURCE

本文要绘制菜单的菜单ID如下:

#define ID_COLOR_RED                    32774#define ID_COLOR_GREEN                  32775#define ID_COLOR_BLUE                   32776

涉及到的数据成员如下:

UINT m_uCurrentColor;COLORREF m_clrMenuBK[3];

初始化数据成员以及修改菜单样式为MF_OWNERDRAW

m_uCurrentColor = ID_COLOR_RED;m_clrMenuBK[0] = RGB(255,0,0);m_clrMenuBK[1] = RGB(0,255,0);m_clrMenuBK[2] = RGB(0,0,255);CMenu *pMenu = GetMenu()->GetSubMenu(1);for (UINT i = ID_COLOR_RED ; i <= ID_COLOR_BLUE ; ++i){pMenu->ModifyMenu(i,MF_BYCOMMAND|MF_OWNERDRAW,i);}

消息映射宏如下:

ON_WM_MEASUREITEM()ON_WM_DRAWITEM()ON_COMMAND_RANGE(ID_COLOR_RED,ID_COLOR_BLUE,OnMenuHandler)ON_UPDATE_COMMAND_UI_RANGE(ID_COLOR_RED,ID_COLOR_BLUE,OnUpdateColorUI)ON_WM_INITMENUPOPUP()

实现菜单的事件处理函数以及菜单的状态更新处理函数

void CmenuTestDlg::OnMenuHandler( UINT uID ){m_uCurrentColor = uID;}void CmenuTestDlg::OnUpdateColorUI( CCmdUI *pCmdUI ){pCmdUI->SetCheck(m_uCurrentColor == pCmdUI->m_nID);}void CmenuTestDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu){CDialogEx::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);ASSERT(pPopupMenu != NULL);      // Check the enabled state of various menu items.      CCmdUI state;      state.m_pMenu = pPopupMenu;      ASSERT(state.m_pOther == NULL);      ASSERT(state.m_pParentMenu == NULL);      // Determine if menu is popup in top-level menu and set m_pOther to      // it if so (m_pParentMenu == NULL indicates that it is secondary popup)    .      HMENU hParentMenu;      if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)      state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.      else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)      {      CWnd* pParent = this;      // Child windows don't have menus--need to go to the top!      if (pParent != NULL &&      (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)      {      int nIndexMax = ::GetMenuItemCount(hParentMenu);      for (int nIndex = 0; nIndex < nIndexMax; nIndex++)      {      if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)      {      // When popup is found, m_pParentMenu is containing menu.      state.m_pParentMenu = CMenu::FromHandle(hParentMenu);      break;      }      }      }      }      state.m_nIndexMax = pPopupMenu->GetMenuItemCount();      for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;      state.m_nIndex++)      {      state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);      if (state.m_nID == 0)      continue; // Menu separator or invalid cmd - ignore it.      ASSERT(state.m_pOther == NULL);      ASSERT(state.m_pMenu != NULL);      if (state.m_nID == (UINT)-1)      {      // Possibly a popup menu, route to first item of that popup.      state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);      if (state.m_pSubMenu == NULL ||      (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||      state.m_nID == (UINT)-1)      {      continue;       // First item of popup can't be routed to.      }      state.DoUpdate(this, TRUE);   // Popups are never auto disabled.      }      else      {      // Normal menu item.      // Auto enable/disable if frame window has m_bAutoMenuEnable      // set and command is _not_ a system command.      state.m_pSubMenu = NULL;      state.DoUpdate(this, FALSE);      }      // Adjust for menu deletions and additions.      UINT nCount = pPopupMenu->GetMenuItemCount();      if (nCount < state.m_nIndexMax)      {      state.m_nIndex -= (state.m_nIndexMax - nCount);      while (state.m_nIndex < nCount &&      pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)      {      state.m_nIndex++;      }      }      state.m_nIndexMax = nCount;      } }

实现菜单WM_MEASUREITEM和WM_DRAWITEM消息处理

#define IMAGE_TOP_INTERVAL 2#define IMAGE_LEFT_INTERVAL 5void CmenuTestDlg::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct){int nWidth = ::GetSystemMetrics(SM_CYMENU) * 4;int nHeight = ::GetSystemMetrics(SM_CYMENU);lpMeasureItemStruct->itemWidth = nWidth;lpMeasureItemStruct->itemHeight = nHeight;CDialogEx::OnMeasureItem(nIDCtl, lpMeasureItemStruct);}void CmenuTestDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct){CBitmap bmp;BOOL bResult = bmp.LoadOEMBitmap(OBM_CHECK);BITMAP bmpInfo;bmp.GetBitmap(&bmpInfo);CDC dc;dc.Attach(lpDrawItemStruct->hDC);CBrush br;// 绘制鼠标移上去的状态br.CreateSolidBrush(::GetSysColor(lpDrawItemStruct->itemState & ODS_SELECTED ? COLOR_HIGHLIGHT : COLOR_MENU));dc.FrameRect(&lpDrawItemStruct->rcItem,&br);br.DeleteObject();// 绘制鼠标单击菜单项后的状态(即绘制菜单项前面选择的图标)if (lpDrawItemStruct->itemState & ODS_CHECKED){CDC memDC;memDC.CreateCompatibleDC(&dc);HGDIOBJ hOldBmp = memDC.SelectObject(bmp.m_hObject);{dc.BitBlt(lpDrawItemStruct->rcItem.left+IMAGE_LEFT_INTERVAL,lpDrawItemStruct->rcItem.top+IMAGE_TOP_INTERVAL,bmpInfo.bmWidth,bmpInfo.bmHeight,&memDC,0,0,SRCCOPY);}memDC.SelectObject(hOldBmp);memDC.DeleteDC();}// 绘制菜单项后面的背景色br.CreateSolidBrush(m_clrMenuBK[lpDrawItemStruct->itemID-ID_COLOR_RED]);CRect rect = lpDrawItemStruct->rcItem;rect.DeflateRect(4,2,2,2);rect.left += bmpInfo.bmWidth;dc.FillRect(rect,&br);br.DeleteObject();// 绘制菜单项的文本dc.SetBkMode(TRANSPARENT);CString strItemText;CMenu *pMenu = CMenu::FromHandle((HMENU)lpDrawItemStruct->hwndItem);pMenu->GetMenuString(lpDrawItemStruct->itemID, strItemText, MF_BYCOMMAND);dc.DrawText(strItemText,rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);// 这里一定要将局部变量dc和lpDrawItemStruct中的dc句柄分离,否则画图会出问题的dc.Detach();CDialogEx::OnDrawItem(nIDCtl, lpDrawItemStruct);}



0 0
原创粉丝点击