在VB6中如何动态的生成菜单,不借助于系统提供的菜单编辑器。

解决方案 »

  1.   

    最容易的应该是使用控件数组,先在菜单编缉器中生成一个菜单,名称为m,索引为0,则可用如下代码动态添加和管理:Load m(1)
    m(1).Caption = "我是新菜单"
    m(1).Visible = True
    如想全面控制,参考:http://bbs.csdn.net/topics/390213637
      

  2.   


    Const MF_STRING = &H0& Const MF_POPUP = &H10& Const MF_BYPOSITION = &H400&   Private hMenu As LongPrivate hSubMenu As LongPrivate blnMenuCreated As Boolean  Private Declare Function CreateMenu Lib "user32" () As LongPrivate Declare Function CreatePopupMenu Lib "user32" () As LongPrivate Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As LongPrivate Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As LongPrivate Declare Function SetMenu Lib "user32" (ByVal hWnd As Long, ByVal hMenu As Long) As LongPrivate Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long  Private Sub Form_Load() Dim strPath As StringDim strFile As StringDim i As Long    Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2     Show           strPath = InputBox("请输入一个文件夹路径,以便制作菜单", , "C:\Windows")     If Len(Dir(strPath, vbDirectory)) = 0 Then Exit Sub    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"           hMenu = GetMenu(hWnd)     If hMenu = 0 Then        hMenu = CreateMenu()         blnMenuCreated = True    End If    hSubMenu = CreatePopupMenu()     Call AppendMenu(hMenu, MF_STRING Or MF_BYPOSITION Or MF_POPUP, hSubMenu, strPath)           strFile = Dir(strPath & "*.*")     Do Until Len(strFile) = 0         i = i + 1         Call AppendMenu(hSubMenu, MF_STRING Or MF_BYPOSITION, i, strFile)         strFile = Dir     Loop          If blnMenuCreated Then Call SetMenu(hWnd, hMenu) End Sub  Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)     Call DestroyMenu(hSubMenu)     If blnMenuCreated Then Call DestroyMenu(hMenu) End Sub
      

  3.   

    用API GetMenu获得菜单,然后调用AppendMenu添加菜单项目。
      

  4.   

    要想菜单有个性,当然是用photoshop画,然后放到picturebox里面,再捕捉鼠标事件。