7.6.5 Using Owner-Drawn Menu Items

Your application can take complete control over the appearance of menu items by using owner-drawn items. An owner-drawn item is one for which your application has total responsibility for drawing in its normal, selected (highlighted), checked, and cleared states.

For example, suppose your application provides a menu from which the user can choose a font. Your application could draw each item by using the font that the item represents; the item for roman would be drawn with a roman font, the item for italic would be drawn in italic, and so on.

You cannot define an owner-drawn item in your application's resource-definition (.RC) file. Instead, you must create a new item or modify an existing one by using the MF_OWNERDRAW menu flag. You can use any of the following functions to specify an owner-drawn menu item:

AppendMenu

InsertMenu

ModifyMenu

When you call any of these functions, you can pass a 32-bit value as the lpNewItem parameter. This 32-bit value can represent any information that is meaningful to your application, and will be available to your application when the item is to be displayed. For example, the value could contain a pointer to a structure; the structure, in turn, might contain a string and the handle of a logical font that your application will use to draw the string.

Before Windows displays an owner-drawn item for the first time, it sends the WM_MEASUREITEM message to the window that owns the item's menu. This message's lParam parameter points to a MEASUREITEMSTRUCT structure that identifies the item and contains the optional 32-bit value for the item. When your application receives the WM_MEASUREITEM message, it must fill in the itemWidth and itemHeight members of the structure before returning from processing the message. Windows uses the information in these members when creating the bounding rectangle in which your application draws the menu item; it also uses the information to detect the user's interaction with the command associated with the item.

When the item must be drawn (for example, when it is first displayed or when the user chooses it as a command), Windows sends the WM_DRAWITEM message to the window that owns the menu. The lParam parameter of the WM_DRAWITEM message points to a DRAWITEMSTRUCT structure. Like the MEASUREITEMSTRUCT structure, DRAWITEMSTRUCT contains information about the item and its optional 32-bit data. In addition, DRAWITEMSTRUCT contains flags that indicate the state of the item (such as grayed or checked) as well as a bounding rectangle and a device context with which your application will draw the item.

In response to the WM_DRAWITEM message, your application must perform the following actions before returning from processing the message:

1.Determine the type of drawing that is necessary. To do so, check the itemAction member of the DRAWITEMSTRUCT structure.

2.Draw the menu item appropriately, using the bounding rectangle and device context obtained from the DRAWITEMSTRUCT structure. Your application must draw only within the bounding rectangle. For performance reasons, Windows does not clip portions of the image that are drawn outside the rectangle.

3.Restore all GDI objects selected for the menu item's device context.

For example, if the menu item is selected, Windows sets the itemAction mem-ber of the DRAWITEMSTRUCT structure to ODA_SELECT, and sets the ODS_SELECTED bit in the itemState member. This is your application's cue to redraw the menu item so that the item indicates that it has been selected.