【问题标题】:MFC - Check/Uncheck menu itemMFC - 选中/取消选中菜单项
【发布时间】:2013-06-20 06:37:44
【问题描述】:

我正在制作一个 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 重复。我该如何解决这个问题?

【问题讨论】:

    标签: menu mfc


    【解决方案1】:

    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());
    }
    

    【讨论】:

    • 我把它放在我的代码中,但由于某种原因,永远不会调用 OnUpdateLevelProperties。
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多