>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