Dynamic two or more levels sub-menu generation in vb6

creationdynamicsubmenuvb6

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!

Best Solution

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&


  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.

Related Question