Brian Gradin
Brian Gradin

Reputation: 2275

MFC - Check/Uncheck menu item

I have an MFC application I'm making, in which there are two menu options corresponding to two toolbars - the menu options toggle visibility of the tool bars. I need the menu options to be checked if the tool bar is currently visible. Here's what I've got so far:

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

It works....sort of. It toggles between checked and not checked, but it does so multiple times per second - I suspect that checking the menu item causes the menu to update, so it is unchecked, so it updates, so it is checked, aaaannnd repeat. How can I get around this?

Upvotes: 0

Views: 3203

Answers (1)

Edward Clements
Edward Clements

Reputation: 5132

The ON_UPDATE_COMMAND_UI() function should only set / clear the check marks; call obj->ShowPane() only when the button is clicked:

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

Upvotes: 2

Related Questions