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
m(1).Caption = "我是新菜单"
m(1).Visible = True
如想全面控制,参考:http://bbs.csdn.net/topics/390213637
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