Adding a Menu Bar

The following sample contains a menu bar with only one submenu named Alignment. When you run the sample and select Alignment, the pull-down menu is displayed. The choices in the pull-down menu are Left, Center, and Right. When you select one of the choices, the text string in the client window aligns to the selected position and a check mark appears beside the selected item.

  1. The following code, from the Windows ahellow3.rc file, defines the text for the menu bar and its associated pull-down menu:
    WND_MAIN  MENUEX 
      BEGIN
        POPUP     "&Alignment"               ,  MI_ALIGNMENT
          BEGIN
            MENUITEM  "&Left"                    ,  MI_LEFT
            MENUITEM  "&Center"                  ,  MI_CENTER
            MENUITEM  "&Right"                   ,  MI_RIGHT
          END
      END
    

    The resource file for the OS/2 platform would be as follows:

    MENU WND_MAIN                                   //Main window menu bar         
      BEGIN                                                                      
            SUBMENU "~Alignment", MI_ALIGNMENT      //Alignment submenu            
              BEGIN
                MENUITEM "~Left",       MI_LEFT     //Left menu item - F7 Key      
                MENUITEM "~Center",     MI_CENTER   //Center menu item - F8 Key    
                MENUITEM "~Right",      MI_RIGHT    //Right menu item - F9 Key     
              END
      END
    

    To assist keyboard users of your application, underline the mnemonic in each menu item. For Windows, use the ampersand (&) and for OS/2, use the tilde (~).

  2. Use a command handler to implement the functions to be performed as a result of a user selecting a menu item. To place a check mark beside the appropriate menu item, create an IMenuBar object to represent the application menu bar.

    The following code, from the ahellow3.hpp file, shows the AHelloWindow class containing an ACommandHandler object, which is derived from the ICommandHandler class:

      private:                                                                
        IMenuBar      menuBar;                                                
        IStaticText   statusLine;                                             
        IStaticText   hello;                                                  
        IInfoArea     infoArea;                                               
        ACommandHandler commandHandler;                                
    
    };                                                                        
    #endif                                                                    
    
  3. The code in this section is from the ahellow3.cpp file.

    The following code creates the mainWindow object from the AHelloWindow class:

      AHelloWindow mainWindow (WND_MAIN);                                     
    
    

    This code shows the first part of the AHelloWindow constructor. The menu bar defined in the resource file is associated with this AHelloWindow object.

    AHelloWindow :: AHelloWindow(unsigned long windowId)               
      : IFrameWindow(IFrameWindow::defaultStyle() |                    
                     IFrameWindow::minimizedIcon,                      
                     windowId)                                                
       ,menuBar(WND_MAIN, this)                                               
       ,statusLine(WND_STATUS, this, this)                                    
       ,hello(WND_HELLO, this, this)                                          
       ,infoArea(this)                                                        
       ,commandHandler(this)                                                  
    {                                                                         
    
    

    The following code attaches the command handler to the frame window:

      commandHandler.handleEventsFor(this);                                   
    
    

    When the user selects a menu item, the ACommandHandler::command function is called to select the appropriate frame window function to call as follows:

      switch (cmdEvent.commandId()) {                                         
        case MI_CENTER:                                                       
          frame->setTextAlignment(AHelloWindow::center);                   
          break;                                                              
    
    

    This code from the AHelloWindow::setAlignment function places the check mark beside the appropriate menu item:

      case center:                                                            
        hello.setAlignment(                                                   
          IStaticText::centerCenter);                                  
        statusLine.setText(STR_CENTER);                                       
        menuBar.checkItem(MI_CENTER);                                         
        menuBar.uncheckItem(MI_LEFT);                                         
        menuBar.uncheckItem(MI_RIGHT);                                        
        break;                                                                
    



Menus


Creating a Frame Window
Adding Menus to your Application


IMenu
IMenuBar
IPopUpMenu
ISubMenu
ISystemMenu
IWindow