|
-
Jan 28th, 2005, 10:15 PM
#1
Adding submenus to external programs
This code relies on a related thread, Subclass External Programs done for you. Download that hook control and dll.
This example adds a menu item under the file menu item in the Notepad application. It then waits for a user to click the new menu item and responds with our own messagebox.
Open a new project, add a command button and a hookcontrol to the form. Rename the hookcontrol to Hook.
VB Code:
Option Explicit
Const MENUID = 56 'our unique identifier
Dim hWndSubMenu As Long
'Forgot these declares in a previous post
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
Private Const MIIM_STATE = &H1
Private Const MIIM_ID = &H2
Private Const MIIM_STRING = &H40
Private Const MIIM_FTYPE = &H100
Private Const MFT_SEPARATOR = &H800
Private Const MFT_STRING = &H0
Private Const MFS_ENABLED = &H0
Private Const MFS_CHECKED = &H8
Private Const WM_COMMAND = &H111
Private Const MF_BYCOMMAND = &H0&
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) 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 RemoveMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long _
) As Long
Private Declare Function GetMenuItemCount Lib "user32.dll" ( _
ByVal hMenu As Long _
) As Long
Private Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" ( _
ByVal hMenu As Long, _
ByVal uItem As Long, _
ByVal fByPosition As Long, _
lpmii As MENUITEMINFO _
) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Sub Command1_Click()
Dim hWndNotepad As Long
Dim hWndMenu As Long
Dim count As Long
Dim mii As MENUITEMINFO
Dim appCaption As String
'Get a handle to Notepad
appCaption = "Untitled - Notepad"
hWndNotepad = FindWindow(vbNullString, appCaption)
While hWndNotepad = 0
If MsgBox("Open Notepad", vbOKCancel) = vbCancel Then Exit Sub
hWndNotepad = FindWindow(vbNullString, appCaption)
Wend
'Get handle to Notepad's main menu
hWndMenu = GetMenu(hWndNotepad)
'Get handle to 'File' submenu
hWndSubMenu = GetSubMenu(hWndMenu, 0)
'Get number of items in sub menu
count = GetMenuItemCount(hWndSubMenu)
'Setup data structure for InsertMenuItem call
With mii
.cbSize = Len(mii)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_STRING Or MIIM_FTYPE
.fType = MFT_STRING
.fState = MFS_ENABLED
.wID = MENUID 'our unique identifier
.dwTypeData = "My New Menu"
.cch = Len(.dwTypeData)
End With
' Add this to the menu.
Call InsertMenuItem(hWndSubMenu, count + 1, 1, mii)
Call DrawMenuBar(hWndNotepad)
'Now set up hook, we want to monitor the WM_COMMAND message
With Hook
.TargethWnd = hWndNotepad
.AddMessage WM_COMMAND, "WM_COMMAND"
.SetHook
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove our hook
Hook.RemoveAllHooks
'remove our menu item
Call RemoveMenu(hWndSubMenu, MENUID, MF_BYCOMMAND)
End Sub
Private Sub Hook_PostedMessage(uMsg As Long, wParam As Long, lParam As Long)
'here is where our messages will arrive
'look for our special menu ID
If (wParam And &HFFFF) = MENUID Then
'our menu item has been selected
MsgBox "Why did you do that?"
End If
End Sub
Last edited by moeur; Mar 18th, 2005 at 09:14 AM.
Reason: Fix bug
-
Feb 10th, 2005, 10:01 PM
#2
Hyperactive Member
Re: Adding submenus to external programs
I love all this hooking stuff
awesome! nice job moeur
Born to help others
(If I've been helpful then please rate my post. Thanks)
call me EJ or be slapped! 
-
Mar 17th, 2005, 08:10 PM
#3
New Member
Re: Adding submenus to external programs
Hello,
I'm using VB.NET and can't seem to get a valid return for:
GetSubMenu(GetMenu(hwnd), menuID)
Is it the menuID (.wID = menuID) that is used as the 2nd parameter for that function, or the position on the menu that the menu you want the submenu from is located?
Thanks.
-
Mar 17th, 2005, 09:29 PM
#4
Re: Adding submenus to external programs
GetSubMenu(GetMenu(hwnd), MenuID)
In this case MenuID specifies the zero-based relative position in the specified menu of an item that activates a drop-down menu or submenu.
Did you first try the code on Notepad as written above to verify it is working?
-
Mar 18th, 2005, 07:49 AM
#5
New Member
Re: Adding submenus to external programs
Great, thanks. It works fine with Notepad, and I've also managed to make it work with the other program I needed it for (thanks to your help). But my problem is, I have added the menu to the new program using InsertMenuItem, so it is not registering as a submenu. I need to add a submenu to it as well.
Thanks for your speedy reply.
-
Mar 18th, 2005, 08:05 AM
#6
Re: Adding submenus to external programs
But my problem is, I have added the menu to the new program using InsertMenuItem, so it is not registering as a submenu. I need to add a submenu to it as well.
So, do you have a solution or is there still a question?
-
Mar 18th, 2005, 08:28 AM
#7
New Member
Re: Adding submenus to external programs
 Originally Posted by moeur
So, do you have a solution or is there still a question?
My main issue is how I can modify the characteristics of these submenu items when they have been added.
Like uncheck a checked menu item?
Thanks
Last edited by Johno; Mar 18th, 2005 at 08:37 AM.
-
Mar 18th, 2005, 08:35 AM
#8
Re: Adding submenus to external programs
So, it worked OK with Notepad, but when you try the exact same thing on another program, it doesn't work?
What is this other program?
-
Mar 18th, 2005, 08:39 AM
#9
New Member
Re: Adding submenus to external programs
I'm starting to confuse myself now, lol. I changed that post to another issue I'm having.
Well, I am actually trying to add a completely new menu option, with sub menu items. I couldn't manage it with the InsertMenuItem function, so I have resorted back to an old method of using the CreatePopup function. (that was my issue earlier, I was trying to add submenu items to a menu I had inserted into the main menu of another program, but I have gone back to an older method i used to use)
But I need to interact with these submenu items, by disabling them, enabling them, changing check boxes etc.
Sorry, lol.
-
Mar 18th, 2005, 08:51 AM
#10
New Member
Re: Adding submenus to external programs
.SetHook PostedMessages
Is this the name of the function to send the hook data to? I seem to get an error saying it cannot be assigned values. Also, in my application "PostedMessages" is not declared as anything.
Last edited by Johno; Mar 18th, 2005 at 08:55 AM.
-
Mar 18th, 2005, 09:16 AM
#11
Re: Adding submenus to external programs
Whoops
I updated the hook control but didn't change this post.
The .SetHook function no longer requires any parameters
I modified the above code to reflect this.
-
Mar 18th, 2005, 09:21 AM
#12
New Member
Re: Adding submenus to external programs
 Originally Posted by moeur
Whoops
I updated the hook control but didn't change this post.
The .SetHook function no longer requires any parameters
I modified the above code to reflect this.
Hehe, that's fine. It still doesn't seem to be helping much though. Will it work in VB.NET? I do know that AddressOf now returns a delegate data type, so it can no longer return numeric values for parameters that the API often needs.
I can't seem to find a way to hook in VB.NET, I've been looking for weeks .
-
Mar 18th, 2005, 09:28 AM
#13
New Member
Re: Adding submenus to external programs
Hook.TargethWnd = GetMenu(FindWindow(vbNullString, "MSN Messenger (BETA)")).ToInt32
Hook.AddMessage(&H111, "WM_COMMAND")
Hook.SetHook()
'Hook.SetHook()' - returns:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in axinterop.hookctrl.dll
Additional information: Invalid procedure call or argument
-
Mar 18th, 2005, 09:39 AM
#14
Re: Adding submenus to external programs
I'm not at all familiar with .NET
Hook.TargethWnd = GetMenu(FindWindow(vbNullString, "MSN Messenger (BETA)"))
Should be
Hook.TargethWnd = FindWindow(vbNullString, "MSN Messenger (BETA)")
-
Mar 18th, 2005, 09:54 AM
#15
New Member
Re: Adding submenus to external programs
VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As IntPtr
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Hook.TargethWnd = FindWindow(vbNullString, "MSN Messenger (BETA)").toInt32
Hook.AddMessage(&H111, "WM_COMMAND")
Hook.SetHook()
End Sub
Public Sub Hook_PostedMessage(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
MsgBox("")
End Sub
Doesn't return an error anymore now, but Hook_PostedMessage never gets triggered.
'(vbNullString, "MSN Messenger (BETA)").toInt32' returns the valid window handle ID.
Am I missing something extra? I have added the OCX to the program, and put the MainHook.dll in the root folder.
Last edited by Hack; Oct 10th, 2005 at 07:59 AM.
-
Mar 21st, 2005, 02:20 PM
#16
New Member
Re: Adding submenus to external programs
i can't get WM_CLOSE to work, i need to know when notepad.exe is closing and change the wmsg...
anyone?
-
Mar 21st, 2005, 05:17 PM
#17
Re: Adding submenus to external programs
Unfortunately the WM_CLOSE and WM_DESTROY are sent messages so you cannot change them.
When the target App receives a WM_DESTROY message, it notifies the hook control that the target App is shutting down and the control raises an UnHook event.
So, you'll be notified when the App is shutting down, but can do nothing about it.
-
Mar 21st, 2005, 06:15 PM
#18
New Member
Re: Adding submenus to external programs
Ok, now i can't hook cmd.exe ... you know hot to do it?
-
Mar 21st, 2005, 07:05 PM
#19
Re: Adding submenus to external programs
-
Jul 21st, 2005, 05:20 AM
#20
New Member
Re: Adding submenus to external programs
I can't make this code work. I have removed the message hooking to try and locate where the process is failing.
It seems to be on the InsertMenuItem call. All the handles are showing as more than 0 but the insertmenuitem returns 0. I'm using Win95 (not my decision) and Word 97 VBA...
VB Code:
Option Explicit
Const MENUID = 56 'our unique identifier
Dim hWndSubMenu As Long
'Forgot these declares in a previous post
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
Private Const MIIM_STATE = &H1
Private Const MIIM_ID = &H2
Private Const MIIM_STRING = &H40
Private Const MIIM_FTYPE = &H100
Private Const MFT_SEPARATOR = &H800
Private Const MFT_STRING = &H0
Private Const MFS_ENABLED = &H0
Private Const MFS_CHECKED = &H8
Private Const WM_COMMAND = &H111
Private Const MF_BYCOMMAND = &H0&
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) 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 RemoveMenu Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long _
) As Long
Private Declare Function GetMenuItemCount Lib "user32.dll" ( _
ByVal hMenu As Long _
) As Long
Private Declare Function InsertMenuItemA Lib "user32.dll" ( _
ByVal hMenu As Long, _
ByVal uItem As Long, _
ByVal fByPosition As Long, _
lpmii As MENUITEMINFO _
) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Sub Command1_Click()
Dim hWndNotepad As Long
Dim hWndMenu As Long
Dim count As Long
Dim mii As MENUITEMINFO
Dim appCaption As String
'Get a handle to Notepad
appCaption = "Untitled - Notepad"
hWndNotepad = FindWindow(vbNullString, appCaption)
While hWndNotepad = 0
If MsgBox("Open Notepad", vbOKCancel) = vbCancel Then Exit Sub
hWndNotepad = FindWindow(vbNullString, appCaption)
Wend
'Get handle to Notepad's main menu
hWndMenu = GetMenu(hWndNotepad)
'Get handle to 'File' submenu
hWndSubMenu = GetSubMenu(hWndMenu, 0)
'Get number of items in sub menu
count = GetMenuItemCount(hWndSubMenu)
'Setup data structure for InsertMenuItem call
With mii
.cbSize = Len(mii)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_STRING Or MIIM_FTYPE
.fType = MFT_STRING
.fState = MFS_ENABLED
.wID = MENUID 'our unique identifier
.dwTypeData = "My New Menu"
.cch = Len(.dwTypeData)
End With
' Add this to the menu.
If InsertMenuItemA(hWndSubMenu, count + 1, 1, mii) <> 0 Then
MsgBox "Menu Created"
Else: MsgBox "Menu Creation Failed"
End If
Call DrawMenuBar(hWndNotepad)
End Sub
Last edited by Hack; Oct 10th, 2005 at 08:00 AM.
-
Jul 21st, 2005, 11:09 AM
#21
Re: Adding submenus to external programs
Sugar,
Please go back and edit your post. place vbcode tags around your code to make it easier for me to read. such as:
[VBCODE] your code goes here [/VBCODE]
Next, I would go through the documentation for all the API calls you made and see if they are supported under Win95. You can find an alphabetical list of all the functions at
http://msdn.microsoft.com/library/de...ical_order.asp
After doing these two things if you still have a question let me know and I'll be glad to help.
-
Jul 21st, 2005, 11:16 AM
#22
-
Oct 10th, 2005, 08:00 AM
#23
Re: Adding submenus to external programs
-
Oct 15th, 2005, 12:53 PM
#24
New Member
Re: Adding submenus to external programs
I have tried the code , it works fine with notepad ,wordpad etc. However I cannot get it work with Excel , Winword. Those applications use floating menu bars.
Getmenu function says "GetMenu does not work on floating menu bars. Floating menu bars are custom controls that mimic standard menus; they are not menus. To get the handle for a floating menu bar, use the Active Accessibility APIs."
It also does not work with VBasic IDE. I have checked the floating menu bar has a class name of MsoCommandBar and its parent is MsoCommandBarDock.
Any idea how to get menus from floating menu bars? Thanks...
-
Dec 9th, 2005, 05:47 PM
#25
Re: Adding submenus to external programs
 Originally Posted by cssriraman
