自定义绘制菜单的实现

在Windows XP和图标选择风格下实现自定义绘制菜单是一种提升应用程序界面美观度的方法。本文将介绍如何在MFC应用程序中添加自定义绘制菜单的步骤和代码示例。

步骤1:添加文件

首先,需要将以下文件添加到项目中:

  • MenuCH.cpp
  • MenuCH.h

步骤2:包含头文件

在MainFrm.h文件的顶部添加以下代码:

#include "MenuCH.h"

步骤3:添加成员变量

在MainFrm.h文件中,添加CMainFrame类的成员变量:

protected: // 控制条嵌入成员 CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; CMenuCH m_FileMenu, m_EditMenu, m_ViewMenu, m_HelpMenu; CMenuCH m_ElementMenu; CMenuCH GraphMenu,ColorMenu;

步骤4:初始化菜单

在MainFrm.cpp文件的CMainFrame()构造函数中,添加以下代码来初始化菜单:

CMainFrame::CMainFrame() { // TODO: 添加成员初始化代码 m_FileMenu.CreatePopupMenu(); m_FileMenu.SetMenuHeight(20); m_FileMenu.SetMenuWidth(150); m_FileMenu.SetMenuType(MIT_XP); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_NEW,"&New\tCtrl+N",IDB_NEW); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_OPEN,"&Open\tCtrl+O",IDB_OPEN); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_CLOSE,"&Close"); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_SAVE,"&Save\tCtrl+S",IDB_SAVE); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_SAVE_AS,"Save &As..."); m_FileMenu.AppendMenu(MF_SEPARATOR,0,""); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT,"&Print...",IDB_PRINT); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT_PREVIEW,"Print Pre&view"); m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT_SETUP,"P&rint Setup..."); m_FileMenu.AppendMenu(MF_SEPARATOR,0,""); m_FileMenu.AppendMenu(MF_ENABLED,ID_APP_EXIT,"E&xit"); m_EditMenu.CreatePopupMenu(); m_EditMenu.SetMenuHeight(20); m_EditMenu.SetMenuWidth(165); m_EditMenu.SetMenuType(MIT_XP); m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_UNDO,"Redo\tCtrl+Z",IDB_UNDO); m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_REDO,"Undo\tCtrl+Y",IDB_REDO); m_EditMenu.AppendMenu(MF_SEPARATOR,0,""); m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_CUT,"Cut\tCtrl+X",IDB_CUT); m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_COPY,"Copy\tCtrl+C",IDB_COPY); m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_PASTE,"Paste\tCtrl+V",IDB_PASTE); m_ViewMenu.CreatePopupMenu(); m_ViewMenu.SetMenuHeight(20); m_ViewMenu.SetMenuWidth(170); m_ViewMenu.SetMenuType(MIT_XP); m_ViewMenu.AppendMenu(MF_ENABLED,ID_VIEW_TOOLBAR,"&Toolbar"); m_ViewMenu.AppendMenu(MF_ENABLED,ID_VIEW_STATUS_BAR,"&Status Bar"); m_HelpMenu.CreatePopupMenu(); m_HelpMenu.SetMenuHeight(20); m_HelpMenu.SetMenuWidth(160); m_HelpMenu.SetMenuType(MIT_XP); m_HelpMenu.AppendMenu(MF_ENABLED,ID_APP_ABOUT,"&About BmpMenuDemo...",IDB_HELP); GraphMenu.CreateMenu(); GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART1BOX,"", NULL, AfxGetApp()->LoadIcon(IDI_LINE)); GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART2BOX,"", NULL, AfxGetApp()->LoadIcon(IDI_GRAPHIC)); GraphMenu.AppendMenu(MF_ENABLED|MF_MENUBREAK,ID_GRAPHPART3BOX,"",NULL,AfxGetApp()->LoadIcon(IDI_CIRCLE)); GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART4BOX,"", NULL, AfxGetApp()->LoadIcon(IDI_POLYGON)); GraphMenu.AppendMenu(MF_ENABLED|MF_MENUBREAK,ID_GRAPHPART5BOX,"",NULL,AfxGetApp()->LoadIcon(IDI_ARC)); GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART6BOX,"", NULL, AfxGetApp()->LoadIcon(IDI_TEXT)); m_ElementMenu.CreatePopupMenu(); m_ElementMenu.SetMenuType(MIT_ICON); m_ElementMenu.AppendMenu(MF_SEPARATOR,0,""); m_ElementMenu.AppendMenu(MF_POPUP,(UINT)GraphMenu.m_hMenu,"Element"); ColorMenu.CreatePopupMenu(); ColorMenu.SetMenuHeight(18); ColorMenu.SetMenuWidth(6); ColorMenu.SetMenuType(MIT_COLOR); char clrValue[64]; for(int i=1; i<=16; i++) { wsprintf(clrValue,"%d",rgbColors[i-1]); if(i%4 == 1) ColorMenu.AppendMenu(MF_MENUBREAK|MF_ENABLED,i,clrValue); else ColorMenu.AppendMenu(MF_ENABLED,i,clrValue); } m_ElementMenu.AppendMenu(MF_POPUP,(UINT)ColorMenu.m_hMenu,"Colors"); }

步骤5:创建菜单函数

在MainFrm.cpp文件中添加CMainFrame类的成员函数CreateMenu(),并添加以下代码:

void CMainFrame::CreateMenu() { CMenu* pMenu = this->GetMenu(); pMenu->RemoveMenu(0,MF_BYPOSITION); pMenu->RemoveMenu(0,MF_BYPOSITION); pMenu->RemoveMenu(0,MF_BYPOSITION); pMenu->RemoveMenu(0,MF_BYPOSITION); pMenu->RemoveMenu(0,MF_BYPOSITION); pMenu->InsertMenu(0,MF_BYPOSITION|MF_POPUP,(UINT)m_FileMenu.m_hMenu,"&File"); pMenu->InsertMenu(1,MF_BYPOSITION|MF_POPUP,(UINT)m_EditMenu.m_hMenu,"&Edit"); pMenu->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)m_ViewMenu.m_hMenu,"&View"); pMenu->InsertMenu(3,MF_BYPOSITION|MF_POPUP,(UINT)m_HelpMenu.m_hMenu,"&Help"); pMenu->InsertMenu(4,MF_BYPOSITION|MF_POPUP,(UINT)m_ElementMenu.m_hMenu,"∈"); }

步骤6:调用创建菜单函数

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; ... CreateMenu(); }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485