静态菜单和动态菜单

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");
/* CMenu::AppendMenu
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");
/* CMenu::InsertMenu
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);*/
 from 玩转C科技.NET
<script type="text/javascript"> google_ad_client = "pub-2416224910262877"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel = ""; google_color_border = "E1771E"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "008000"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值