Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleList a form's menu items
Keywordsmenu, list menus
CategoriesControls
 
Use Windows menu functions to list the menu items.

The GetMenu API function returns the window handle for a form's menu. GetMenuItemCount returns the number of items in a menu. It returns zero if the menu is a command rather than a submenu. GetMenuString returns the menu's text and GetSubmenu gets the handles of a menu's submenus.

 
Private Sub Form_Load()
Dim txt As String
    
    GetMenuInfo GetMenu(hwnd), 0, txt
    Text1.Text = txt
End Sub

Private Sub GetMenuInfo(hMenu As Long, spaces As Integer, _
    txt As String)
Dim num As Integer
Dim i As Integer
Dim length As Long
Dim sub_hmenu As Long
Dim sub_name As String

    num = GetMenuItemCount(hMenu)
    For i = 0 To num - 1
        ' Save this menu's info.
        sub_hmenu = GetSubMenu(hMenu, i)
        sub_name = Space$(256)
        length = GetMenuString(hMenu, i, sub_name, _
            Len(sub_name), MF_BYPOSITION)
        sub_name = Left$(sub_name, length)

        txt = txt & Space$(spaces) & sub_name & vbCrLf
        
        ' Get its child menu's names.
        GetMenuInfo sub_hmenu, spaces + 4, txt
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated