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

后端 未结 1 1490
孤城傲影
孤城傲影 2021-01-23 20:59

I am using

System.Diagnostics.Process.Start(ProcessInfo);

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

Detai

相关标签:
1条回答
  • 2021-01-23 21:32

    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.

    0 讨论(0)
提交回复
热议问题