2013-06-20 103 views
0

我有一個MFC應用程序,其中有兩個菜單選項對應於兩個工具欄 - 菜單選項可以切換工具欄的可見性。如果工具欄當前可見,我需要檢查菜單選項。這是我到目前爲止:MFC - 檢查/取消選中菜單項

BEGIN_MESSAGE_MAP(CLevelPackEditApp, CWinAppEx) 
    // Standard file based document commands 
    ON_UPDATE_COMMAND_UI(ID_LEVEL_PROPERTIES, &CLevelPackEditApp::OnViewLevelProperties) 
END_MESSAGE_MAP() 

void CLevelPackEditApp::OnViewLevelProperties(CCmdUI* pCmdUI) 
{ 
    // Get a handle to the main window 
    CMainFrame* main = ((CMainFrame*)m_pMainWnd); 

    // Get a handle to the level properties toolbar for the main window 
    CLevelProperties* obj = main->GetLevelProperties(); 

    if (obj->IsWindowVisible()) 
    { 
     pCmdUI->SetCheck(0); 
     obj->ShowPane(false, false, false); 
    } else { 
     pCmdUI->SetCheck(); 
     obj->ShowPane(true, false, true); 
    } 
} 

它的工作....有點。它在選中狀態和未選中狀態之間切換,但是它每秒執行多次 - 我懷疑檢查菜單項會導致菜單更新,所以它沒有被選中,所以它會更新,所以它會被檢查,aaaannnd和重複。我怎樣才能解決這個問題?

回答

2

ON_UPDATE_COMMAND_UI()函數應該只設置/清除複選標記;只有在按鈕被點擊時才撥打obj->ShowPane()

BEGIN_MESSAGE_MAP(CLevelPackEditApp, CWinAppEx) 
    // Standard file based document commands 
    ON_COMMAND_UI(ID_LEVEL_PROPERTIES, &CLevelPackEditApp::OnViewLevelProperties) 
    ON_UPDATE_COMMAND_UI(ID_LEVEL_PROPERTIES, &CLevelPackEditApp::OnUpdateViewLevelProperties) 
END_MESSAGE_MAP() 

void CLevelPackEditApp::OnViewLevelProperties() 
{ 
    // Get a handle to the main window 
    CMainFrame* main = ((CMainFrame*)m_pMainWnd); 

    // Get a handle to the level properties toolbar for the main window 
    CLevelProperties* obj = main->GetLevelProperties(); 

    if (obj->IsWindowVisible()) 
     obj->ShowPane(false, false, false); 
    else 
     obj->ShowPane(true, false, true); 
} 

void CLevelPackEditApp::OnUpdateViewLevelProperties(CCmdUI* pCmdUI) 
{ 
    // Get a handle to the main window 
    CMainFrame* main = ((CMainFrame*)m_pMainWnd); 

    // Get a handle to the level properties toolbar for the main window 
    CLevelProperties* obj = main->GetLevelProperties(); 

    pCmdUI->SetCheck(obj->IsWindowVisible()); 
} 
+0

我把這個放在我的代碼中,但是由於某種原因,OnUpdateLevelProperties從來沒有被調用過。 –

+0

Nvm,我犯了一個愚蠢的錯誤。 –