Inserting/Deleting Menu Bar Items

The insertion of an item is achieved by sending a message of class MM_INSERTITEM to the system menu (or to the menu bar or appropriate pulldown menu) using the WinSendDlgItemMsg() function. The menu item information is placed into a data structure of type MENUITEM, as shown in Figure "Dynamically Inserting a Menu Bar Item".

In the example shown above, an item is to be inserted into a standard "File" pulldown menu. The menu item information is placed into the data structure MenuItem, and the text to appear in the pulldown menu is contained in the string variable szItemText.

Note however, that the menu bar is a child of the frame window, and the pulldown menu is a child of the menu bar. In order to successfully pass the message, the handle of the frame window must be obtained, and used to obtain the handle of the menu bar window. This handle is then used in the WinSendDlgItemMsg() call, along with the window identifier MN_FILE, to send the MM_INSERTITEM message to the pulldown menu. The frame window handle is obtained using the WinQueryWindow() function, and the QW_PARENT attribute causes the function to return the handle of the client's parent (that is, the frame window).

Once the frame window's handle is obtained, the various attributes of the MenuItem structure are initialized. An MM_INSERTITEM message is then sent directly to the pulldown menu.

Deletion of an item is accomplished in a similar fashion using a message of class MM_DELETEITEM. Both message classes and the MENUITEM data structure are described in the IBM OS/2 Version 2.0 Presentation Manager Reference.

Inserting/Deleting Separators

Where more than one logical group of items is contained within a single pulldown menu, these groups should be divided by a separator, which is a horizontal bar appearing between the last item of one group and the first item of the next, in order to provide a visual indication of the group's distinct identities. A separator may be defined within the resource script file (see Presentation Manager Resources) or may be inserted and deleted dynamically by the application. This dynamic insertion/deletion is carried out in a similar manner to that already described for "normal" pulldown menu items. However, the afStyle field in the MENUITEM structure is specified as MIS_SEPARATOR, and the text of the item is set to NULL. In such cases, an item identifier is not required for the separator, although it is recommended since an identifier must be specified in order to delete the separator from the pulldown menu should this be necessary at a later time.

Deletion of a separator is achieved in exactly the same manner as that described for a normal pulldown menu item, using a message of class MM_DELETEITEM and specifying the identifier of the separator to be deleted.

Inserting/Deleting Pulldown Menus

The technique for dynamically inserting a pulldown menu or cascade pulldown into the menu bar is basically similar to that already described for inserting a menu bar or pulldown menu item. An example is given in Figure "Dynamically Inserting a Pulldown Menu".

The difference lies in the fact that a pulldown menu or cascade pulldown requires a menu template to be reserved in memory in order to contain the items that will subsequently be inserted into the pulldown. This template is created using the WinCreateMenu() function, which returns a handle to the menu template. This handle is then used in the hwndSubmenu field of the MENUITEM structure.


[Back: The Menu Bar]
[Next: Enabling/Disabling Items]