Menu-静态菜单和动态菜单
1、 静态菜单
对CMainFrame::OnCreate中添加以下代码。
/*TODO:将子菜单中的某一菜单项选中
CWnd::GetMenu()->GetSubMenu(0)->CheckMenuItem(0,MF_BYPOSITION|MF_CHECKED);
GetMenu()获取菜单
GetSubMenu(0)获取子菜单
CheckMenuItem(0,MF_BYPOSITION|MF_CHECKED);
按索引访问,索引为0,按位置访问MF_BYPOSITION,并加上标记MF_CHECKED
按ID访问,ID为ID号,按位置访问MF_BYCOMMAND,并加上标记MF_CHECKED
*/
//GetMenu()->GetSubMenu(0)->CheckMenuItem(0,MF_BYPOSITION|MF_CHECKED);
//GetMenu()->GetSubMenu(0)->CheckMenuItem(ID_FILE_NEW_FRAME,MF_BYCOMMAND|MF_CHECKED);
/*TODO:缺省菜单项,即某一子菜单以粗体的形式显示
BOOL SetDefaultItem(UINT uItem,BOOL fByPos = FALSE);
If this parameter(fByPos) is FALSE, uItem is a menu item identifier. Otherwise, it is a menu item position.
如果为FALSE的时候FALSE也可以不写,因为缺省值就是FALSE
另外:在一个子菜单中只能有一个缺省菜单,如果设置多个,则以后一个为缺省菜单
子菜单中计算索引值的时候也应该包括分隔符
*/
//GetMenu()->GetSubMenu (0)->SetDefaultItem (1,TRUE);
//GetMenu()->GetSubMenu (0)->SetDefaultItem (ID_FILE_OPEN,FALSE);
//GetMenu()->GetSubMenu (0)->SetDefaultItem (ID_FILE_OPEN);
/*TODO:为菜单项添加图形标记,也就是在标签前加一个位图。
此处必须注意:对菜单位图是有大小要求的。在不知道的情况下可以通过编程来获得。
//以下代码用来获得系统菜单MENUCHECK的高和宽
CString str;
str.Format("x=%d,y=%d",GetSystemMetrics(SM_CXMENUCHECK)
,GetSystemMetrics(SM_CYMENUCHECK));
MessageBox(str);
*/
//m_bitmap.LoadBitmap (IDB_BITMAP1);
//GetMenu()->GetSubMenu (0)->SetMenuItemBitmaps(2,MF_BYPOSITION,&m_bitmap,&m_bitmap);
/*让一个菜单生效或屏蔽它
NOTE: m_bAutoMenuEnable is set to FALSE in the constructor of
CMainFrame so no ON_UPDATE_COMMAND_UI or ON_COMMAND handlers are
needed, and CMenu::EnableMenuItem() will work as expected.
因此在MainFrame()构造函数中要让m_bAutoMenuEnable=FALSE;才可以让手动修改生效。
*/
//GetMenu()->GetSubMenu(0)->EnableMenuItem(3,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
/*TODO:隐藏/显示菜单。
*/
//SetMenu(NULL); //隐藏菜单。
//以下显示菜单:
//CMenu menu;
//menu.LoadMenu(IDR_MAINFRAME);
//SetMenu(&menu);
//menu.Detach(); //必须调用此方法以保证在调用完菜单后将其与当前句柄脱离,其将在WINDOWS销毁的时候一起销毁。
2、 动态菜单
/* CMenu menu;
menu.CreatePopupMenu(); //插入一个弹出菜单
// GetMenu()->AppendMenu(MF_POPUP,(UINT)menu.m_hMenu,"WinSun");
Appends a new item to the end of a menu.
BOOL AppendMenu(
UINT nFlags,
UINT_PTR nIDNewItem = 0,
LPCTSTR lpszNewItem = NULL
);
BOOL AppendMenu(
UINT nFlags,
UINT_PTR nIDNewItem,
const CBitmap* pBmp
);
*/
GetMenu()->InsertMenu(2,MF_BYPOSITION | MF_POPUP,(UINT)menu.m_hMenu,"WinSun");
Inserts a new menu item at the position specified by nPosition and moves other items down the menu.
BOOL InsertMenu(
UINT nPosition,
UINT nFlags,
UINT_PTR nIDNewItem = 0,
LPCTSTR lpszNewItem = NULL
);
BOOL InsertMenu(
UINT nPosition,
UINT nFlags,
UINT_PTR nIDNewItem,
const CBitmap* pBmp
);
*/
menu.AppendMenu(MF_STRING,IDM_HELLO,"Hello");
menu.AppendMenu(MF_STRING,112,"Weixin");
menu.AppendMenu(MF_STRING,113,"Mybole");
menu.Detach();
GetMenu()->GetSubMenu(0)->AppendMenu(MF_STRING,114,"Welcome");
GetMenu()->GetSubMenu(0)->InsertMenu(ID_FILE_OPEN,
MF_BYCOMMAND | MF_STRING,115,"维新");
GetMenu()->DeleteMenu(1,MF_BYPOSITION);
GetMenu()->GetSubMenu(0)->DeleteMenu(2,MF_BYPOSITION);*/
posted on 2008-07-18 20:12 volnet(可以叫我大V) 阅读(2610) 评论(2) 编辑 收藏 举报