The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
2 Common Controls Library &nbsp
Toolbars and Rebars: Quick Start 2 - - A CoolMenu in Your Application
Getting started with the vbAccelerator CoolMenu, Toolbar and Rebar Control. &nbsp

If you follow the steps described in Toolbars and Rebars: Quick Start 1, you will have seen how to get a fully working Rebar containing a toolbar. To add CoolMenu support to your application is a simple extension of this technique - except this time the Toolbar you add is linked to a menu.

The first thing you must note is that to support CoolMenus your application must not have a Visual Basic menu. If it has a VB menu, you need to convert that menu to one implemented using the PopupMenu ActiveX DLL instead, and once that is working you delete the VB menu.

Follow these steps
  1. Add PopupMenu ActiveX DLL to Your Application
    Choose Project->References in VB and then pick the vbAccelerator Popup Menu DLL from the list. If you cannot find this in the list, choose Browse and locate cNewMenu.DLL on your disk. You can download cNewMenu.DLL from the ActiveX PopupMenu page.

    Having referenced the DLL, you can now create an instance of it in the code. Add this code to your project's declares:

Option Explicit

Private WithEvents m_cMenu As cPopupMenu

  • Instantiate the PopupMenu object
    Do this by setting the m_cMenu object to a new instance and then setting the hWndOwner property of the PopupMenu object to the hWnd of the Form you are placing the CoolMenu on:

  • Set m_cMenu = New cPopupMenu

    ' Make sure you do this! If you do not, your menus will
    ' either not show or they will be very large and will not draw:
    m_cMenu.hWndOwner = Me.hWnd

  • Create Your Application's Menu
    Creating a menu using the cNewMenu DLL is shown in detail in many of the samples on this site. However, basically menus are added using the AddItem method. Whenever you want to add a sub-menu item to a given menu item, store the return value of the AddItem method in a Long variable. Then use this as the value in the lParentIndex parameter of the AddItem call you make to add the sub-menu item.

    The following code shows the creation of a simple menu containing the Edit and Help elements of Explorer, including the setting up of accelerator keys, keys to refer to the menu items (which are chosen according to the naming convention I used to use for standard VB menus) and icons (which are specified as 0-based numeric indexes of the icon within the ImageList):

  • ' Edit menu
    iP = .AddItem("&Edit", , , , , , , "mnuEditTOP")
    .AddItem "&Undo", , , iP, 4, , , "mnuEdit(0)"
    .AddItem "-", , , iP, , , , "mnuEdit(1)"
    .AddItem "Cu&t" & vbTab & "Ctrl+X", , , iP, 5, , , "mnuEdit(2)"
    .AddItem "&Copy" & vbTab & "Ctrl+C", , , iP, 6, , , "mnuEdit(3)"
    .AddItem "&Paste" & vbTab & "Ctrl+V", , , iP, 7, , , "mnuEdit(4)"
    .AddItem "-", , , iP, , , , "mnuEdit(5)"
    .AddItem "Select &All" & vbTab & "Ctrl+A", , , iP, , , , "mnuEdit(6)"
    .AddItem "&Invert Selection", , , iP, , , , "mnuEdit(7)"

    ' Help menu.
    iP = .AddItem("&Help", , , , , , , "mnuHelpTOP")
    .AddItem "&Contents", , , iP, 29, , , "mnuHelp(0)"
    .AddItem "-", , , iP, , , , "mnuHelp(4)"
    .AddItem "&About...", , , iP, , , , "mnuHelp(5)"

  • Create the CoolMenu
    The final initialisation stage is to create the CoolMenu and initialise the internal code to activate CoolMenu operations. This is done by passing the cPopupMenu object containing the menu to use into an unused cToolbar control via the CreateFromMenu method:

  • ' Create the CoolMenu:
    cToolbar1.CreateFromMenu m_cMenu

    Once you have created the toolbar, you can then add it to the form's Rebar control (or a cToolbarHost control if you want!)

  • Respond to Menu Events
    All Menu events will be raised to your code via the m_cMenu_Click event. This event passes the Index of the menu item which was clicked as the only parameter. You can interpret the Index as a key by using cPopupMenus ItemKey method. That completes the description of the steps you need to take to create a CoolMenu. Check out these two samples to get more details of the process for real world menus:

  • TopBack to top

    BackBack to CoolMenu Toolbar and Rebar Control

    Source Code - What We're About!Back to Source Code


    AboutContributeSend FeedbackPrivacy

    Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
    Last updated: 30 May 1999