人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統(tǒng)下載站 > 技術開發(fā)教程 > 詳細頁面

讓對話框對UPDATE_COMMAND_UI生效

讓對話框對UPDATE_COMMAND_UI生效

更新時間:2022-09-03 文章作者:未知 信息來源:網絡 閱讀次數:

問題:一般情況下我們用UPDATE_COMMAND_UI來修改菜單的狀態(tài)(enable/disable, check/uncheck, change text),但這個方法在一個基于對話框上的菜單卻沒有效果。
void CTestDlg::OnUpdateFileExit(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(FALSE);
    pCmdUI->SetCheck(TRUE);
    pCmdUI->SetRadio(TRUE);
    pCmdUI->SetText("Close");
//以上方法在MDI、SDI程序中都能起作用,在對話框中卻沒有效果,根本沒有調用這個函數。
}
    
原因分析:當顯示一個下拉的菜單的時候,在顯示菜單前會發(fā)送WM_INITMENUPOPUP消息。而CFrameWnd::OnInitMenuPopup函數會刷新這個菜單項,同時如果有UPDATE_COMMAND_UI響應函數,則調用它。通過它來更新反應每個菜單的外觀效果(enabled/disabled, checked/unchecked).
在一個基于對話框的程序中,因為沒有OnInitMenuPopup函數,所以不會調用UPDATE_COMMAND_UI響應函數,而是使用了CWnd類的默認處理, 這種處理沒有調用UPDATE_COMMAND_UI響應函數。

解決方法如下:
第一步:
在對話框類的.cpp文件,添加一個ON_WM_INITMENUPOPUP入口到消息映射里面
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//}}AFX_MSG_MAP
ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()
第二步:
在對話框類的.h文件添加消息函數聲明。
// Generated message map functions
//{{AFX_MSG(CDisableDlgMenuDlg)
afx_msg void OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
第三步:
在對話框類的.cpp文件添加如下函數代碼(大部分代碼取自WinFrm.cpp文件的函數CFrameWnd::OnInitMenuPopup):
void C******Dlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
{
    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;
    }
}

更詳細的信息可參考
http://support.microsoft.com/default.aspx?scid=kb;en-us;242577

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 尼玛县| 株洲市| 渭源县| 来凤县| 泗水县| 唐海县| 九台市| 遂昌县| 铜梁县| 德庆县| 鹿邑县| 原阳县| 松滋市| 思茅市| 峨眉山市| 临夏市| 南昌市| 平凉市| 通许县| 吴旗县| 湖州市| 建水县| 鄱阳县| 凤庆县| 双桥区| 长治县| 宁津县| 拉萨市| 克拉玛依市| 龙口市| 麻江县| 东台市| 闽侯县| 新建县| 巴青县| 隆昌县| 西平县| 垣曲县| 夏河县| 荃湾区| 兰西县|