Indigo
Indigo

Reputation: 2997

How to open an application on active display while using Process.Start()?

I am using

System.Diagnostics.Process.Start(ProcessInfo);

to open a TEXT file in notepad from within my windows form application.

Detailed code snippet is

ProcessStartInfo PSI = new ProcessStartInfo("notepad.exe", LogFile);
PSI.WindowStyle = ProcessWindowStyle.Normal;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);

However, it opens notepad on the default desktop but not on the extended desktop on which the main application is running.

Now, the question is, how to open notepad on the active desktop i.e. Window on which the current application is running?

Upvotes: 1

Views: 2279

Answers (1)

Michael Edenfield
Michael Edenfield

Reputation: 28338

Other that specifying the initial window state (normal, hidden, etc), you have basically no control over how the newly launched application starts up and where it shows itself.

The best bet here is to launch the application, then use its window handle to tell it to move. This all requires using P/Invoke, to call MoveWindow. The C# signatures for all of those functions are on pinvoke.net.

Here's a very simple (VB.NET) example that shows the basic idea. In this case, as @Lloyd points out, the window handle you need is the Process.MainWindowHandle you get back from Process.Start. Note that Process.MainWindowHandle isn't filled in immediately; you typically need to call WaitForInputIdle to make sure the window actually gets created. If MainWindowHandle is 0 then you'll know it's too soon.

Upvotes: 1

Related Questions