Click to See Complete Forum and Search --> : Menu command
WadeD
Jan 5th, 2000, 02:13 AM
Hi,
I've got the handle to a submenu that I wish to call. How can I use SendMessage to select it?
I'm using:
Call SendMessage(hSubMenu, &H100, 0, 0)
but I'm not getting a response.
Thanks in advance,
Wade
ChrisJackson
Jan 5th, 2000, 02:56 AM
Hi WadeD,
Here's the code to select a Menu Item from a Window.
To use it, create a new project and place 2 command buttons on it.
In the VB Editor, paste the following:
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 GetMenuItemID Lib "user32" (ByVal HMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal HMenu As Long, ByVal nPos As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Hwindow As Long
Private HMenu As Long
Private HSubMenu As Long
Private MenuID As Long
Private Sub Command1_Click()
SelectMenuItem "Calculator", 2, 2
End Sub
Private Sub Command2_Click()
Unload Me
End
End Sub
Private Sub SelectMenuItem(WinName As String, Level1 As Long, Level2 As Long)
Hwindow = FindWindow(vbNullString, WinName)
HMenu = GetMenu(Hwindow)
HSubMenu = GetSubMenu(HMenu, Level1)
MenuID = GetMenuItemID(HSubMenu, Level2)
PostMessage Hwindow, &H111, MenuID, 0
End Sub
Be sure that you have "Calculator" open when you run it.
All the best.
Chris Jackson
WadeD
Jan 5th, 2000, 03:44 AM
This does work for calculator but not for the app that I'm calling. I know that I have the correct window and menu handles because I can get the menu text using these. Any ideas??
Thanks,
Wade
ChrisJackson
Jan 5th, 2000, 07:24 AM
WadeD,
Using the code I posted, make sure that you are typing in the title of the window correctly (including the correct Case). Also, keep in mind that the Menu Items and Sub Items begin with 0 and not 1. Finally, a "--------" (line) in a drop-down menu should be counted when you trying to access the menu choices.
What's the name of the App that you are trying to access the Menu of anyway?
Hope this helps.
Chris
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.