Results 1 to 27 of 27

Thread: [RESOLVED] Adding menus to a menubar

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Resolved [RESOLVED] Adding menus to a menubar

    When you add menus to a menubar using code (not the Menu Editor) how then do you add functionality to the added menus. Assume my Form already has three top level menu items each with a sub menu and each has a Sub that performs the functionalities for the menu items. The following code will append a new top level menu item at the end of the menubar and a sub menu. How do I add functionality to that newly appended menu

    Code:
    Private Sub cmdAddNewMenu_Click()
     Dim MII As MENUITEMINFOW
     
     m_hMenu = GetMenu(AppHwnd)
    
     If m_hMenu Then
       AppendMenuW m_hMenu, MF_STRING, IDM_TOP_LEVEL_0, StrPtr("My Added Menu")
    
       SetMenu AppHwnd, m_hMenu
       DrawMenuBar AppHwnd
     End If
        
     GetSubMenu m_hMenu, 3&
    
     MII.cbSize = LenB(MII)
     MII.fMask = MIIM_SUBMENU
     MII.hSubMenu = CreatePopupMenu
    
     If MII.hSubMenu Then
       AppendMenuW MII.hSubMenu, MF_STRING, IDM_SUB_MENU_0, StrPtr("My Added Sub Menu")
          
       SetMenuItemInfoW m_hMenu, IDM_TOP_LEVEL_0, False, MII
       
       DrawMenuBar AppHwnd
     End If
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: Adding menus to a menubar

    You will have to subclass the Form and watch for the WM_COMMAND message. As mentioned in the documentation:

    Quote Originally Posted by MSDN
    When the user chooses a command item from a menu, the system sends a WM_COMMAND message to the window procedure. The low-order word of the WM_COMMAND message's wParam parameter contains the identifier of the chosen item. The window procedure should examine the identifier and process the message accordingly.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    OK, I got it. Thanks Victor.

    BTW: Is it possible to capture menu clicks from another application using my subclass proc

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    BTW: Is it possible to capture menu clicks from another application using my subclass proc
    Likely not. In that case, you would need to subclass that other application. That would require a global hook in a standard DLL or DLL injection. And if that other app is in a 64 bit process then things can get trickier.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    The other app is Notepad. I can add menu items to Notepad in the same way I have added them to my own app. As posted above I am able to capture the menu click of the added menu items using subclass but so far I cannot capture the menu click of the menu item(s) I added to Notepad. I read elsewhere that perhaps, like you said, I would probably have to use a DLL and do a global hook but that is something I have never done. I'm basically just playing around with this menu adding to get some experience but another member in his thread was asking how to click on the added menu from Notepad and capture the click so I thought I would just give it a try but first I had to learn how to add menu items.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adding menus to a menubar

    Another thing you might be able to research is window hooks. I don't recall if one of them can be used (without being in a DLL) to capture/spy messages in other applications.

    NVidia was big on this decades ago. There was a time when you installed their drivers and it would add a menu item to to the system menu of nearly every running application.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    I don't know if it was NVida or not, probably was, but I do remember having an application that did exactly that. It was very annoying.

    I have an app that does key logging but I don't know it it can log mouse strokes. Probably not since it appears to log key down events like typing into a textbox, for example.

    I'll do some looking around for Window hook.

  8. #8
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    I don't know if it was NVida or not, probably was, but I do remember having an application that did exactly that. It was very annoying.

    I have an app that does key logging but I don't know it it can log mouse strokes. Probably not since it appears to log key down events like typing into a textbox, for example.

    I'll do some looking around for Window hook.
    If I am not mistaken I have tried similar things years ago. There are some projects on my github (see my signature) and I could look into my old projects to see whether I have some useful code.

    Ah, I found an old post: http://www.vbforums.com/showthread.p...highlight=menu - I could reupload the attachment.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: Adding menus to a menubar

    I'll tell you how I do this. Typically, I'm wanting to add to some specific sub-menu. For instance, I have this form with "Edit", "Transform", "Memory", and "Play" main menu items. And each of these will have sub-menus. However, I don't want to load them at design-time, so I just create a single entry with an Index value set to 0:

    Name:  menu.png
