Click to See Complete Forum and Search --> : Communication with another Window in the desktop
dalys
February 11th, 2003, 10:42 AM
Hi,
I am trying to access the menu items of another window in the desktop. This particular window has no menu assigned to it.
Instead the menu bar of this window exists as a window itself, descended from the parent window. I have observed that the same structure exists for Microsoft Visual Studio and Microsoft Word.
But, for windows like Notepad, there is a menu bar assigned to the parent window. Because of this, the following code helps me to access the menu items of Notepad window:
HWND noteHwnd = ::FindWindow(NULL,_T("Untitled - Notepad"));
HMENU hMainMenu = ::GetMenu(noteHwnd);
HMENU hSubMenu = ::GetSubMenu(hMainMenu,0);
UINT menuId = ::GetMenuItemID(hSubMenu,1);
::SendMessage(noteHwnd,WM_COMMAND,menuId,0);
But, the same procedure if applied to Microsft Visual Studio etc, gives me NULL as the Menu handle. As my required window is similar to Visual Studio, it faces the same problem.
Can anyone tell me how should I go about to get the menu handle for such class of windows, so that I can access the menu items in such windows?
Thankyou,
Dalys
Caprice
February 12th, 2003, 07:41 AM
The way is more complicated like the Studio is the more complicated application compare to Notepad. I'm afraid you need to inject your code into a process where you want to destroy :) the menu. Richter will be helpful.
Valen
February 12th, 2003, 08:43 AM
After you get a handle to the parent window use EnumChildWindows to get a handle to the window associated with the child windor containing the menubar. At that point you should be able to send menu messages to the child window. I've found this to work with dockable toolbars, though I've never tried it with a dockable menu. They should work in much the same fashion though.
dalys
February 12th, 2003, 09:16 AM
Thankyou very much for your replies. I am sure you would be able to help, if you have a similar experience with dockable toolbars. I will explain my problem in more detail.
I have tried getting the handle of the child window of the menu bar and I have succeeded in that. I had not used EnumWindows, but GetDlgItem, to retrieve the handle of the child window corresponding to menu bar.
But, my objective is to popup each submenu of the concerned application by sending windows messages from a different application. My difficulties are:
1) How will I popup a submenu of the menubar, given the handle of the window containing the menu bar? How will I retirieve the handle to the submenu from the handle of the window? The function GetSubMenu did not work for me.
2) After this, how will I access the menu item in each submenu?
- All this have to be done from a different application.
Below I list down the code which I used for this purpose:
HWND parentHwnd = ::FindWindow(NULL,_T("Target - App"));
HWND childHwnd = ::GetDlgItem(parentHwnd, childWindowId);
HWND menuHwnd = ::GetDlgItem(childHwnd,menuWindowId);
HMENU hmenu = ::GetMenu(menuHwnd);
HMENU hSubMenu = ::GetSubMenu(hMenu,0);
The childwindowId and the menuWindowId were retrieved from Spy+ tool. The first 3 lines of code gave me the handle of the window containing the menu bar. I verified this from Spy+. The 4rth line of code didnot work, as the menu handle I got was the menuWindowId which could be wrong. The 5th line of code returned me a NULL handle.
It would be very helpful, if you tell me where I have gone wrong.
Dalys
hankdane
February 12th, 2003, 12:53 PM
Probably Visual Studio does its own menu implementation in order to get special effects on older systems, for instance, having 3d menus in NT4. You cannot use menu calls, such as GetSubMenu(), on proprietary menu implementations.
I'm not sure what you're trying to accomplish, but a couple of suggestions:
1. If you just want to issue the command to VS, send the appropriate WM_COMMAND message. (Use Spy++ to figure out what it is).
2. If you want to show a menu, create a menu and show it with a call to something like TrackPopupMenu, then proceed with 1. to issue the command.
dalys
February 12th, 2003, 02:05 PM
Hi Henri Hein,
I will give more details of my program.
I am working on a voice recognizer application, which has to pass the recognized voice commands to the target application and thus replicate most operations at the target application as if the user had clicked a mouse there. So if the user says 'File', the 'File' popup menu in the target application should come up. Similarly, if the user say 'Edit', the 'Edit' popup menu should come up at the target application.
I see some difficulties in doing your suggestions:
1) I cannot send a WM_COMMAND to a pop-up menu to activate it. When a popup menu (like 'File',"Edit') is activated, I find that the application donot receive a WM_COMMAND. My feeling is that WM_COMMAND is received only when an user selects a menuitem.
When an user selects a popmenu, I find that the application get WM_INITMENU and WM_INITPOPUP.
2) To use TrackPopupMenu, I need to have the x and y co-ordinates which is possible if I use the mouse at the traget application. As mouse click is unavailble in this case, I cannot use it either.
Thankyou,
Dalys
hankdane
February 12th, 2003, 10:57 PM
Originally posted by dalys
I am working on a voice recognizer application, which has to pass the recognized voice commands to the target application and thus replicate most operations at the target application as if the user had clicked a mouse there.
In that case, my only suggestion is to use a journalling hook to send keystrokes to the window corresponding to the command spoken. That would make the functionality more generic anyway, not tied to one application. (See SetWindowsHookEx for a beginning on hooks).
I also want to mention that there are some generic accessibility APIs in Windows. They are generally aimed at exactly this sort of thing, ie., overloading some of the standard input and rendering functions. I'm not that familiar with them, but I think you may want to look into that. Some of them uses hooks, so you may get around to that anyway. Search on accessibility in MSDN and see what comes up.
Good luck.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.