• Advertisement
Sign in to follow this  

Invoking menu of other applications

This topic is 4587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to open menu of other applications programmatically, sending the messages a window receives when I click the menu items or when I press ALT+F then 'O' to open a file, for example. I've tried with: CWnd * pWnd= the window I want to control (I read the handle with Spy++ then I use CWnd::FromHandle); CMenu * pMenu=pWnd->GetMenu(); CMenu * pSubMenu=pMenu->GetSubMenu(0); // 0 correspond to "File" menu int MenuID=pSubMenu->GetMenuItemID(5); // 5 correspond to "Save as..." item pWnd->SendMessage(WM_COMMAND,MenuID,NULL); While this can work for Notepad, for other application it doesn't work (eg. with an instance of Visual C++). I searched on google and yesterday I found a source in VB that make use of CommandBar objects to invoke menu and I promised myself to give it a try (after installing the .NET 2003 Toolkit I've not downloaded yet [grin]). (I think it is easier to write a program like that with VB instead of VC++.) Can you suggest a link or a tutorial on using CommandBar and/or invoking menu of another application since I cannot find the VB source code I found yesterday? Any other suggestion (ie. open source project, other methods, etc.) will be surely helpful. Thank you very much in advance. :)

Share this post

Link to post
Share on other sites
A long time ago I wrote a function to do this for VB6 (I have no idea how to program for VB.NET).

You need to declare all the necessary functions.
You need to pass the hWnd number.
I always used findwindow(), which you only need the class name string or title string, but whichever method you wish to use is fine.

I couldn't run menus for Windows Explorer, but it worked for other basic programs.

Sub RunMenuByString(Application As Long, StringSearch As String)

tosearch& = GetMenu(Application)
menucount& = GetMenuItemCount(tosearch&)

For findstring& = 0 To menucount& - 1
tosearchsub& = GetSubMenu(tosearch&, findstring&)
menuitemcount& = GetMenuItemCount(tosearchsub&)

For GetString& = 0 To menuitemcount& - 1
subcount& = GetMenuItemID(tosearchsub&, GetString&)
menustring$ = String(100, " ")
getstringmenu& = GetMenuString(tosearchsub&, subcount&, menustring$, 100, 1)

If InStr(UCase(menustring$), UCase(StringSearch)) Then
MenuItem& = subcount&
GoTo MatchString
End If

Next GetString&

Next findstring&
Exit Sub
RunTheMenu& = PostMessage(Application, WM_COMMAND, MenuItem&, 0&)
End Sub

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement