How can I access to the controls on another application's window?
I need to change the value of that controls (like textboxes) or click on them (like buttons).
I think I Should use API functions? But how?
Asked
Active
Viewed 2,658 times
3 Answers
3
Look for "Spy++": http://msdn.microsoft.com/en-us/library/dd460756.aspx
You can access controls on other windows with it.

Headpuster
- 267
- 1
- 2
- 9
-
It can find them, but how can I click on a control? By 'post message'? how? – mrbm Jan 18 '12 at 11:12
3
See the SendKeys class and read this article, here is an example from the article:
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the Calculator application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
// Verify that Calculator is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
// Make Calculator the foreground application and send it
// a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}

CloudyMarble
- 36,908
- 70
- 97
- 130
-
1Its good but is it safe? I mean a should use tab key to move throw controls, but what if some keys don't work? then I will change the value of another control! I'm looking for a very safe way! – mrbm Jan 18 '12 at 11:05
-
It depends what are you going to use it for? there may be better options depending on if the other window you would like to access is urs or not, you may use something like: http://inputsimulator.codeplex.com/ to simulate input on the same window, or a script program to record a macro which simulates this like: http://mwganson.freeyellow.com/winkeysim/ whats the goal of all this? – CloudyMarble Jan 18 '12 at 11:15
-
1No that window is not mine. I need to run and control an application that must be hidden from user, in my own application. – mrbm Jan 18 '12 at 13:46
3
you should use Windows APIs like EnumWindow
or FindWindow
then use EnumChildWindows
API to find the controls in the target window, like the textbox you are looking for, then the API SetWindowText
have a look here for some ideas: Why is EnumChildWindows skipping children? also search for these APIs names here in Stack Overflow and you will find many examples...

Community
- 1
- 1

Davide Piras
- 43,984
- 10
- 98
- 147