Link to home
Start Free TrialLog in
Avatar of jcorbin
jcorbin

asked on

Hooking Into an App's Menu

Is there a way to add a menu item to an app using VB? I have a database utility that I'd like to be able to use directly from SQL Server's Query Analyzer, and would like to be able to make it available via QA's menu. Is this possible?
Avatar of mcrider
mcrider

You may want to take a look at this article: http://www.vbsquare.com/articles/sysmenu/ 


Cheers!®©
"Changing and Responding to a Modified System Menu"

http://www.mvps.org/vbnet/code/subclass/systemabout.htm
Avatar of jcorbin

ASKER

I am not trying to subclass my own apps menus I need to hook into SQL Server's Query Analyzer application. I.e. add my utility to the menu in Query Analyzer.
adding a menu item to the app isnt too hard...responding to someone clicking it is a bit more difficult.  heres how you would add a menu item to Notepad for example:

Option Explicit

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 ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As String) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long
Private Declare Function CreatePopupMenu Lib "user32" () As Long

Const MF_BYPOSITION = &H400&
Const MF_POPUP = &H10&
Const MF_ENABLED = &H0&
Const MF_STRING = &H0&


Private Sub CreateMenus()
Dim target_hwnd As Long
Dim hFormMenu As Long
Dim hSubMenu2 As Long
Dim hSubMenu2List As Long
   
    target_hwnd = FindWindow( _
        vbNullString, "Untitled - Notepad")
    If target_hwnd = 0 Then
        MsgBox "Error finding target application"
        Exit Sub
    End If

    hFormMenu = GetMenu(target_hwnd)
    If hFormMenu = 0 Then
        MsgBox "Error getting main menu"
        Exit Sub
    End If

    hSubMenu2List = CreatePopupMenu()
    If hSubMenu2List = 0 Then
        MsgBox "Error creating new menu"
        Exit Sub
    End If

    ModifyMenu hFormMenu, 0, MF_BYPOSITION + MF_POPUP, hSubMenu2List, "New Menu Item"

    hSubMenu2 = GetSubMenu(hFormMenu, 0)

    Call AppendMenu(hSubMenu2, MF_STRING + MF_ENABLED, 1001, "My Menu Item")
End Sub


Private Sub Form_Load()
    CreateMenus
End Sub

I believe you're going to have to use the SetWindowsHookEx UnhookWindowsHookEx and CallNextHookEx API calls to do the processing...

For an example of these APIs, check out the following microsoft KB article:

HOWTO: Intercept Keyboard Input from Visual Basic
http://support.microsoft.com/support/kb/articles/Q177/9/92.ASP?LN=EN-US&SD=msdn&FR=0 

The article doesn't have anything to do with menus, but it does show how to use the APIs...


Cheers!®©
Avatar of jcorbin

ASKER

Ok thanks guys, that will do the job I believe, now can I split the points between you since one gave me the add menu and the other the processing the menu click??
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Community Support has reduced points from 200 to 100
Hello everyone,

I am reducing points on this question for a split.

jcorbin, you can now accept one of the comments as an answer. Remember, the Accept Comment button is in the header of the Comment.

For the second Expert, you will need to post a new question in this topic area. The new question title should be 'For ExpertName -- 10360101' inserting the appropriate Expert's username.

For your convenience, you can use this link to create the new question(right click and open in new window):
https://www.experts-exchange.com/bin/NewQForm?ta=31

darinw
Customer Service
Avatar of jcorbin

ASKER

Comment accepted as answer