I just see your thread Adding submenus to external programs. It is excellant code.
I need small help from you.
1. In that thread you added a menu in File menu of Notepad. How can I add a menu in Edit Menu of Notepad.
2. I just want to click/executre the File -> Open menu in Notepad.
Please help me!
Thanks in advance.
CS.
CS,
I don't like to answer these things in PM so I'm posting your question here so others can benefit.
To change the code sot that the new menu item appears under the Edit menu rather than the File menu simply change this line
VB Code:
hWndSubMenu = GetSubMenu(hWndMenu, 1) 'use 0 for file menu and 1 for edit menu
I hope this answers your question.
-
Dec 9th, 2005, 07:26 PM
#26
Re: Adding submenus to external programs
Thanks,
How can I click the File -> Open menu?
Thanks in advance,
-
Dec 9th, 2005, 08:36 PM
#27
Re: Adding submenus to external programs
try this
VB Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long
Private Declare Function SetForegroundWindow Lib "user32" ( _
ByVal hwnd As Long _
) As Long
Private Sub Command1_Click()
Dim hWndNotepad As Long
Dim appCaption As String
Dim appClass As String
'Get a handle to Notepad
appCaption = "Untitled - Notepad"
appClass = "Notepad"
hWndNotepad = FindWindow(appClass, vbNullString)
If hWndNotepad = 0 Then
If MsgBox("Open Notepad?", vbOKCancel) = vbCancel Then Exit Sub
Shell "Notepad.exe"
While hWndNotepad = 0
hWndNotepad = FindWindow(vbNullString, appCaption)
DoEvents
Wend
End If
'make it the active window
If SetForegroundWindow(hWndNotepad) = 0 Then
MsgBox "SetForegroundWindow failed"
Exit Sub
End If
SendKeys "%fo"
End Sub
-
Jan 9th, 2006, 09:52 AM
#28
Re: Adding submenus to external programs
Thanks! Is there any way without using sendkeys?
because, sendkeys are not working sometimes.
-
Jan 9th, 2006, 10:11 AM
#29
Re: Adding submenus to external programs
is the problem that there is no keyboard shortcut for the menu?
-
Jan 9th, 2006, 10:36 AM
#30
Re: Adding submenus to external programs
Yes. I cannot use shortcut keys as you said for a software. Using mouse only I can use that software. With notepad no issues.
-
Jan 28th, 2006, 11:41 AM
#31
Addicted Member
Re: Adding submenus to external programs
 Originally Posted by moeur
