Generally, this is quite a bad idea, a dirty field of programming, nothing really reliable or supportable. Some applications develops a feature called "Automation" to be controlled by some external process, but such applications should be specially designed for this and provide appropriate API. To me, automation is still dirty stuff, but it is used sometimes and is considered legitimate.
If this is a general-purpose UI application, and you hard life really forces you into doing bad stuff, you need three things:
1) Run application using
System.Diagnostics.Process.Start
, see
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[
^].
2) When a process is started, you can use the property
Process.MainWindowHandle
to manipulate it using raw Windows API such as
SetForegroundWindow
,
SetActiveWindow
,
GetLastActivePopup
, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[
^] (good in case the application executes some dialog boxes, etc., using P/Invoke (see below). Remember that that handle can by zero, which means that it is not yet created, then you should repeat the call to the property a bit later. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646311%28v=vs.85%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[
^].
3) You can simulate use input on a low level using raw Windows API
SendInput
via P/Invoke, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[
^].
In you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/invoke[
^],
http://msdn.microsoft.com/en-us/library/Aa712982[
^].
This CodeProject article can also be useful:
Essential P/Invoke[
^].
Do you really want all this for the questionable benefit of controlling some stupid UI application? To tell you the truth, I would not bother. :-)
—SA