Solved

Dynamicly adding sub-sub menu's

Posted on 2003-11-05
9
355 Views
Last Modified: 2010-08-05
Hi, I've checked out this topic:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20393997.html

and I managed to get a layout like:

file
 \_ open
 \_ save
 \_ exit

BUT, do you have any idea how to create a menu layout like

file
 \_ new
        \_ normal
        \_ template
 \_ open
 \_ save

(I hope my scheme's make any sence).. Thanks in advance!
0
Comment
Question by:Siebe
9 Comments
 
LVL 2

Expert Comment

by:wuttrain
ID: 9686369
I usually do this:

Main
....One
.........aOne
....Two
.........aTwo

Be sure that the index property of aOne and aTwo menu items are set to 0.

Then try the code below.

Option Explicit

Private Sub Command1_Click()
  Load mnuaOne(1)
  mnuaOne(1).Visible = True
  mnuaOne(1).Caption = "bOne"
  Load mnuaOne(2)
  mnuaOne(2).Visible = True
  mnuaOne(2).Caption = "cOne"
 
  Load mnuaTwo(1)
  mnuaTwo(1).Visible = True
  mnuaTwo(1).Caption = "bTwo"
End Sub

hope this helps.
0
 

Author Comment

by:Siebe
ID: 9686386
I was reffering to the API's like InsertMenu, AppendMenu, DrawMenuBar etc.. This since the menu has to be hooked into a different application...
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9686581
Listening
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Siebe
ID: 9686609
Listening?
Anyway, this is some basic code I have so far:

Public Function HookInMSN() As Long
    'Hook into MSN (if possible)
    Dim wMSN As Long, wNMenu As Long, wSubMenu As Long
    wMSN = FindWindow("MSNMSBLClass", vbNullString)
   
    If wMSN > 0 Then
        'Create the menu and fill it with items
        wNMenu = GetMenu(wMSN): DoEvents
        wSubMenu = CreatePopupMenu
       
        'Items...
        Call AppendMenu(wSubMenu, MF_STRING, Offset + 1, "Games")
        Call AppendMenu(wSubMenu, MF_STRING, Offset + 2, vbNullString)
        Call AppendMenu(wSubMenu, MF_STRING, Offset + 3, "About")
        Call AppendMenu(wSubMenu, MF_STRING, Offset + 4, "Exit")
        Call InsertMenu(wNMenu, 7, MF_BYPOSITION Or MF_POPUP, wSubMenu, "BOT2K3"): DoEvents
   
        'Draw the new menubar
        Call DrawMenuBar(wMSN)
       
        'Draw the subitems
        Call DrawSubItems(wNMenu)
       
        'Return true
        HookInMSN = wMSN
    Else
        'Return false
        HookInMSN = 0
    End If
End Function

Now I would like to know how I can create items under the subitem "Games":

BOT2K3
....Games
........PimPamPet
........Trivial Pursuit
....---
....About
....Exit
0
 

Author Comment

by:Siebe
ID: 9686614
Correction, at the place of

        'Draw the subitems
        Call DrawSubItems(wNMenu)

there should be nothing (temp. testing something).
0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 250 total points
ID: 9686828
You can do it the same way you did it in your "code". Just supply the itemid as the sub-menu you created. Example below.  Be warned though, you'll need the call-back function of the window procedure to be in a DLL to receive WM_COMMAND messages to know when they click on your menus. :)

Private Const MF_BYPOSITION = &H400&
Private Const MF_POPUP = &H10&
Private Const MFT_STRING = &H0

Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long

Private Sub Form_Load()
    Dim hMenu As Long, hSubMenu As Long, hSubSubMenu As Long
    hMenu = GetMenu(Me.hWnd)
    hSubMenu = CreatePopupMenu
    hSubSubMenu = CreatePopupMenu
    Call AppendMenu(hSubSubMenu, MF_STRING, 1, "Game1")
    Call AppendMenu(hSubSubMenu, MF_STRING, 1, "Game2")
   
    Call AppendMenu(hSubMenu, MF_POPUP, hSubSubMenu, "Games")
    Call AppendMenu(hSubMenu, MF_STRING, 2, vbNullString)
    Call AppendMenu(hSubMenu, MF_STRING, 3, "About")
    Call AppendMenu(hSubMenu, MF_STRING, 4, "Exit")
    Call InsertMenu(hMenu, 0, MF_BYPOSITION Or MF_POPUP, hSubMenu, "BOT2K3")
    Call DrawMenuBar(Me.hWnd)
End Sub
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 9686895

I've been using this control:

http://www.vbaccelerator.com/home/VB/Code/Controls/Menus/Icon_Menu_Control/article.asp

that lets you do what you need (and other things, also).
Since it is open source, it may be useful for you, too.

HTH
M.
0
 

Author Comment

by:Siebe
ID: 9686950
Cheers!
0
 
LVL 2

Expert Comment

by:wuttrain
ID: 10658191
siebe,

how do you hook the created menu to an existing application - say notepad?  I have tried adding my own menu to notepad, but the problem is I don't know how to run a function/code when they click my menu.

thanks!

wuttrain
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question