Win32 菜单添加复选标记和单选标记

在菜单中添加复选标记用CheckMenuItem函数

CheckMenuItem函数原型:

DWORD
WINAPI
CheckMenuItem(
    _In_ HMENU hMenu,    //菜单句柄
    _In_ UINT uIDCheckItem,  //菜单ID
    _In_ UINT uCheck);    //复选标记MF_CHECKED(添加复选标记)和MF_UNCHECKED(取消复选标记)

实例:

LRESULT MyWnd::OnCommand(DWORD dwID, DWORD dwEnter, HWND hCtrlWnd)
{
    HMENU hMenu = GetMenu(m_hWnd);  //获得窗口菜单句柄
    HMENU hItem = GetSubMenu(hMenu, 1);//获得第二个子菜单项的句柄

    switch (dwID)
    {
   case ID_TOOBAR_MENU:
    {
        //判断是否有复选标记,如果有就取消复选标记,没有就添加复选标记
        if (CheckMenuItem(hItem, ID_TOOBAR_MENU, MF_CHECKED)&MF_CHECKED)
            CheckMenuItem(hItem, ID_TOOBAR_MENU, MF_UNCHECKED);

        break;
    }
    case ID_STAT_MENU:
    {
    
        if (CheckMenuItem(hItem, ID_STAT_MENU, MF_CHECKED)&MF_CHECKED)
            CheckMenuItem(hItem, ID_STAT_MENU, MF_UNCHECKED);

        break;
    }
        
    case ID_MENU_MENU:
    {
        if (CheckMenuItem(hItem, ID_MENU_MENU, MF_CHECKED)&MF_CHECKED)
            CheckMenuItem(hItem, ID_MENU_MENU, MF_UNCHECKED);
       
        break;
    }

    }
    return 0;
}

 

 

在菜单中添加单选标记用CheckMenuRadioItem函数

CheckMenuRadioItem函数原型:

BOOL
WINAPI
CheckMenuRadioItem(
    _In_ HMENU hmenu,  //菜单句柄
    _In_ UINT first,  //第一个菜单项ID
    _In_ UINT last,   //最后一个菜单项ID
    _In_ UINT check,  //要标记的菜单项ID
    _In_ UINT flags);  //单选标记MF_BYCOMMAND

实例:

//菜单ID号要是连续的Id号

#define ID_TOOBAR_MENU 40027
#define ID_STAT_MENU 40028
#define ID_MENU_MENU 40029




LRESULT MyWnd::OnCommand(DWORD dwID, DWORD dwEnter, HWND hCtrlWnd) { HMENU hMenu
= GetMenu(m_hWnd); HMENU hItem = GetSubMenu(hMenu, 1); switch (dwID) { case ID_TOOBAR_MENU: { CheckMenuRadioItem(hItem, ID_TOOBAR_MENU, ID_MENU_MENU, ID_TOOBAR_MENU, MF_BYCOMMAND); break; } case ID_STAT_MENU: { CheckMenuRadioItem(hItem, ID_TOOBAR_MENU, ID_MENU_MENU, ID_STAT_MENU, MF_BYCOMMAND); break; } case ID_MENU_MENU: { CheckMenuRadioItem(hItem, ID_TOOBAR_MENU, ID_MENU_MENU, ID_MENU_MENU, MF_BYCOMMAND); break; } } return 0; }

效果图:

 

 

 

posted @ 2021-06-24 00:35  初吻给了烟灬  阅读(337)  评论(0编辑  收藏  举报