This code relies on a related thread, Subclass External Programs done for you. Download that hook control and dll.
This example adds a menu item under the file menu item in the Notepad application. It then waits for a user to click the new menu item and responds with our own messagebox.
Open a new project, add a command button and a hookcontrol to the form. Rename the hookcontrol to Hook.
Can you explain what is the hookcontrol
If above question or answer will help to you
Don't forget to rate me
தமிழ்இன்பன்
-
Jan 28th, 2006, 12:53 PM
#32
Re: Adding submenus to external programs
Did you read the thread I referred to? It is very in depth.
-
Jan 29th, 2006, 01:33 PM
#33
Re: Adding submenus to external programs
 Originally Posted by cssriraman
Thanks,
How can I click the File -> Open menu?
Thanks in advance,
Can someone help me on that.
I would like to achieve that without using sendkeys.
-
Jan 29th, 2006, 03:07 PM
#34
Re: Adding submenus to external programs
Please post this question as a new thread in the regular forums not here.
-
Jan 30th, 2006, 09:31 PM
#35
Re: Adding submenus to external programs
-
Apr 18th, 2006, 08:35 PM
#36
Hyperactive Member
Re: Adding submenus to external programs
Hi!
Maybe a stupid question, but how do I "install" the DLL?
Because it gives this error:
File not found: MainHook.Dll
Thanx
-
Apr 18th, 2006, 10:26 PM
#37
Re: Adding submenus to external programs
put it in your Windows system32 directory or in the executable directory
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
|