How to use %di with menus

In this section:

There is no change in the provision for %ac (Accelerator Key). These cannot be read from accelerator tables in a resource.

It is possible to use the existing mechanism to provide menus (%mn (Menu), %pm (Popup Menu) and %sm (Modify System Menu)) or enhanced menus but only if the menu is fully defined before using %di. When using %mn in this way it is a good idea to add a dummy menu to the resource script and to attach this to the dialog in the Dialog Properties. In this way the positioning of dialog components will be correctly adjusted to allow for the menu bar.

A simpler approach for standard and popup menus (%mn (Menu) and %pm (Popup Menu)) is to construct the menu in the resource editor as follows. This approach cannot be used for system menus (%sm (Modify System Menu)).

For simplicity use the default id values that are automatically generated by the menu editor. Accelerator key information can be added after the item text by using a tab character (\t) and this information is automatically added to the accelerator key table (note that accelerator key action is not automatically disabled when the coresponding menu item is disabled).

Help strings can be inserted into the "Prompt" edit box.

A standard menu (%mn (Menu)) constructed in this way must be attached to the dialog as one of the "Dialog Properties". The first use of %pm (Popup Menu) in the program code for a dialog must provide the menu identifier:


In the menu editor, a popup menu must start with a dummy top level item (call this Pop1 etc., say). The actual popup menu will be the submenu of Pop1. (Note that you can use the right mouse button in the menu editor and select "View as popup" but this setting is neither appropriate nor helpful in this context.)

When a standard menu or popup menu is constructed in the resource, each menu item id must be linked to a callback function in the program. The structure of the menu and the item text are given in the resource.

In the program, %mn and %pm are used in a different way from normal. The format codes must appear after %di. Here is an example:

wr=winio@('%di&', IDD_DIALOG1)
wr=winio@('%mn&', ID_FILE_OPEN,open_cb)

In this context %mn and %pm always take an integer id and a callback function. The id must be unique to the memu item. Even if a popup menu item duplicates the action of a standard menu item, they must have different id values. This also applies to menus in different windows if an appliction has more than one window open.

A tilde (~) is added for a grey control variable and a grave is added for a check control variable.

A grey control must preceed a check control in the argument list.

A question mark can be added for help text as an alternative to using the menu resource.....

wr=winio@('%?mn[Quit the application]&',ID_FILE_EXIT,'EXIT')

A menu that is defined as a resource can also include small bitmaps. When used, the bitmap replaces the check mark in a menu item and normally has the same size (12x12 pixels for "Small Fonts" display). If the id is the same as the id for a bitmap in the resource then the bitmap is automatically loaded into the item.

Menu items defined in the program can be ommitted from all menu resources and dynamically inserted after the window has been defined. Two routines can be used in this context:


INTEGER progID,Before

text is the character string for the new menu item.

progID is the id corresponding to a %mn item in the program linking to a callback function and possibly grey and check controls. If progID=0 a separator is inserted.

rscID is the id of an item that is in the resource (this must not be a separator and the popup menu cannot be empty).

If rscID=progID then the item is replaced and its grey state is toggled only if it does not have a grey control. To delete a separator set Before=1 and set progID for the item after the separator otherwise set Before=0.

Accelerator keys, help strings and bitmaps can also be employed in this context.

One approach is to use a place marker in the resource which is initially greyed (e.g. it might be set in the resource to "Recent files"). For a place marker, use INSERT_MENU_ITEM@ with Insert=0 to change the first item added. Also, instead of removing the last of the items that have been added, use INSERT_MENU_ITEM@ again (with the text set for example to "Recent files") and disable the item.



Copyright © 1999-2021 Silverfrost Limited