In order to create an example menu for our DDT dialog, we will need one Double-word variable to hold the handle of the menu, and one for each of the popup menu levels that our menu will contain. In the following code, we will work towards creating a menu with two items on the menu bar (therefore two popup menus). In all, we will need three 32-bit variables:
DIM hMenu AS DWORD
DIM hPopup1 AS DWORD
To begin creating our menu, we use the MENU NEW BAR statement:
MENU NEW BAR TO hMenu
The value returned in hMenu is termed the menu handle. We use this handle to attach each of our popup menus. In order to create these popup menus, we will also need to create a handle:
MENU NEW POPUP TO hPopup1
Now we will "glue" our new popup menu to the menu bar. We do this using the MENU ADD POPUP statement, which results in an entry on the menu bar labeled "File", complete with a command accelerator:
MENU ADD POPUP, hMenu, "&File", hPopup1, %MF_ENABLED
The ampersand character in "&File" means that pressing ALT+F on the keyboard, in addition to the conventional mouse click, can open the menu. The hPopup1 parameter instructs the DDT engine to attach the menu to the menu bar (hMenu), and it is initially enabled.
Using the handle returned in hPopup1, we can begin adding items to the newly created popup menu. For each menu item that is a command (Open, Save, etc), we assign an ID value and specify the state of the item. When the user clicks on a menu item, the dialog Callback Function receives a %WM_COMMAND message.
In turn, we can then use the CBCTL function to obtain this ID value, to determine which menu item the user has selected. The state parameter allows us to specify whether the menu item is initially enabled, grayed (disabled), checked, or unchecked, etc.
Now let's begin to add items to our new popup menu:
MENU ADD STRING, hPopup1, "&Open", 201, %MF_ENABLED
MENU ADD STRING, hPopup1, "&Exit", 202, %MF_ENABLED
MENU ADD STRING, hPopup1, "-", 0, 0
Here we created two items that form part of our first popup menu. These menu items have the ID values 201 and 202 respectively, and each is initially enabled. The third item is a special type of menu item, called a separator. A separator is a horizontal line within the menu, and can be used to visually separate groups of menu items from each other within the same popup menu.
We recommend using equates for the ID parameters, as they make your code more readable and maintainable. For this example we use hard-coded values simply for clarity. |
Let's add an additional popup menu to this original popup menu, just to demonstrate how simple it can be to create menus with multiple "layers". First, we will need to create a new popup menu handle:
MENU NEW POPUP TO hPopup2
Using this new popup menu handle, we attach menu items in exactly the same order as we did as before:
MENU ADD STRING, hPopup2, "Option &1", 403, %MF_ENABLED
MENU ADD STRING, hPopup2, "Option &2", 404, %MF_ENABLED
Now comes the tricky part... we must attach this new menu to the previous menu, rather than the menu bar:
MENU ADD POPUP, hPopup1, "&More Options", hPopup2, %MF_ENABLED
This statement "glues" the second popup menu to the end of the first popup menu. If we changed the hPopup1 parameter to hMenu, the popup menu would appear on the menu bar. Making multiple level menus is that simple!
With our menu created, we then attach the menu to our DDT dialog:
MENU ATTACH hMenu, hDlg
This code is almost self-explanatory - DDT is instructed to attach our menu structure to the dialog handle contained in hDlg. The only thing left now is to show the dialog, complete with a menu.
DIALOG SHOW MODAL hDlg, CALL DlgProc TO lResult
See Also