1、為頁面(窗口)添加一個(gè)菜單欄和子菜單
2、在XXDlg.h文件中定義一個(gè)菜單欄變量和bool變量
CMenu m_Menu; //菜單變量
bool m_EnableMenu;//菜單欄中某個(gè)子菜單禁用/啟用(變灰)的控制變量
3、在OnInitDialog函數(shù)中進(jìn)行初始化:(即將菜單欄植入主界面)
m_Menu.LoadMenu(IDR_MENU_MAIN);//菜單欄的ID
SetMenu(&m_Menu);//添加入到界面
m_EnableMenu =false;//初始化為禁用子菜單
4、添加菜單ID的ON_UPDATE_COMMAND_UI消息,點(diǎn)擊菜單,在子菜單上右鍵點(diǎn)擊,添加事件處理程序,選擇你的對話框類,在左邊的消息類型中選擇UPDATE_COMMAND_UI添加如下代碼:
void CXXDlg::OnUpdateDisconnectAll(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_EnableMenu); //m_EnableMenu的值對應(yīng)禁用和啟用,1是啟用,0是禁用
}
根據(jù)網(wǎng)上資料,對于視圖文檔類MFC程序,在主框架類CMainFrame的構(gòu)造函數(shù)中把成員變量m_bAutoMenuEnable=FALSE就可以解決問題了。但是基于對話框的MFC應(yīng)用程序沒有m_bAutoMenuEnable變量。解決方法:
5、在對話框類的消息中加入OnInitMenuPopup()消息:
消息函數(shù)內(nèi)重載如下:文章來源:http://www.zghlxwxcb.cn/news/detail-733124.html
void CXXXDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: 在此處添加消息處理程序代碼
ASSERT(pPopupMenu != NULL);
// Check the enabled state of various menu items.
CCmdUI state;
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// Determine if menu is popup in top-level menu and set m_pOther to
// it if so (m_pParentMenu == NULL indicates that it is secondary popup).
HMENU hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
{
CWnd* pParent = this;
// Child windows don't have menus--need to go to the top!
if (pParent != NULL &&
(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
{
int nIndexMax = ::GetMenuItemCount(hParentMenu);
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
{
if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
{
// When popup is found, m_pParentMenu is containing menu.
state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
state.m_nIndex++)
{
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // Menu separator or invalid cmd - ignore it.
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1)
{
// Possibly a popup menu, route to first item of that popup.
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
if (state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1)
{
continue; // First item of popup can't be routed to.
}
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
}
else
{
// Normal menu item.
// Auto enable/disable if frame window has m_bAutoMenuEnable
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// Adjust for menu deletions and additions.
UINT nCount = pPopupMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax)
{
state.m_nIndex -= (state.m_nIndexMax - nCount);
while (state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}
如果想在其他程序中改變菜單的啟用/禁用屬性,改變m_EnableMenu的值即可實(shí)現(xiàn)。文章來源地址http://www.zghlxwxcb.cn/news/detail-733124.html
到了這里,關(guān)于MFC 如何啟用/禁用菜單(返灰/不可點(diǎn)擊狀態(tài))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!