此方法实现每次点击菜单项时,使其选中的菜单项,打√标记,并设置到Disable状态.
在菜单项的响应函数中调用,形参传递对应的ID即可!
CMenu* pSubMenu=NULL;
UINT Sub_IndexMax=0;
pSubMenu = CMenu::FromHandle(GetSkinMenu(m_pMainWnd->m_hWnd))->GetSubMenu(1);
Sub_IndexMax = pSubMenu->GetMenuItemCount();
MENUITEMINFO info;
info.cbSize = sizeof (MENUITEMINFO); // must fill up this field
info.fMask = MIIM_STATE | MIIM_ID; // get the state and the id of the menu item
for (UINT cout =0; cout < Sub_IndexMax; cout++)
{
VERIFY(pSubMenu->GetMenuItemInfo(cout, &info,TRUE));
if (info.wID == m_ID)
{
pSubMenu->CheckMenuItem(m_ID,MF_BYCOMMAND | MF_CHECKED); //打√
pSubMenu->EnableMenuItem(m_ID,MF_DISABLED | MF_GRAYED); //Disable menu
continue;
}
if (info.fState & MF_CHECKED)
pSubMenu->CheckMenuItem(cout, MF_UNCHECKED | MF_BYPOSITION);
if (info.fState & (MF_DISABLED | MF_GRAYED))
pSubMenu->EnableMenuItem(cout,MF_ENABLED | MF_BYPOSITION);
}
说明:
pSubMenu = CMenu::FromHandle(GetSkinMenu(m_pMainWnd->m_hWnd))->GetSubMenu(1);
//作为CwinApp的成员函数,pSubMenu指向要处理的菜单项,GetSkinMenu为Skin类成员函数,在使用skin美化之后需要这样处理(具体请看上篇文章描述)
SkinMagic使用后按钮加自定义图标或菜单GetMneu返回NULL的解决方法
Sub_IndexMax = pSubMenu->GetMenuItemCount(); //调用Cemnu成员函数获取菜单项数量
info.fMask = MIIM_STATE | MIIM_ID; // 使其返回菜单状态及ID信息
//m_ID为菜单项的ID号
if (info.wID == m_ID)
{
pSubMenu->CheckMenuItem(m_ID,MF_BYCOMMAND | MF_CHECKED); //打√
pSubMenu->EnableMenuItem(m_ID,MF_DISABLED | MF_GRAYED); //变灰及取消选中状态
continue;
}
//每次选中任意菜单项前,先初始化所有菜单项的状态
if (info.fState & MF_CHECKED)
pSubMenu->CheckMenuItem(cout, MF_UNCHECKED | MF_BYPOSITION);
if (info.fState & (MF_DISABLED | MF_GRAYED))
pSubMenu->EnableMenuItem(cout,MF_ENABLED | MF_BYPOSITION);