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

坚强是说给别人听的谎言 提交于 2019-12-02 10:23:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14506817/how-to-open-an-application-on-active-display-while-using-process-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!