Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Menu Controls

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • 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.

    • Menu New Bar - creates a menu bar (not yet associated with a dialog)
    • Menu New PopUp - adds a popup menu to the bar (no dropdown content)
    • Menu Add String - adds a menu item to a popup menu
    • Menu Attach - places a menu bar on a dialog

    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".

      • Menu
       New Bar, Attach 
      • PopUp
       New Popup, Add Popup, Add String 
      • Menu Items
       Get State, Set State, Set Text, Get Text 
      • Management
       Draw Bar, Delete 

    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.

    • MENU ADD POPUP - add popup to menu
    • MENU ADD STRING - add string to popup menu
    • MENU ATTACH - attach menu to dialog
    • MENU DELETE - delete string from popup menu
    • MENU DRAW BAR - redraw menu
    • MENU GET STATE - get state of menu item
    • MENU GET TEXT - get text from menu item
    • MENU NEW BAR - create new menu bar
    • MENU NEW POPUP - create new popup menu
    • MENU SET STATE - set state of menu item
    • MENU SET TEXT - set text of menu item

    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.

    • MENU ADD POPUP - add popup to menu
          Menu Add PopUp, hMenu, "&Text", hPop, %mf_enabled  'now on bar
          

      Before a popup can be added to an existing menu, it must be created first. See the complete example up above.

    • MENU ADD STRING - add string to popup menu
          Menu Add String,hMnu,"&Txt",201,%mf_enabled
      
          Menu Add String, hMnu, "&Txt", 201, %mf_enabled, _
               AT BYCMD pos%,CALL callback
          

      Adds a menu item to an existing Popup menu. Normally added at botom of list, but can be inserted at pos% or before pos% (if BYCMD) is used.

      Before a string can be added to a popup menu, the popup menu must be created and added to an existing menu bar. See the complete example up above.

    • MENU ATTACH - attach menu to dialog
          Menu Attach hMenu, hDlg 
          

    • MENU DELETE - delete string from popup menu
          Menu Delete hHelpMenu, BYCMD %ID_About   'deletes %ID_About
          

    • MENU DRAW BAR - redraw menu
          Menu Draw Bar hDlg     'use when menu altered by code
          

    • MENU GET STATE - get state of menu item
          Menu Get State hHelpMenu, BYCMD %ID_About TO state&
          

      -1 is returns if the specified menu item does not exist. If the menu item is a popup child menu, LO(BYTE, state&) contains the menu flags for the item, and HI(BYTE, state&) contains the number of items in the popup child menu. Otherwise the result is a bitmask containing one or more of the above mentioned flags combined together with the OR operator to form the bitmask.

      Use an equation like this: iResult = state& And %MF_CHECKED to test for the presence of a flag with the returned bitmask.

    • MENU GET TEXT - get text from menu item
          Menu Get Text hHelpMenu, BYCMD %ID_About TO txt$
          

    • MENU NEW BAR - create new menu bar
          Dim hMenu as DWORD
          Menu New Bar TO hMenu    'create menu, assign handle to hMenu
          

    • MENU NEW POPUP - create new popup menu
          Dim hPop As Dword
          Menu New PopUp To hPop   'create popup (not yet on menu bar)
          

      Note that this statement creates a popup menu, but does not associate it with an existing menu bar. The Menu Add Popup statement must be used for that purpose.

    • MENU SET STATE - set state of menu item
          state& = %MF_CHECKED OR %MF_ENABLED
          Menu Set State hHelpMenu, BYCMD %ID_About, state&
          

    • MENU SET TEXT - set text of menu item
          Menu Set Text hHelpMenu, BYCOMD %ID_About, "NewText"
          

    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

    • TPM_CENTERALIGN
      Centers the shortcut menu horizontally relative to the coordinate specified by the x parameter.

    • TPM_LEFTALIGN
      Positions the shortcut menu so that its left side is aligned with the coordinate specified by the x parameter.

    • TPM_RIGHTALIGN
      Positions the shortcut menu so that its right side is aligned with the coordinate specified by the x parameter.

    Vertical Alignment

    • TPM_BOTTOMALIGN
      Positions the shortcut menu so that its bottom side is aligned with the coordinate specified by the y parameter.

    • TPM_TOPALIGN
      Positions the shortcut menu so that its top side is aligned with the coordinate specified by the y parameter.

    • TPM_VCENTERALIGN
      Centers the shortcut menu vertically relative to the coordinate specified by the y parameter.

    To Work Without A Parent Window

    • TPM_NONOTIFY
      Does not send notification messages when the user clicks on a menu item

    • TPM_RETURNCMD
      Returns the menu item identifier of the user's selection in the return value

    Specify Mouse Button

    • TPM_LEFTBUTTON
      User can select menu items with only the left mouse button

    • TPM_RIGHTBUTTON
      User can select menu items with both the left and right mouse buttons

    Animation <

    • TPM_HORNEGANIMATION
      Animates the menu from right to left

    • TPM_HORPOSANIMATION
      Animates the menu from left to right

    • TPM_NOANIMATION
      Displays menu without animation

    • TPM_VERNEGANIMATION
      Animates the menu from bottom to top

    • TPM_VERPOSANIMATION
      Animates the menu from top to bottom

    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.