Creating and Using Menus

Top  Previous  Next

The Emergence BASIC compiler supports creating menus through both high level macro commands and low level API functions. The high level commands are an easy way to add an unlimited menu structure to your window or dialog. Context menus are also supported through the use of the high level menu creation macros. The macro statements closely follow the format used in menu resources to make converting between the two easier.

 

Adding a menu bar to a window or dialog

Begin creation of the menu bar with the BEGINMENU command. The window or dialog must be currently open.
 

BEGINMENU myWindow

Add menus to the menu bar with the MENUTITLE statement
 

MENUTITLE "&File"

Add items to the menu with the MENUITEM statement. The MENUITEM statement requires three parameters. The name of the item, any style flags and the ID of the menu item that will be used when your program receives menu messages. The ID cannot be 0.
 

MENUITEM "&Open",0, 1
MENUITEM "Close", 0, 2
MENUITEM "Save", 0, 3
SEPARATOR
MENUITEM "Quit", 0, 4
ENDMENU

End the menu bar definition with the ENDMENU statement. Every BEGINMENU statement must be paired with a matching ENDMENU statement. The "&" in the item definitions specify the letter to be underlined in the item text. The item name string can contain any standard string escape sequences. Use the SEPARATOR statement to create the item separator line in the menu.

The style flags can a combination of @MENUCHECK for a menu that is initially checked and @MENUDISABLE for a menu that is initially disabled.

 

Creating submenus (popup menus)

Within a menu definition you can create unlimited levels of popup menus using the BEGINPOPUP and ENDPOPUP pair.
 

BEGINMENU myWindow
    MENUTITLE "&File"
        BEGINPOPUP "Load"
            MENUITEM "Text file", 0, 100
            MENUITEM "Word document", 0, 101
            MENUITEM "RTF File", 0, 102
        ENDPOPUP
        MENUITEM "Close", 0, 2
        MENUITEM "Save", 0, 3
        MENUITEM "Quit", 0, 4
    MENUTITLE "Edit"
        MENUITEM "Undo", 0, 255
        MENUITEM "Redo", 0, 256
ENDMENU

 

Inserting menus

To insert a new menu into an existing one use BEGININSERTMENU instead of BEGINMENU.  BEGININSERTMENU accepts two parameters, the window or dialog and the position to insert the new menu into. Position is zero based with 0 inserting before the first menu title, 1 inserting before the second, etc. BEGININSERTMENU must be used on MDI frame windows as there is already an existing menu containing the window tiling commands.
 

BEGININSERTMENU myWindow, 2
    MENUTITLE "Help"
        MENUITEM "About", 0, 300
ENDMENU

Creating context menus

Use the CONTEXTMENU statement in place of BEGINMENU to create and show a right-click context menu in your window or dialog. The x and y coordinates specified in the CONTEXTMENU statement are client coordinates. It is common to retrieve the coordinates from the @MOUSEX and @MOUSEY variables after an @IDRBUTTONUP message has been received.
 

'
SELECT @MESSAGE
CASE @IDRBUTTONUP
    CONTEXTMENU mywin,@MOUSEX,@MOUSEY
        MENUITEM "Color",0,99
        MENUITEM "Clear",0,1
        BEGINPOPUP "Line Size"
            MENUITEM "1",(linesize = 1) * @MENUCHECK,2
            MENUITEM "2",(linesize = 2) * @MENUCHECK,3
            MENUITEM "3",(linesize = 3) * @MENUCHECK,4
            MENUITEM "4",(linesize = 4) * @MENUCHECK,5
        ENDPOPUP
    ENDMENU

The MENUTITLE statement is not allowed in a context menu block as there is only one menu. It is important to remember that the context menu will be shown immediately and your message handler will receive menu messages from the context menu in the same manner as a normal menu.

Item addition and removal

Menu items can be added to an existing menu title by using the ADDMENUITEM command. The position specifies the zero based position of the menu to add the item to.
 

ADDMENUITEM myWindow, 3, "Register Online", 0, 75

Remove an existing menu or menu item by using the REMOVEMENUITEM command. The position specifies the zero based position of the menu or menu item to remove. If an ID of 0 is specified then the entire menu specified by position is removed
 

REMOVEMENUITEM myWindow, 3, 75

Menu appearance

Control the appearance of a menu or menu item using the ENABLEMENU, ENABLEMENUITEM and CHECKMENUITEM statements. Click on the preceding links for descriptions.
 

