Friends, Tell me how to generate more than 1 levels of sub-menu in VB6 at runtime? Explain in brief? Any specific controls are there? But i dont want to use external controls!
Asked
Active
Viewed 6,096 times
3
-
If you had any code, provide me! – Srinivasan MK May 23 '09 at 17:21
2 Answers
4
You Can Create More than One level of Submenu by using API function
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hmenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hmenu As Long, ByVal wFlags As Long, ByVal X As Long, ByVal Y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hmenu As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim hmenu As Long, hSubMenu As Long
Private Const MF_STRING = &H0&
Private Const MF_SEPARATOR = &H800&
Private Const MF_POPUP = &H10&
hSubMenu = CreatePopupMenu
AppendMenu hSubMenu, 0, 121, "Sub Menu1"
AppendMenu hSubMenu, 0, 122, "Sub Menu2"
hmenu = CreatePopupMenu
AppendMenu hmenu, 0, 107, "Menu1"
AppendMenu hmenu, 0, 106, "Menu2"
AppendMenu hmenu, MF_POPUP, hSubMenu, "Menu3"
AppendMenu hmenu, MF_POPUP, hSubMenu, "Menu4"
AppendMenu hmenu, 0, 101, "Menu5"
To display
If Button = vbRightButton Then
Dim P As POINTAPI
GetCursorPos P
TrackPopupMenu hmenu, 0, P.X, P.Y, 0, hwnd, 0
The menu is not displayed until TrackPopupMenu
is called. Its return value can indicate which (if any) menu item was selected. e.g., it could return '107' if "Menu1" was selected.
-
2
-
@bjan there are no events - you just check the value of TrackPopupMenu. I've noted this in the answer. – StayOnTarget Mar 05 '19 at 14:42
2
You can do this with standard VB-menus, but since you'll have to use control arrays, you have to create a first prototype menu with Index = 0
(e.g. mnuFoo(0)
) at design time (usually invisible). You can now load new items dynamically.
Call Me.Load(mnuFoo(1)) ' New array member (index 1) '
With mnuFoo(1)
.Visible = True ' Make it visible
' --- Do some settings
End With

Dario
- 48,658
- 8
- 97
- 130
-
I need more than one level of menu. ie., a submenu and its submenu. Complete code fragment needed! – Srinivasan MK May 24 '09 at 04:27
-