-
Jun 27th, 2020, 09:53 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 28th, 2020, 03:38 AM
#2
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:
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.
-
Jun 28th, 2020, 03:06 PM
#3
Thread Starter
Hyperactive Member
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
-
Jun 28th, 2020, 04:55 PM
#4
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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.
-
Jun 28th, 2020, 05:50 PM
#5
Thread Starter
Hyperactive Member
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.
-
Jun 28th, 2020, 06:01 PM
#6
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.
-
Jun 28th, 2020, 06:33 PM
#7
Thread Starter
Hyperactive Member
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.
-
Jun 29th, 2020, 08:04 AM
#8
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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.
-
Jun 29th, 2020, 09:50 AM
#9
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:
... 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.
-
Jun 29th, 2020, 10:43 AM
#10
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by Peter Swinkels
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.
-
Jun 29th, 2020, 12:35 PM
#11
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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?
-
Jun 29th, 2020, 12:37 PM
#12
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by Victor Bravo VI
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
-
Jun 29th, 2020, 02:55 PM
#13
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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
-
Jun 29th, 2020, 02:58 PM
#14
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)
-
Jun 29th, 2020, 03:03 PM
#15
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by Peter Swinkels
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.
-
Jun 29th, 2020, 03:06 PM
#16
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by LaVolpe
@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.
-
Jun 30th, 2020, 01:12 AM
#17
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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.
-
Jun 30th, 2020, 06:01 AM
#18
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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.
-
Jun 30th, 2020, 11:06 AM
#19
Thread Starter
Hyperactive Member
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
-
Jun 30th, 2020, 11:14 AM
#20
Re: Adding menus to a menubar
[QUOTE=Ordinary Guy;5485033]
Originally Posted by Peter Swinkels
Darn. That's that.
-
Jun 30th, 2020, 12:31 PM
#21
Thread Starter
Hyperactive Member
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.
-
Jun 30th, 2020, 01:34 PM
#22
Re: Adding menus to a menubar
AFAIK menus are not created during design-time -- no menu handles until runtime.
-
Jun 30th, 2020, 04:04 PM
#23
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by LaVolpe
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.
-
Jun 30th, 2020, 04:30 PM
#24
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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.
-
Jul 1st, 2020, 02:33 PM
#25
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
-
Jul 1st, 2020, 03:00 PM
#26
Thread Starter
Hyperactive Member
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.
-
Jul 4th, 2020, 06:55 PM
#27
Thread Starter
Hyperactive Member
Re: Adding menus to a menubar
Originally Posted by Ordinary Guy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|