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 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
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 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. ' 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 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
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 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() Add menu items to the newly created popup menus by using APPENDMENU APPENDMENU(hPopup,"Open",MF_STRING,1) 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. |