Menus
PowerBASIC can readily create standard Windows menus. The process is similar to that of creating a dialog and then placing controls on it.

In PowerBASIC terminology a menu is called a "menu", a "bar, or a "menu bar". The top-level selections on a menu bar are called "popup menus". A popup menu may have dropdown selections, called "menu items",

PowerBASIC applications can also display "context menus", but the method requires the use of API to augment the PowerBASIC menu features. Displaying context menus is covered further down this page.

Using the terminology above, here is a description of the four primary PowerBASIC menu commands.

In addition to these four commands, PowerBASIC provides several other commands which help manage an application's menus. Here's a list of all MENU commands, categorized by the features they provide. Note that all of these commands start with "Menu".

Note that both the menu bar and popup menus are memory structures. One command is used to create the structures, including assigning them a windows handle. A second command is require to associate the structures with a parent (associating a menu bar with a parent dialog, and associating a popup menu with a parent menu bar).

The dropdown menu items are essentially controls and require that the PowerBASIC programmer assign them control IDs. Menu items are created and assigned to a parent popup menu using a single statement.

Menu Functions Listing
Here are one-line descriptions of all the menu functions.

Creating a Menu
There are four steps to creating a complete menu. They are:

  1. Create Menu Bar
        Dim hMenu As Dword
        Menu New Bar To hMenu
        
  2. Create Menu Popup (toplevel entry)
        'Create File + Children -------------------------
        Dim hMenuFile As Dword
        Menu New PopUp To hMenuFile
        Menu Add PopUp, hMenu, "&File", hMenuFile, %mf_enabled
        
  3. Add submenu to popup
        Menu Add String, hMenuFile, "&Open", %ID_Open, %mf_enabled
        
  4. Attach Menu Bar to Dialog
        Menu Attach hMenu, hDlg
        Dialog Show Modal hdlg
        

Example - Dialog + Menu
Here's the complete code for creating the following example of a dialog with a tool bar. The tool bar has two main items (File and Help). Each of those have submenus.

         

    #Compile Exe
    #Dim All
    %ID_Open = 201
    %ID_Save = 202
    %ID_SaveAs = 203
    %ID_Exit = 204

    %ID_Email = 901
    %ID_WebSite = 902
    %ID_Help = 903
    %ID_Update = 904
    %ID_About = 905

    Function PBMain()

    Dim hDlg As Dword
    Dialog New 0, "My Dialog",,, 150,100, %ws_overlappedwindow , To hDlg

    'Create Bar -------------------------
    Dim hMenu As Dword
    Menu New Bar To hMenu

    'Create File + Children -------------------------
    Dim hMenuFile As Dword
    Menu New PopUp To hMenuFile
    Menu Add PopUp, hMenu, "&File", hMenuFile, %mf_enabled

    Menu Add String, hMenuFile, "&Open", %ID_Open, %mf_enabled
    Menu Add String, hMenuFile, "&Save", %ID_Save, %mf_enabled
    Menu Add String, hMenuFile, "&Save As ..", %ID_SaveAs, %mf_enabled
    Menu Add String, hMenuFile, "E&xit", %ID_Exit, %mf_enabled

    'Create Help + Children -------------------------
    Dim hMenuHelp As Dword
    Menu New PopUp To hMenuHelp
    Menu Add PopUp, hMenu, "&Help", hMenuHelp, %mf_enabled

    Menu Add String, hMenuHelp, "&Email Author", %ID_Email, %mf_enabled
    Menu Add String, hMenuHelp, "&WebSite", %ID_WebSite, %mf_enabled
    Menu Add String, hMenuHelp, "&Online Help ..", %ID_Help, %mf_enabled
    Menu Add String, hMenuHelp, "&Online Update ..", %ID_Update, %mf_enabled
    Menu Add String, hMenuHelp, "&About ..", %ID_About, %mf_enabled

    'Attach Bar to Dialog -------------------------
    Menu Attach hMenu, hDlg

    Dialog Show Modal hdlg
    End Function

Menu Item States
Several of the statements use the state& property. Allowed values are:

    %MF_CHECKED - has checkmark 
    %MF_DISABLED - disabled (not grayed out)
    %MF_ENABLED - enabled
    %MF_GRAYED - disabled (grayed out)
    %MF_HILITE - highlighted
    %MF_SEPARATOR - separator (horizontal line)
    %MF_UNCHECKED - no checkmark

Even though state& can be set at the time a menu item is created, remember that the menu item is not visible until the menu item's parent popup menu is selected.

To apply more than one state, combine them with the OR operator.

Menu Statements Examples
Here are examples for each of the menu functions. The functions are listed in alphabetical order.

Context Menus
The PowerBASIC MENU statements, along with the TrackMenuPopup API function can be used to provide context menus - menus which appear when a dialog or control is right clicked.

To create a context menu, a programmer creates a menu item using Menu New Popup, but does not attach the new menu item to a menu bar. Sub-menu strings are added to the Popup just like they would be to any other menu bar popup menu item.

Finally, the TrackPopupMenu API is used to display the unattached Popup menu item at a specific location.

To activate a context menu, press and release the right mouse button. On release, Windows will send a WM_CONTEXTMENU message (as well as the WM_RBUTTONUP message).

The WM_CONTEXTMENU message is used to respond to a menu selection because it is specifically sent when the user selects a command item from a menu - whereas a WM_RBUTTONUP can occur independently of any menu selection.

Here's the basic code for creating a context menu.

    Case %WM_CONTEXTMENU
        x = Lo(Word,Cb.LParam) : y = Hi(Word, Cb.LParam)
        Menu New PopUp To hPopup
        Menu Add String, hPopup, "One", 101, %MF_Enabled
        Menu Add String, hPopup, "Two", 102, %MF_Enabled
        TrackPopupMenu hPopup, %TPM_LEFTALIGN, x, y, 0, Cb.Hndl, ByVal 0

The %TPM_LEFTALIGN value is one of several flags. MSDN says to use zero of more of the following flags.

Horizontal Alignment

Vertical Alignment

To Work Without A Parent Window

Specify Mouse Button

Animation <

For any animation to occur, the SystemParametersInfo function must set SPI_SETMENUANIMATION. Also, all the TPM_*ANIMATION flags, except TPM_NOANIMATION, are ignored if menu fade animation is on,

See the SPI_GETMENUFADE flag in SystemParametersInfo.

Use the TPM_RECURSE flag to display a menu when another menu is already displayed. This is intended to support context menus within a menu.

To have text layout from right-to-left, use TPM_LAYOUTRTL. By default, the text layout is left-to-right.

If you have any suggestions or corrections, please let me know.