1,488
社区成员




Const MF_STRING = &H0&
Const MF_POPUP = &H10&
Const MF_BYPOSITION = &H400&
Private hMenu As Long
Private hSubMenu As Long
Private blnMenuCreated As Boolean
Private Declare Function CreateMenu Lib "user32" () As Long
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function SetMenu Lib "user32" (ByVal hWnd As Long, ByVal hMenu As Long) 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 String) As Long
Private Sub Form_Load()
Dim strPath As String
Dim strFile As String
Dim 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