I am trying to control another application with my C# code on Win 7.
I need to programmatically click an option in the menu
I am successfully sending keys to that application, or pressing a "stand-alone" button in that application with:
SendMessage
Spy++ gives me a button's handle, so that SendMessage can hit that button, however I am not able to figure out how to get a handle to the menu item and hit that option (I tried iterating though children of he parent handle, but no luck).
Related
What I want to do:
I want to create a custom menu item (along with minimize, maximize, close, etc.) when a user right-clicks on a window's title bar. Additionally, I want the user to be able to click on my menu item and trigger my code. Also, I want to do this for every newly opened windows. For example, when the user opens up a new notepad window and right clicks on its title bar, he/she will see: Restore, Move, Maximize, Minimize, Foo (my menu item), Close. Once they click on "Foo", my code gets called.
What I have so far:
I am using SetWinEventHook to hook the EventObjectCreate event. I am also following this guide to insert my own menu item (via GetSystemMenu and AppendMenu).
My question:
So now I can successfully inject my own menu item onto newly created windows, but nothing happens when the user clicks on the item. How do I register my function to this menu item when it gets clicked?
P.S. I'm doing this in C# using Pinvoke, not sure if this matters.
You have to hook the windows' window proc (also known as subclassing the window) and handle the WM_SYSCOMMAND message. You check the wParam parameter and call your function if the menu item selected is your custom item.
You're going to run into interesting problems. See, for example, Subclass a native application from C# and Subclassing a external window in C# .NET.
It can be done, but it's not simple.
You might also be interested in http://www.codeproject.com/Articles/3234/Subclassing-in-NET-The-pure-NET-way.
I am in a need of hiding the console window, which I quickly resolved by P/Invoking ShowWindow. However, the call hides the console window as well as the taskbar button of the console window. I need the taskbar button to remain visible. I am not, however, looking for a way to minimize the console window. Clicking the taskbar button show not do anything. I tried to use SetWinEventHook and hide the window every time it was activated, but to no avail, as the window calling this function must run message loop in order to receive the events (and my console window didn't, no matter if I set OutOfContext or InContext flag).
Is there a way I can make my console application run, show itself in taskbar, then hide itself (or never show itself in a first place) and just keep running with no window but taskbar button visible?
The normal way to get a button on the taskbar is to create a visible unowned top-level window. So that's no use to you. One possible alternative is to use ITaskbarList::AddTab to add a button. I don't know whether or not this will do anything for an invisible window. Either way you'd need to run a message loop for your window. Even if you could do this the net result would not feel very nice for the user.
I'm working on a program to simultaneously control a handful of other programs of the same type.
So far I use EnumWindows() and collect the handles of the appropriate windows(programs) that I'm going to be controlling. Upon recognizing some keyboard keystroke, I iterate through all of the programs(there could be any number of them open) and I post click messages to all of these programs simultaneously by using EnumChildWindows() to find the appropriate windows(buttons) based on which keystroke was pressed and then using PostMessage() to actually send the click.
Each of these programs that I'm controlling has a button named "Load Settings" which opens an Open file dialog when clicked. What I would like to do is first use PostMessage() to click "Load Settings"(I have successfully gotten this far, what follows is what I'm trying to do). Then I want to get the handle of that Open file dialog window, fill in the text field with the path to the appropriate settings file that I've already determined, then click the "Open" button to finish the task and close the Open window.
My main problem is finding that Open window. Now, I can use EnumWindows once again to find that window but the problem with this is upon clicking "Load Settings" the Open window doesn't open instantaneously. I could sleep after clicking the button, then enum through the windows to find it, but that's not practical as the window could take any amount of time to open.
I've used spy++ to gather some information about this window, but as far as I know, none of it helps enough. Anyways, here is what I have found:
The window "Open" is the Previous Window to the program which it was opened from.
The window "Open"'s parent is the program which it was opened from.
The window "Open"'s owner is the program which it was opened from.
But even though it's listed as a child of the main program under its window properties, it's listed on the same level as the main program (it's not nested like other child windows).
I've tried FindWindowEx() and EnumChildWindows() but neither could find the Open window.
I would think there should be some way to, based on some handle, get the handle of the previous window.
Once that's working, then I need to fill the Edit(text field) window with the appropriate file path.
UIA is what you want, if your goal is to automate UI actions (e.g. for testing). UIA exposes the entire UIA tree which contains all applications active on the machine.
So what you'll see is a root node which has one child node for each application. Each of those nodes should represent the top level window of that application. Any further children depend on the application itself.
See more info at UIA Overview
I'm writing a program in C# to automate another application. I use user32 api. When the external application needs user input, my C# program fills the boxes and presses the buttons. When a new window comes up, I can find it and it's controls using findwindow() and findwindowex() function but when this dialog comes up (in the picture) I cannot find the controls in it(the buttons). Even UI Spy cannot show them. I tried to send key events to this window but nothing happened. Can you give me some tips how to go on?
I am trying to do something with the Modal dialog in Win7. To do that I need to know which control window has got focus right now by using Win32 API. If I could know which control is now got focus then I can send message to this window to do some specific task.
Could any one suggest me how should I know which control has got focus right now using win 32?
If you want to know which window has keyboard focus and you're only interested in windows belonging to your own thread use GetFocus.
Finding out which window in the entire system has focus requires a few steps:
GetForegroundWindow
GetWindowThreadProcessId
GetGUIThreadInfo