'Disable the file menu
ENABLEMENU myWindow, 0, FALSE
'Check a menu item
CHECKMENUITEM myWindow, 4
'Disable a menu item
ENABLEMENUITEM myWindow, 255, FALSE

Handling menu messages

Once you define menus for your window, your programs handler subroutine will receive an @IDMENUPICK message whenever an item is selected. The ID of the menu will be returned in the @MENUNUM system variable.

Before the menu is displayed to the user an @IDMENUINIT message is sent to your handler to allow the program to make any modifications to the menu before it is shown. The modifications can include addition, removal, checking and disabling of menus and menu items.

Complete menu example:

REM define a window variable
DEF w1 as WINDOW
REM a variable to keep track of a checked menu
DEF bChecked as INT:bChecked = FALSE
REM open the window
OPENWINDOW w1,0,0,350,350,@MINBOX|@MAXBOX|@SIZE,0,"Simple Window",&main
REM define the menus
BEGINMENU w1
MENUTITLE "Option"
    MENUITEM "Print", 0, 1
    MENUITEM "Quit", 0, 2
    SEPARATOR
    MENUITEM "Check me", 0, 3
ENDMENU
REM print a message
PRINT w1,"Hello World "
REM when w1 = 0 the window has been closed
WAITUNTIL w1 = 0
END
 
SUB main
SELECT @MESSAGE
    CASE @IDCLOSEWINDOW
        REM closes the window and sets w1 = 0
        CLOSEWINDOW w1
    CASE @IDMENUPICK
        SELECT @MENUNUM
            CASE 1: ' user selected Print
                PRINTWINDOW w1
            CASE 2: ' user selected Quit
                CLOSEWINDOW w1
            CASE 3: ' toggle a checkmark
                bChecked = (bChecked = FALSE)
                CHECKMENUITEM w1, 3, bChecked
        ENDSELECT
ENDSELECT
RETURN
ENDSUB

 

Keyboard Accelerators

Keyboard accelerators, sometimes known as shortcut keys, can be added for any menu using the ADDACCELERATOR command.  The format of the command is:

ADDACCELERATOR window|dialog, fvirt, key, cmd

fVirt is a flag describing whether the key needs to be combined with the SHIFT, ALT or CTRL key in order to activate the accelerator. Key can be and ASCII key code or one of the virtual key codes listed in the appendix. Cmd is the menu ID.

Example:

BEGINMENU win
    MENUTITLE "&File"
    MENUITEM "&Load File\tCtrl+L",0,1
    MENUITEM "&Save\tCtrl+S",0,2
    MENUITEM "&Print\tAlt+P",0,4
    MENUITEM "&Quit\tCtrl+C",0,3
    MENUTITLE "&Options"
    MENUITEM "Change Font\tF4",0,5
ENDMENU
 
'add our keyboard accelerators
ADDACCELERATOR win,@FCONTROL|@FVIRTKEY,ASC("L"),1
ADDACCELERATOR win,@FCONTROL|@FVIRTKEY,ASC("S"),2
ADDACCELERATOR win,@FCONTROL|@FVIRTKEY,ASC("C"),3
ADDACCELERATOR win,@FALT|@FVIRTKEY,ASC("P"),4
ADDACCELERATOR win,@FVIRTKEY,0x73,5:'F4 key changes font

Low level API menu functions

The low level functions are part of the standard command set and are for advanced menu creation. They are used by the higher level macros to actually create and attach the menus to the window or dialog. The low level functions are equivalents to the Windows API functions for manipulating menus and only differ in that they understand EBASIC WINDOW and DIALOG variables and check for NULL handle values.

In order to construct a menu bar you first need to call CreateMenu with no parameters.
 

hMenu = CreateMenu( )

The handle returned is of type UINT and is a standard Windows HMENU.  Once the main menu is created you construct the various levels by using CreateMenu with the parameter set to 1 to create the dropdown menu titles or embedded popup menus. Attach the popup menu to the menu bar using APPENDMENU.
 

hMenu = CreateMenu()
hPopup = CreateMenu(1)
APPENDMENU(hMenu, "File", MF_POPUP|MF_STRING, hPopup)

Add menu items to the newly created popup menus by using APPENDMENU
 

APPENDMENU(hPopup,"Open",MF_STRING,1)
APPENDMENU(hPopup,"Quit",MF_STRING,2)

And finally replace the menu bar in the window or dialog with your newly created one by calling SETMENU
 

SETMENU win, hMenu

See any Windows API guide for more complex examples on using menu creation functions. Constants for APPENDMENU can be found in the API Viewer or on MSDN.