2013-06-20 42 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,我犯了一个愚蠢的错误。 –