Chris
Chris

Reputation: 1766

Win32 Alter Menu In Code

I dont do a lot of windows programming but I cant seem to find out how to do this...

I have a toolbar for my application that will allow the user to switch certain functionality on and off. I have windows recognising when one of these toolbars are clicked however one of the options uses the "Checked" functionality.

The question is how do I know if this is current true or false in my code and also how do I set it to false in my code?

Basically I need this...

enter image description here

To look like this...

enter image description here

After the user clicks it

Code so Far...

switch (wmId)
        {
        case ID_SETTINGS_ENABLEGRAVITY:
            {
                MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
                mii.fMask = MIIM_STATE;
                GetMenuItemInfo((HMENU)IDR_MENU1,ID_SETTINGS_ENABLEGRAVITY, FALSE, &mii);
                mii.fState ^= MFS_CHECKED;

                SetMenuItemInfo((HMENU)IDR_MENU1,ID_SETTINGS_ENABLEGRAVITY, FALSE, &mii);
                break;
            }

Thanks

Upvotes: 2

Views: 2286

Answers (1)

David Heffernan
David Heffernan

Reputation: 613491

For Win32 you do this with the GetMenuItemInfo and SetMenuItemInfo functions:

MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_STATE;
GetMenuItemInfo(hMenu, uItem, FALSE, &mii);
mii.fState ^= MFS_CHECKED;
SetMenuItemInfo(hMenu, uItem, FALSE, &mii);

This code toggles the checked property. It assumes that you are identifying the menu item by ID rather than position.


Looking at the code you posted, (HMENU)IDR_MENU1 appears suspicious. I imagine that IDR_MENU1 is an identifier rather than an HMENU. Casting is always a sign of a potential problem. If you don't have the HMENU to hand then call GetMenu to obtain it.

In your code you should be testing the return value of the API calls and if they return FALSE then you should call GetLastError to obtain further information about the failure.

Upvotes: 6

Related Questions