久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

解決 不能通過(guò)CCmdUI *pCmdUI改變對(duì)話框菜單狀態(tài) 的問(wèn)題

 c 資料收集 2014-09-12

    我在處理一個(gè)對(duì)話框的菜單時(shí),想為兩個(gè)菜單命令添加單選標(biāo)記,,于是給兩個(gè)菜單都添加了UPDATE_COMAND_UI 事件,,在生成的函數(shù)里設(shè)置單選標(biāo)記,代碼如下:

Code:
  1. void CCaculatorDlg::OnUpdateToStd(CCmdUI *pCmdUI)   
  2. {   
  3.     if (!m_bSwitch)   
  4.     {   
  5.         pCmdUI->SetCheck(1);   
  6.     }   
  7.     else  
  8.     {   
  9.         pCmdUI->SetCheck(0);   
  10.     }   
  11. }   
  12.   
  13. void CCaculatorDlg::OnUpdateToSci(CCmdUI *pCmdUI)   
  14. {   
  15.     if (m_bSwitch)   
  16.     {   
  17.         pCmdUI->SetCheck(1);   
  18.     }   
  19.     else  
  20.     {   
  21.         pCmdUI->SetCheck(0);   
  22.     }   
  23. }  

    可是,,程序并沒(méi)有像預(yù)想的那樣出現(xiàn)單選標(biāo)記,。這種方法在單文檔或多文檔程序中很常用,根本不會(huì)出現(xiàn)問(wèn)題,,為什么在對(duì)話框程序中就不行了呢,?

    折騰了一個(gè)多小時(shí),,終于在網(wǎng)上找到了問(wèn)題的所在和解決辦法,。引用原文:

Symptos:

Changing the state (enable/disable, check/uncheck, change text) of a menu item from its command user-interface (UI) handler does not work correctly if the menu is attached to a dialog box:

Code:
  1. void CTestDlg::OnUpdateFileExit(CCmdUI* pCmdUI)    
  2. {   
  3.     pCmdUI->Enable(FALSE); //Not calling the command handler, but does not show as disabled.   
  4.     pCmdUI->SetCheck(TRUE); // Does not show check mark before the text.   
  5.     pCmdUI->SetRadio(TRUE); // Does not show dot before the text.   
  6.     pCmdUI->SetText("Close"); //Does not change the text.   
  7. }  

Cause:

When a drop-down menu is displayed, the WM_INITMENUPOPUP message is sent prior to displaying the menu items. The MFC CFrameWnd::OnInitMenuPopup function iterates through the menu items and calls the update command UI handler for the item, if there is one. The appearance of each menu item is updated to reflect its state (enabled/disabled, checked/unchecked).

The update UI mechanism doesn't work for a dialog box-based application because CDialog has no OnInitMenuPopup handler and it uses CWnd's default handler, which does not call update command UI handlers for menu items.

Rusolution:

Use the following steps to resolve this problem:

1.Add an ON_WM_INITMENUPOPUP entry to the message map:

Code:
  1. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)   
  2.     //}}AFX_MSG_MAP   
  3.   
  4.     ON_WM_INITMENUPOPUP()   
  5. END_MESSAGE_MAP()  

2.Add a OnInitMenuPopup member function to your dialog box class and copy the following code (note that this code is taken largely from CFrameWnd::OnInitMenuPopup in WinFrm.cpp):

Code:
  1. void CTestDlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)   
  2. {   
  3.     ASSERT(pPopupMenu != NULL);   
  4.     // Check the enabled state of various menu items.   
  5.   
  6.     CCmdUI state;   
  7.     state.m_pMenu = pPopupMenu;   
  8.     ASSERT(state.m_pOther == NULL);   
  9.     ASSERT(state.m_pParentMenu == NULL);   
  10.   
  11.     // Determine if menu is popup in top-level menu and set m_pOther to   
  12.     // it if so (m_pParentMenu == NULL indicates that it is secondary popup).   
  13.     HMENU hParentMenu;   
  14.     if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)   
  15.         state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.   
  16.     else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)   
  17.     {   
  18.         CWnd* pParent = this;   
  19.            // Child windows don't have menus--need to go to the top!   
  20.         if (pParent != NULL &&   
  21.            (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)   
  22.         {   
  23.            int nIndexMax = ::GetMenuItemCount(hParentMenu);   
  24.            for (int nIndex = 0; nIndex < nIndexMax; nIndex++)   
  25.            {   
  26.             if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)   
  27.             {   
  28.                 // When popup is found, m_pParentMenu is containing menu.   
  29.                 state.m_pParentMenu = CMenu::FromHandle(hParentMenu);   
  30.                 break;   
  31.             }   
  32.            }   
  33.         }   
  34.     }   
  35.   
  36.     state.m_nIndexMax = pPopupMenu->GetMenuItemCount();   
  37.     for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;   
  38.       state.m_nIndex++)   
  39.     {   
  40.         state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);   
  41.         if (state.m_nID == 0)   
  42.            continue; // Menu separator or invalid cmd - ignore it.   
  43.   
  44.         ASSERT(state.m_pOther == NULL);   
  45.         ASSERT(state.m_pMenu != NULL);   
  46.         if (state.m_nID == (UINT)-1)   
  47.         {   
  48.            // Possibly a popup menu, route to first item of that popup.   
  49.            state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);   
  50.            if (state.m_pSubMenu == NULL ||   
  51.             (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||   
  52.             state.m_nID == (UINT)-1)   
  53.            {   
  54.             continue;       // First item of popup can't be routed to.   
  55.            }   
  56.            state.DoUpdate(this, TRUE);   // Popups are never auto disabled.   
  57.         }   
  58.         else  
  59.         {   
  60.            // Normal menu item.   
  61.            // Auto enable/disable if frame window has m_bAutoMenuEnable   
  62.            // set and command is _not_ a system command.   
  63.            state.m_pSubMenu = NULL;   
  64.            state.DoUpdate(this, FALSE);   
  65.         }   
  66.   
  67.         // Adjust for menu deletions and additions.   
  68.         UINT nCount = pPopupMenu->GetMenuItemCount();   
  69.         if (nCount < state.m_nIndexMax)   
  70.         {   
  71.            state.m_nIndex -= (state.m_nIndexMax - nCount);   
  72.            while (state.m_nIndex < nCount &&   
  73.             pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)   
  74.            {   
  75.             state.m_nIndex++;   
  76.            }   
  77.         }   
  78.         state.m_nIndexMax = nCount;   
  79.     }   
  80. }  

原來(lái)如此,按照解決方案做,,預(yù)期的效果真的出現(xiàn)了,!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多