|2||Common Controls Library|| |
|Toolbars and Rebars: Quick Start 2 - - A CoolMenu in Your Application|
|Getting started with the vbAccelerator CoolMenu, Toolbar and Rebar Control.|| |
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
Private WithEvents m_cMenu As cPopupMenu
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
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):
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)"
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:
Once you have created the toolbar, you can then add it to the form's Rebar control (or a cToolbarHost control if you want!)
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:
Back to top
Back to Source Code