F.A.Q. - How do I add a menu to my shaped form?
The most usefull menu to add to a shaped form would be a popup menu, but even in this case there is a problem making it in a normal way because if there is any menu on a form, even if it is not visible (as in the case of a popup menu) the titlebar appears, which shifts the whole thing out of line. There are two ways to solve this problem. The first is simply to offset the shape to compensate for having a titlebar. To do this see the FAQ item on adding an icon to the task bar entry.
The other method is to create a new form, and host the menu system on that. To do this, create the form, give it a name such as frmMenuHolder and set its Visible property to False. You can then create the menu system on this form. Each popup menu should be the subitems of a top level menu item which does not appear. An example of this is shown in the picture on the right, in which there are two separate popup menus, each having its own top level holder. The code for the menus must also go in this form, even if it is just to call public functions in the shaped form. |
Note that this form will be loaded automatically when the menu is called, so it must be unloaded when the program exits. Putting the following line in the shaped form's Unload event will make sure this happens.
Unload frmMenuHolder
There are two possible ways of calling a popup menu. You can either have it appear whenever the right mouse button is clicked, or you can have it bound to a button so it will appear when that button is clicked, and next to or below that button (rather than wherever the mouse pointer happens to be).
To make it appear on right clicking put the following code in the shaped form's MouseDown event, before the ReleaseCapture line (added by VBSFC to allow form dragging):
If Button = vbRightButton Then
Me.PopupMenu frmMenuHolder.PopupHolder1
Exit Sub
End If
To make it appear when a button is clicked simply put in the click event of the button the following line of code:
Me.PopupMenu frmMenuHolder.PopupHolder1, vbPopupMenuLeftAlign, btnMenu.Left, btnMenu.Top + btnMenu.Height
Where btnMenu is the name of the button showing the menu. Note that this makes the menu appear left aligned at the bottom left of the button, but you can change this to make it appear wherever you choose.