Valhalla Legends Forums Archive | General Programming | Menu (Win32 API) Help Requested

AuthorMessageTime
Grok
Goal is to click a menu item in another application.

The Menu:  &Server  &Optical  &Whatever  S&tore

So far I've used GetMenu, GetMenuItemCount, and GetMenuString to find "S&tore" as the 4th item on the window's menu.

Do I need to click on S&tore to reveal its "submenu" and then get a handle to the submenu, so I can click on its item?

I want to click the last item in the submenu revealed by S&tore from the main menu.

Where do I go next?
March 12, 2003, 11:16 AM
Grok
OK, I have time to paste what I'm doing in more detail...

This is code so far...
[code]    If gWnd.OSI.hwnd = 0 Then
       Dim hMenu As Long, lMenuCount As Long
       Dim sMenu As String, sMenuItem As String
       Dim lPos As Long
       hMenu = GetMenu(gWnd.NUTL.hwnd)
       If hMenu = 0 Then Exit Sub
       lMenuCount = GetMenuItemCount(hMenu)
       For lPos = 0 To lMenuCount - 1
           sMenuItem = String(30, 0)
           lRet = GetMenuString(hMenu, lPos, sMenuItem, 30, MF_BYPOSITION)
           lRet = HiliteMenuItem(gWnd.NUTL.hwnd, hMenu, 4, MF_BYPOSITION + MF_HILITE)
           If Len(sMenu) > 0 Then sMenu = sMenu & vbCrLf
           sMenu = sMenu & StripNulls(sMenuItem)
       Next lPos
       MsgBox "Menu items = " & vbCrLf & sMenu
       Exit Sub
   End If
[/code]

This prints out
Menu items =
&Server
&Optical
O&bject
&Index
S&tore

Next I need to open S&tore's menu and click a particular item in that menu.
March 12, 2003, 12:50 PM
Skywing
You can send WM_COMMAND to the window to simulate a menu item hit.  Check out MSDN for full details, but I think it's what you're looking for.
March 12, 2003, 2:09 PM
Grok
Yes thank you, but I'm trying to get the menu item number of the menu subitem off the 4-indexed menu item on the main menu.

The difficulty I'm having is with moving beyond the "top" menu (the one with the 5 items, 0-4).  I don't know how to open the "S&tore" menu to traverse its members.

Hmm, maybe GetMenuItem, GetSubMenu from there, then traverse those items to get the desired submenu's subitem id.  If I get positive results I'll post the solution.
March 12, 2003, 5:20 PM
Grok
Got it working now, but it doesn't return to VB from SendMessage until the user closes the dialog that was opened by the menu selection.  How can I fix that?

[code]
Dim hMenu As Long, hSubMenu As Long, hSubItem As Long

hMenu = GetMenu(gWnd.NUTL.hwnd)
hSubMenu = GetSubMenu(hMenu, 4)
hSubItem = GetMenuItemID(hSubMenu, 4)

lRet = SendMessage(gWnd.NUTL.hwnd, WM_COMMAND, hSubItem, 0&)
[/code]
March 12, 2003, 6:08 PM
kamakazie
Perhaps this could be of some help:  http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvbtips/html/msdn_msdn75.asp
March 12, 2003, 6:34 PM
Yoni
SendMessage waits for the target to finish processing the message before it returns. If you want it to return immediately, use SendNotifyMessage instead. (Same prototype other than return value, which you can't retrieve since the function may return before the message finishes processing.)
March 12, 2003, 6:42 PM
Grok
Thanks Yoni, works splendidly.
[code]Dim hMenu As Long, hSubMenu As Long, hSubItem As Long
hMenu = GetMenu(gWnd.NUTL.hWnd)
hSubMenu = GetSubMenu(hMenu, 4)
hSubItem = GetMenuItemID(hSubMenu, 4)
lret = SendNotifyMessage(gWnd.NUTL.hWnd, WM_COMMAND, hSubItem, 0&)[/code]
March 13, 2003, 9:14 AM

Search