Views: 411
Size:  11.3 KB

    ... then, I can add items to this sub-menu at will, also probably using this 0 Index item first. I can add with the following code:

    Code:
    
    Load mnuEditItem(iNextIndexNumberThatMustBeTracked)
    mnuEditItem(iEditItem).Visible = True
    mnuEditItem(iEditItem).Caption = "Whatever You Want"
    
    And then, in the menu's event, I can execute whatever I want:

    Code:
    
    Private Sub mnuEditItem_Click(Index As Integer)
        ' Check value of Index and do what's appropriate.
    End Sub
    
    ----------

    This is an entirely non-API approach. VB6 is quite weird with the way it builds its menus, and this approach just circumvents all that weirdness.

    Anyway, Ordinary, maybe it won't work for you, but I thought I'd share how I accomplish these things.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by Peter Swinkels View Post
    If I am not mistaken I have tried similar things years ago. There are some projects on my github (see my signature) and I could look into my old projects to see whether I have some useful code.

    Ah, I found an old post: http://www.vbforums.com/showthread.p...highlight=menu - I could reupload the attachment.
    Would you please reupload the attachment. Thank you.

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    Would you please reupload the attachment. Thank you.
    Rather than reuploading here I uploaded the program to GitHub: https://github.com/PeterSwinkels/Menu-Explorer - Is that okay too?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by Victor Bravo VI View Post
    You will have to subclass the Form and watch for the WM_COMMAND message.
    How do I know the values of wParam? In the code below you see I have the values but I didn't know what they were so I had to put a Debug.Print wParam (now removed) line after the Case WM_COMMAND to see what the values were for each menu item. Is there a better way to get these values

    Code:
    Public Function MainWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
     Select Case hWnd
       Case AppHwnd
         Select Case uMsg
           Case WM_COMMAND
             Select Case wParam
               Case 2
                 MsgBox "You selected Open"
    
               Case 4
                 MsgBox "You selected Edit"
    
               Case 6
                 MsgBox "You selected View"
    
               Case 1   'ID of the menu sub item I added via code
                 MsgBox "You selected new menu"
             End Select
             
             MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
             
             Exit Function
             
           Case Else
             MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
         End Select
       
       Case Else
         MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
     End Select
    End Function

  13. #13
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    How do I know the values of wParam? In the code below you see I have the values but I didn't know what they were so I had to put a Debug.Print wParam (now removed) line after the Case WM_COMMAND to see what the values were for each menu item. Is there a better way to get these values

    Code:
    Public Function MainWndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
     Select Case hWnd
       Case AppHwnd
         Select Case uMsg
           Case WM_COMMAND
             Select Case wParam
               Case 2
                 MsgBox "You selected Open"
    
               Case 4
                 MsgBox "You selected Edit"
    
               Case 6
                 MsgBox "You selected View"
    
               Case 1   'ID of the menu sub item I added via code
                 MsgBox "You selected new menu"
             End Select
             
             MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
             
             Exit Function
             
           Case Else
             MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
         End Select
       
       Case Else
         MainWndProc = CallWindowProc(MainOldWindPoc, hWnd, uMsg, wParam, lParam)
     End Select
    End Function
    https://docs.microsoft.com/en-us/win...allwindowproca

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adding menus to a menubar

    @Peter. I didn't download your samples from github, just looked them over. Does it contain code to determine when another app's menu is being clicked? I didn't see that and seems to be one of things wanted here (post #3)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by Peter Swinkels View Post

    That doesn't answer my question; it only tells me about the WindowProc. My question was how do I know the values of wParam so I don't have to pre-test wParam as I described
    Last edited by Ordinary Guy; Jun 29th, 2020 at 03:07 PM.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by LaVolpe View Post
    @Peter. I didn't download your samples from github, just looked them over. Does it contain code to determine when another app's menu is being clicked? I didn't see that and seems to be one of things wanted here (post #3)
    His program only lists menus of running apps but not all apps are captured, like Outlook, for example. Actually, I already had an app that did pretty much the same thing.

  17. #17
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    His program only lists menus of running apps but not all apps are captured, like Outlook, for example. Actually, I already had an app that did pretty much the same thing.
    I am sorry my program doesn’t offer the solution you were looking for. I offered it in the hope it might contain useful snippets of code. Good luck with your project.

  18. #18
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    That doesn't answer my question; it only tells me about the WindowProc. My question was how do I know the values of wParam so I don't have to pre-test wParam as I described
    My apologies. Does the following page answer your question: https://docs.microsoft.com/en-us/win...urc/wm-command ?

    @LaVolpe, no it doesn't have any code that checks what menu is being clicked. I am afraid that's all I could find while digging through my old files. Sorry.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    [QUOTE=Peter Swinkels;5485015]My apologies. Does the following page answer your question: https://docs.microsoft.com/en-us/win...urc/wm-command ?

    No

  20. #20
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Adding menus to a menubar

    [QUOTE=Ordinary Guy;5485033]
    Quote Originally Posted by Peter Swinkels View Post
    My apologies. Does the following page answer your question: https://docs.microsoft.com/en-us/win...urc/wm-command ?

    No
    Darn. That's that.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    I'm trying to figure out how to pre-determine the actual values of wParam during design time for each menu item. I can get the values by doing a pre-run of the program and noting the values passed using Debug.Print but that's not what I want to do; I want to know ahead of time so I can code these values as I write the code.

  22. #22
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adding menus to a menubar

    AFAIK menus are not created during design-time -- no menu handles until runtime.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by LaVolpe View Post
    AFAIK menus are not created during design-time -- no menu handles until runtime.
    Well, they are if you use the Menu Editor which is what I used to make the first three menus. Only the forth menu which I inserted between the original menu 1 and menu 2 was created during runtime. When I subclassed the app in order to get functionality for my newly created menu I had to know the value that was in wParam in order to capture the mouse click on the new menu item. This is what I have been asking about is how do I pre-determine the value of wParam so I can code it during design time. I had to do a pre-run of the program and using Debug.Print I was able to get the wParam values for all the menu items and then went back and hard coded them in the Windows Proc. What I want to know is these values ahead of time without doing a pre-run.

  24. #24
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    Well, they are if you use the Menu Editor which is what I used to make the first three menus.
    Well, you said you had to place Debug.Print statements to get the value; therefore, they were not available until runtime.

    Anyway, during form load, you can navigate the menubar to get the menu IDs. Once you enumerate those, you can then hardcode your 'wparams'. I doubt VB will change those menu IDs when the app is compiled, but you may want to test that. I think you already are aware of the APIs:

    GetMenu, GetMenuItemInfo, etc. The MENUITEMINFO structure passed to/from GetMenuItemInfo will have that wparam value as its wID member.

    Also note that what you are currently doing with hardcoding menu IDs is not guaranteed to work in any other application. Many applications can dynamically load menu items and dynamically create menus on demand, just as you have done with CreateMenu, AppendMenu, etc.

    In real life, those menu IDs are assigned by the coder (VB in this case) and just like your subclass procedure, the coder knows that menu ID #1 does this and menu ID #2 does that, and so on. The IDs are not dynamic in that sense, they are selected by the coder so that when it comes through the subclass procedures, the coder knows what routine to call based on that menu ID.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    OK, my question has been answered. see http://www.vbforums.com/showthread.p...=1#post5485172

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    I guess the only thing left is how to capture menu clicks from an external application and from what I have read it will require a DLL and even then it may not work.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    500

    Re: Adding menus to a menubar

    Quote Originally Posted by Ordinary Guy View Post
    I guess the only thing left is how to capture menu clicks from an external application and from what I have read it will require a DLL and even then it may not work.
    OK, I got that one taken care of now. Click here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width