3

朋友,告诉我如何在VB6运行时生成多于1​​级的子菜单?简要解释一下?有什么具体的控制吗?但我不想使用外部控件!

4

2 回答 2

4

您可以使用 API 函数创建多个级别的子菜单

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"

显示

  If Button = vbRightButton Then
    Dim P As POINTAPI
    GetCursorPos P
    TrackPopupMenu hmenu, 0, P.X, P.Y, 0, hwnd, 0

菜单在TrackPopupMenu被调用之前不会显示。它的返回值可以指示选择了哪个(如果有)菜单项。例如,如果选择了“Menu1”,它可能会返回“107”。

于 2011-08-09T09:10:45.800 回答
2

您可以使用标准的 VB 菜单来做到这一点,但是由于您必须使用控件数组,因此您必须在设计时(通常不可见)创建第一个原型菜单Index = 0(例如mnuFoo(0))。您现在可以动态加载新项目。

Call Me.Load(mnuFoo(1)) ' New array member (index 1) '
With mnuFoo(1)
    .Visible = True ' Make it visible
    ' --- Do some settings
End With
于 2009-05-23T17:43:39.577 回答