菜单简单操作

1。CMenu::CheckMenuItem
UINT CheckMenuItem(
   UINT nIDCheckItem,
   UINT nCheck 
);
添加或者去掉选中标记
nCheck:MF_CHECKED or MF_UNCHECKED with MF_BYPOSITION or MF_BYCOMMAND 
nIDCheckItem:如果nCheck为MF_BYCOMMAND,则这为ID,如果nCheck为MF_BYPOSITION,则为0,1,2,3。。。
返回值:
为上次的状态
The previous state of the item: MF_CHECKED or MF_UNCHECKED, or 0xFFFFFFFF if the menu item did not exist


CMenu *pSysMenu;
pSysMenu = this->GetMenu();
CMenu * pMenu = NULL;
pMenu = pSysMenu->GetSubMenu(0);
if (pMenu!=NULL)
{
pMenu->CheckMenuItem(ID_SYS_NEW,MF_BYCOMMAND|MF_CHECKED);
//pMenu->CheckMenuItem(ID_SYS_NEW,MF_BYCOMMAND|MF_UNCHECKED);
}




2。CMenu::EnableMenuItem 
UINT EnableMenuItem(
   UINT nIDEnableItem,
   UINT nEnable 
);
激活或不激活改项
nEnable:
MF_DISABLED, MF_ENABLED, or MF_GRAYED, MF_BYCOMMAND or MF_BYPOSITION
nIDEnableItem:
如果nEnable为MF_BYCOMMAND,则这为ID,如果nEnable为MF_BYPOSITION,则为0,1,2,3。。。


CMenu *pSysMenu;
pSysMenu = this->GetMenu();
CMenu * pMenu = NULL;
pMenu = pSysMenu->GetSubMenu(0);
if (pMenu!=NULL)
{
pMenu->EnableMenuItem(0,MF_BYPOSITION|MF_ENABLED);
//pMenu->EnableMenuItem(0,MF_BYPOSITION|MF_DISABLED|MF_GRAYED);
}




3。CMenu::ModifyMenu 
BOOL ModifyMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem = 0,
   LPCTSTR lpszNewItem = NULL 
);
BOOL ModifyMenu(
   UINT nPosition,
   UINT nFlags,
   UINT_PTR nIDNewItem,
   const CBitmap* pBmp 
);
改变菜单项的状态
Changes an existing menu item at the position specified by nPosition.
nPosition:
看nFlags而定,如果为BY_COMMAND,则为菜单项的id,否则为菜单项的顺序,从0开始
nFlags:有MF_DISABLED, MF_ENABLED, MF_GRAYED, MF_BYCOMMAND,MF_BYPOSITION,MF_CHECKED,MF_UNCHECKED等。


nIDNewItem:菜单项id,如果nFlags为MF_POPUP,则为菜单项的句柄。
lpszNewItem:
菜单项名称
CMenu *pSysMenu;
pSysMenu = this->GetMenu();
CMenu * pMenu = NULL;
pMenu = pSysMenu->GetSubMenu(0);
if (pMenu!=NULL)
{
pMenu->ModifyMenu(ID_SYS_OPEN,MF_BYCOMMAND|MF_DISABLED|MF_GRAYED,ID_SYS_OPEN,"打开"); 
                                                           //后面两项要有
pMenu->ModifyMenu(ID_SYS_OPEN,MF_BYCOMMAND|MF_ENABLED,ID_SYS_OPEN,"打开");
}




4。CMenu::AppendMenu 
在后面添加菜单项
BOOL AppendMenu(
   UINT nFlags,
   UINT_PTR nIDNewItem = 0,
   LPCTSTR lpszNewItem = NULL 
);
BOOL AppendMenu(
   UINT nFlags,
   UINT_PTR nIDNewItem,
   const CBitmap* pBmp 
);
nFlags:为上面讲过的各种标记
nIDNewItem:ID或者顺序号,或者新popup的句柄(如果nFlags为MF_POPUP)
lpszNewItem:新项名称


5。CMenu::CreateMenu 
创建菜单
Creates a menu and attaches it to the CMenu object.
BOOL CreateMenu( );
创建成功返回true,否则为false


6。CMenu::CreatePopupMenu 
Creates a pop-up menu and attaches it to the CMenu object.
创建弹出菜单
BOOL CreatePopupMenu( );




7。CMenu::TrackPopupMenu
Displays a floating pop-up menu at the specified location and tracks the selection of items on the pop-up menu.
显示弹出菜单
BOOL TrackPopupMenu(
   UINT nFlags,
   int x,
   int y,
   CWnd* pWnd,
   LPCRECT lpRect = 0
);
nFlags:Specifies a screen-position flag and a mouse-button flag. The screen-position flag can be one of the following: 
TPM_CENTERALIGN   Centers the pop-up menu horizontally relative to the coordinate specified by x. 
TPM_LEFTALIGN   Positions the pop-up menu so that its left side is aligned with the coordinate specified by x. 
TPM_RIGHTALIGN   Positions the pop-up menu so that its right side is aligned with the coordinate specified by x. 
The mouse-button flag can be either of the following:


TPM_LEFTBUTTON   Causes the pop-up menu to track the left mouse button. 
TPM_RIGHTBUTTON   Causes the pop-up menu to track the right mouse button. 
还有其他
x,y为坐标
pWnd:拥有菜单的窗体


8。CMenu::GetSubMenu 
Retrieves the CMenu object of a pop-up menu.
得到菜单的子弹出菜单
CMenu* GetSubMenu(
   int nPos 
) const;
Parameters
nPos 
Specifies the position of the pop-up menu contained in the menu. Position values start at 0 for the first menu item. The


pop-up menu's identifier cannot be used in this function




9。CMenu::GetSafeHmenu 
得到菜单句柄
Returns the HMENU wrapped by this CMenu object, or a NULL CMenu pointer.
HMENU GetSafeHmenu( ) const


例子:弹出菜单
int nSel = 0;
CMenu menu;
menu.CreatePopupMenu();
for (int i=0; i<13; i++)
{
CString str;
str.Format("menu%02d",i);
menu.AppendMenu(MF_STRING,i+1,str);
   if (i%4==0&&i!=13-1)
     {
menu.AppendMenu(MF_SEPARATOR);
     }
}
POINT pos;
::GetCursorPos(&pos);
//TPM_RETURNCMD|TPM_NONOTIFY表示不即或WM_COMMAND而是直接返回值
nSel = menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RETURNCMD|TPM_NONOTIFY,pos.x,pos.y,this);
menu.DestroyMenu();
return nSel;
//其中使用TPM_RETURNCMD|TPM_NONOTIFY使得选中菜单返回值而不是触发WM_COMMAND消息,
如果没选中则返回0,否则返回AppendMenu的第二个参数


例子:弹出菜单,其中的一菜单中又有一菜单
int nRet = 0;
CMenu popMenu;
CMenu subMenu;
subMenu.CreatePopupMenu();
CString str;
for (int i=0; i<4; i++)
{
str.Format("子菜单%2d",i);
subMenu.AppendMenu(MF_STRING,i+1+40,str);
}
popMenu.CreatePopupMenu();
for (i=0; i<9; i++)
{
str.Format("菜单%2d",i);
if (i!=4)
{
   popMenu.AppendMenu(MF_STRING,i+1,str);
}
else
{
   popMenu.AppendMenu(MF_POPUP,(UINT)subMenu.m_hMenu,str);
}


}
ClientToScreen(&point);
nRet = popMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,point.x,point.y,this);
ScreenToClient(&point);
subMenu.DestroyMenu();
popMenu.DestroyMenu();


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值