nidhal
nidhal

Reputation: 1797

How to set check on menu item mfc c++

how to set check on menu item mfc c++ i try this but, always unchecked menu item.

CString tcBuff; 
    CMenu popMenu;
    popMenu.LoadMenu(nMenuID);

    if (text.Compare(defaultconfig) == 0)
    {

        tcBuff.LoadStringW(IDC_DEFAULTREMOVE);
        popMenu.ModifyMenuW(ID_CONFIGURATION_DEFAULT,0,ID_CONFIGURATION_DEFAULT,tcBuff);
        popMenu.CheckMenuItem(IDC_DEFAULTREMOVE, MF_CHECKED || MF_BYCOMMAND);
    }

thanks for help.

Upvotes: 0

Views: 5668

Answers (2)

Pavel Nazarov
Pavel Nazarov

Reputation: 733

Try to use ID_CONFIGURATION_DEFAULT instead of IDC_DEFAULTREMOVE in the statement popMenu.CheckMenuItem(IDC_DEFAULTREMOVE, MF_CHECKED || MF_BYCOMMAND);

ID_CONFIGURATION_DEFAULT - command ID IDC_DEFAULTREMOVE - string resource ID

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244981

You want the | operator, not the || operator.

You want to combine the MF_CHECKED and MF_BYCOMMAND bit flags, which you do with a bitwise OR operation. That requires the | operator.

Change your code to look like this:

popMenu.CheckMenuItem(IDC_DEFAULTREMOVE, MF_CHECKED | MF_BYCOMMAND);


The || operator is the logical OR operator. It actually gives you this:

0x8 /* MF_CHECKED */  ||  0x0 /* MF_BYCOMMAND */ == 0

Which is equivalent to MF_UNCHECKED.

Upvotes: 1

Related Questions