Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Menu - Identifying a menu's Hierarchical order 2

Status
Not open for further replies.

LostInCode

Programmer
Feb 4, 2004
216
DE

I am looking for a method to find the complete Hierarchy that a VB Menu item belongs to.

Example:
Code:
-mnuItem_1
        - SubItem_A
                 -SubItem_A1
                 -SubItem_A2
        - SubItem_B
                 -SubItem_B1

With-out having to rename the menu items, or create a seperate hard-coded list, how can I find out with code at runtime that:

SubItem_A1 is an element of SubItem_A, and SubItem_A is an element of mnuItem_1

?
 
'without having to rename the menu items'

Shame. That would have made things easy.

mnuA
mnuAA

MnuB
mnuBA
mnuBAA
mnuBAB

Did you know that menus can have a tag?
But the menu editor does not expose it.
You can do it using a text editor:

Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 165
ClientTop = 735
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.Menu dscds
Caption = "dsc"
Tag = "AA"
End
End

So, in this way, you could use the tag property of the clicked item to determine its hierarchical position.
 
Thank you for responding.

Yes, this I know.
You can also set the Tag property in code.

But then I would need to place the complete hierarchy into the tag property.

Then I could just go ahead and hard-code it.
This is what I am trying to prevent.

 
perhaps this might help,

I used :

Form1
-containing- Picture1
-containing- Frame1
-containing- Command1
-containing- Command2
-containing- Text1 [multiLine = True]

Form Code :

Dim txt1 As String * 100

Private Sub Command2_Click()
Dim l As Long
Dim r As Long
Dim i As Integer
Dim txt2 As String

Text1.Text = ""
l = Command1.hwnd
Do While l <> 0
txt1 = ""
r = GetClassName(l, txt1, 100)
i = InStr(1, txt1, Chr(0)) - 1
txt2 = Left(txt1, i) + " : " + Str(l)
Text1.Text = Text1.Text + vbNewLine + txt2
l = GetParent(l)
Loop
End Sub


Module1

Module Code :

Public Declare Function GetParent _
Lib "user32" _
( _
ByVal hwnd As Long _
) _
As Long

Public Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" _
( _
ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long _
) _
As Long

Result :

ThunderCommandButton : 3081522
ThunderFrame : 3146880
ThunderPictureBoxDC : 3212376
ThunderFormDC : 4195536
 
You can map the hierarchical menu structure of a form into a treeview control. See the code below. It scans the whole menu tree recursively and populates a treeview control.

Start a new VB project and create the desired menu structure for your from using the Menu Editor.

Add Microsoft Windows Common Controls to your toolbox and place a treeview control on your form.

Insert the following code in your form.
___
[tt]
Option Explicit
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Const MF_BYPOSITION = &H400&

Private Sub Form_Load()
PopulateMenus TreeView1.Nodes.Add(Text:=Caption), GetMenu(hwnd)
End Sub

Sub PopulateMenus(Node As Node, hMenu As Long)
Dim N As Long, S As String * 1000
For N = 0 To GetMenuItemCount(hMenu) - 1
GetMenuString hMenu, N, S, Len(S), MF_BYPOSITION
PopulateMenus TreeView1.Nodes.Add(Node, tvwChild, , S), GetSubMenu(hMenu, N)
Next
Node.Expanded = True
End Sub

Private Sub Form_Resize()
TreeView1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub[/tt]
___

Run the program, the treeview will be filled hierarchically with the menu structure of the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top