Reputation: 801
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!
Upvotes: 3
Views: 6410
Reputation: 41
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.
Upvotes: 4
Reputation: 49218
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
Upvotes: 2