#include <afxwin.h>//必须加载此文件,该文件中定义了所有MFC相关的类 #include "resource.h" class MyFrame:public CFrameWnd { private: CMenu MainMenu,*SysMenu,*PopMenu,*SubMenu; COLORREF color; public: MyFrame() { Create(NULL,//产生标准窗口 "Menu",//窗口标题 WS_OVERLAPPEDWINDOW|WS_VISIBLE,//窗口样式 rectDefault,//窗口大小 NULL,//指向父窗口的指针 MAKEINTRESOURCE(IDR_SMENU)//使用菜单的标志 ); SysMenu = GetSystemMenu(FALSE);//取得系统菜单 SysMenu->AppendMenu(IDR_INSMENU,MF_SEPARATOR); SysMenu->AppendMenu(MF_STRING|MF_CHECKED,IDR_INSMENU,"insert by me"); color = RGB(255,0,0); } afx_msg void OnSwitchMenu() { CMenu InsMenu; InsMenu.LoadMenu(IDR_INSMENU); MainMenu.LoadMenu(IDR_MAINMENU); MainMenu.AppendMenu(MF_POPUP,(UINT)InsMenu.m_hMenu,"Color Menu"); SubMenu= MainMenu.GetSubMenu(1); SubMenu->AppendMenu(MF_SEPARATOR); SubMenu->InsertMenu(IDM_CreateItem,MF_BYCOMMAND|MF_STRING,IDM_InsertItem,"Insert a Item"); SubMenu->InsertMenu(IDM_CreateItem,MF_BYCOMMAND|MF_POPUP,(UINT)InsMenu.m_hMenu,"insert a sub menu"); //SetCheck();//设置应该被选择的菜单项 SubMenu->InsertMenu(IDM_CreateItem,MF_BYCOMMAND|MF_SEPARATOR); InsMenu.Detach();//将菜单资源与菜单对象分离 SetMenu(&MainMenu); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd) ON_COMMAND(IDR_SwitchMenu,OnSwitchMenu) END_MESSAGE_MAP() class MyApp:public CWinApp { public: BOOL InitInstance() { CFrameWnd *Frame = new MyFrame; m_pMainWnd = Frame; Frame->ShowWindow(SW_SHOW); return true; } } a_app;
#define IDR_MAINMENU 101 #define IDR_SMENU 102 #define IDR_POPMENU 103 #define IDR_INSMENU 104 #define ID_RED 40001 #define ID_RED_GREEN 40002 #define ID_RED_BLUE 40003 #define ID_RED_BLACK 40004 #define IDM_CREATEITEM 40005 #define ID_FILE 40005 #define ID_DYNAMICMENU 40006 #define ID_Menu 40007 #define ID_DYNAMICMENU_INSERTASUBMENU 40008 #define ID_COLORMENU 40009 #define ID_COLORMENU_RED 40010 #define ID_COLORMENU_GREEN 40011 #define ID_COLORMENU_BLUE 40012 #define ID__POPITEM1 40013 #define ID__POPITEM2 40014 #define ID_FILE_EXIT 40015 #define IDR_SwitchMenu 40016 #define IDM_CreateItem 40017 #define IDM_InsertItem 40018