Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / August 2007



Tip: Looking for answers? Try searching our database.

Open a menu and click/select an item in VB via win32

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gordon - 25 Aug 2007 00:00 GMT
Hi,

I'd like to open a menu in an external windows app and select an item using
VB and Win32 -- I've figured out how to click a button and populate an edit
control with text. If anyone knows hwo to accomplish this in VB, I'd
appreciate it. Thanks!

Gordon
Ed - 25 Aug 2007 06:11 GMT
>Hi,
>
[quoted text clipped - 4 lines]
>
>Gordon

Option Explicit

Private Const WM_COMMAND = &H111
Private Const MIIM_TYPE = &H10
Private Const MIIM_ID = 2

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 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 GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" _
(ByVal hMenu As Long, ByVal un As Long, ByVal b As Long, lpMenuItemInfo As MENUITEMINFO) As Long

Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Sub MenuClick(Hwnd As Long, Menu As Long, Item As Long)
Dim hMenu As Long, hSubMenu As Long, L As Long
Dim M As MENUITEMINFO
If Hwnd Then
   hMenu = GetMenu(Hwnd)
   If hMenu Then
       hSubMenu = GetSubMenu(hMenu, Menu)
       If hSubMenu Then
           M.fMask = MIIM_TYPE Or MIIM_ID
           M.dwTypeData = Space$(128)
           M.cbSize = Len(M)
           M.cch = 128
           L = GetMenuItemInfo(hSubMenu, Item, True, M)
           L = SendMessage(Hwnd, WM_COMMAND, M.wID, ByVal 0)
       End If
   End If
End If
End Sub

' Example - Open Notepad's About Dialog.
Private Sub Command1_Click()
   Dim Lng As Long
   Lng = FindWindow(vbNullString, "Untitled - Notepad")
   MenuClick Lng, 4, 2 ' 0 based, menu seperators count as an item.
End Sub
' -----

hth,
Ed
Gordon - 25 Aug 2007 14:02 GMT
That's fantastic. Thanks, Ed!

> >Hi,
> >
[quoted text clipped - 67 lines]
> hth,
> Ed
Norm Cook - 25 Aug 2007 17:01 GMT
> >Hi,
> >
[quoted text clipped - 4 lines]
> >
> >Gordon

<< SNIP >>>
> ' Example - Open Notepad's About Dialog.
> Private Sub Command1_Click()
>     Dim Lng As Long
>     Lng = FindWindow(vbNullString, "Untitled - Notepad")
>     MenuClick Lng, 4, 2 ' 0 based, menu seperators count as an item.
> End Sub

Nice code but the Notepad I'm seeing only has 4 top level menus
So shouldn't it be:
   MenuClick Lng, 3, 2 ' 0 based, menu seperators count as an item.
Ed - 25 Aug 2007 18:52 GMT
>> End Sub
>
>Nice code but the Notepad I'm seeing only has 4 top level menus
>So shouldn't it be:
>    MenuClick Lng, 3, 2 ' 0 based, menu seperators count as an item.

Yes, because the first menu on the left would be 0.

Found this example, shows how to get the menus of other programs.
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=48141&lngWId=1

Cheers,
Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.