Flag Behavior in the MFC Menu

I am trying to add a menu item so that it acts as a checkmark, where the user can check / uncheck the box, and other classes can see this flag status of the menu item. I received a proposal to create a class for a menu option (with the option of a pop-up window), however I cannot create a class for a menu option when I am in the resource layout editor in Visual Studio 2005. It would be great to hear suggestions on the easiest way to create menu items, which can fulfill what I described.

+3
source share
3 answers

I ended up getting the menu from the mainframe using the GetMenu () method, and then used this menu object and ID numbers to call CheckMenuItem () with the correct flags, as well as the GetMenuState () function.

+1
source

You must use the function CCmdUI::SetCheckto add a flag to the menu item with ON_UPDATE_COMMAND_UIand a ON_COMMANDhandler to change the state of the flag. This method works both for the main application menu and for any pop-up menus that you can create.

, MDI SDI MFC, , , , , , . , : , ; , , ..

( , , False.)

ID_MY_COMMAND:

// MyView.h

class CMyView : public CView
{
private:
    BOOL m_Flag;

    afx_msg void OnMyCommand();
    afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
    DECLARE_MESSAGE_MAP()
};

// MyView.cpp

BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
    ON_UPDATE_COMMAND_UI(ID_MY_COMMAND, OnUpdateMyCommand)
END_MESSAGE_MAP()

void CMyView::OnMyCommand()
{
    m_Flag = !m_Flag; // Toggle the flag
    // Use the new flag value.
}

void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_Flag);
}

, - m_Flag , , CMyView OnInitialUpdate.

, !

+8

@ChrisN MFC Dialog (pCmdUI->SetCheck(m_Flag); ). Dialog:

// MyView.h

class CMyView : public CView
{
private:
    BOOL m_Flag;
    CMenu * m_menu;

    virtual BOOL OnInitDialog();
    afx_msg void OnMyCommand();
    DECLARE_MESSAGE_MAP()
};

// MyView.cpp

BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
END_MESSAGE_MAP()

BOOL CMyView::OnInitDialog()
{
    m_menu = GetMenu();
}

void CMyView::OnMyCommand()
{
    m_Flag = !m_Flag; // Toggle the flag

    if (m_flag) {
        m_menu->CheckMenuItem(ID_MENUITEM, MF_CHECKED | MF_BYCOMMAND);
    } else {
        m_menu->CheckMenuItem(ID_MENUITEM, MF_UNCHECKED | MF_BYCOMMAND);
    }
}

:

http://www.codeguru.com/forum/showthread.php?t=322261

+2
source

Source: https://habr.com/ru/post/1714028/


All Articles