How to Create Pop-up Menus on a Visual Basic Form ID Number: Q71279
1.00 WINDOWS
Summary:
Visual Basic can call the Windows API function TrackPopupMenu to display a specified menu at the location on the screen that the user clicks with the mouse.
This information applies to Microsoft Visual Basic Programming System version 1.00 for Windows.
More Information:
The TrackPopupMenu function displays a “floating” pop-up menu at the specified location and tracks the selection of items on the pop-up menu. A floating pop-up menu can appear anywhere on the screen. The hMenu parameter specifies the handle of the menu to be displayed; the application obtains this handle by calling GetSubMenu to retrieve the handle of a pop-up menu associated with an existing menu item.
TrackPopupMenu is defined as follows
TrackPopupMenu (hMenu%,wFlags%, X%, Y%, rRes%, hwnd%, lpRes&)
where:
hMenu% - Identifies the pop-up menu to be displayed. wFlags% - Not used. This parameter must be set to zero. x% - Specifies the horizontal position in screen coordinates of the left side of the menu on the screen. y% - Specifies the vertical position in screen coordinates of the top of the menu on the screen. nRes% - Is reserved and must be set to zero. hWnd% - Identifies the window that owns the pop-up menu. lpRes& - Is reserved and must be set to NULL.
The supporting Windows API functions needed to support the arguments to TrackPopupMenu are:
GetMenu(hWnd%)
hWnd% - Identifies the window whose menu is to be examined.
GetMenu returns a value that identifies the menu. The return value is NULL if the given window has no menu. The return value is undefined if the window is a child window.
GetSubMenu(hMenu%, nPos%)
hMenu% - Identifies the menu. nPos% - Specifies the position in the given menu of the pop-up menu. Position values start at zero for the first menu item.
GetSubMenu returns a value that identifies the given pop-up menu. The return value is NULL if no pop-up menu exists at the given position.
To create a pop-up menu within Visual Basic, define a menu system with the Menu Design window. The following is an example of a menu system:
Caption CntlName Indented ——- ——– ——–
File M_File No New M_New Once Open M_Open Once Close M_Close Once Exit M_Exit Once Help M_Help No
Within the general-declaration section of your Code window, declare the following:
Declare Function TrackPopupMenu% Lib “user”(ByVal hMenu%, ByVal wFlags%, ByVal X%, ByVal Y%, ByVal r2%, ByVal hwnd%, ByVal r1&) Declare Function GetMenu% Lib “user” (ByVal hwnd%) Declare Function GetSubMenu% Lib “user” (ByVal hMenu%, ByVal nPos%)
Note: Each Declare statement above must be located on just one line.
Place the following code in the form’s MouseUp event procedure:
Sub Form1_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single) ’ The above Sub statement must be concatenated onto one line. Const PIXEL = 3 Const TWIP = 1 ScaleMode = PIXEL InPixels = ScaleWidth ScaleMode = TWIP IX = (X + Left) (ScaleWidth InPixels) IY = (Y + (Top + (Height - ScaleHeight - (Width - ScaleWidth)))) (ScaleWidth InPixels) ’ The above IY = … statement must be concatenated onto one line. hMenu% = GetMenu(hwnd) hSubMenu% = GetSubMenu(hMenu%, Button - 1) R = TrackPopupMenu(hSubMenu%, 0, IX, IY, 0, hwnd, 0) End Sub
When you run the program, clicking anywhere on Form1 will display the first menu on your menu bar at that location.
Reference:
- “Programming Windows: the Microsoft Guide to Writing Applications for Windows 3,” by Charles Petzold (published by Microsoft Press, 1990)
- “Microsoft Windows 3.0 Software Development Kit: Reference Volume 1”
- The WINSDK.HLP file shipped with Microsoft Windows 3.0 Software Development Kit.