Menu-item attributes are represented in the fAttribute field of the MENUITEM data structure. Typically, attributes are set in the resource-definition file of the menu and are changed at run time as required. Applications can use the MM_SETITEMATTR and MM_QUERYITEMATTR messages to set and query attributes for a particular menu item. One of the most common uses of these messages is to check and uncheck menu items to let the user know what option is selected currently. For example, if you have a menu item that should toggle between checked and unchecked each time the user selects it, you can use the following figure to change the checked attribute. In this example, you send an MM_QUERYITEMATTR message to the menu item to obtain its current checked attribute; then, you use the exclusive OR operator to toggle the state; and finally, you send the new attribute state back to the item using an MM_SETITEMATTR message.


    usAttrib = SHORT1FROMMR(
        WinSendMsg(hwndMenu,           /* Submenu window        */
        MM_QUERYITEMATTR,              /* Message               */
        (MPARAM)itemID,                /* Item identifier       */
        (MPARAM)MIA_CHECKED            /* Attribute mask        */
        ));

    usAttrib = MIA_CHECKED;        /* XOR to toggle checked attribute */

    WinSendMsg(hwndMenu,                      /* Submenu window        */
        MM_SETITEMATTR,                       /* Message               */
        (MPARAM)itemID,                       /* Item identifier       */
        MPFROM2SHORT(MIA_CHECKED, usAttrib)); /* Attribute mask, value */


[Back] [Next]