使用Process.Start()时如何在活动显示上打开应用程序? [英] How to open an application on active display while using Process.Start()?

查看:63
本文介绍了使用Process.Start()时如何在活动显示上打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

System.Diagnostics.Process.Start(ProcessInfo);

从我的Windows窗体应用程序中在记事本中打开一个TEXT文件.

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

详细的代码段为

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?

推荐答案

除了指定初始窗口状态(正常,隐藏等)外,您基本上无法控制新启动的应用程序的启动方式以及显示位置

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.

这里最好的选择是启动应用程序,然后使用其窗口句柄告诉它移动.所有这些都需要使用P/Invoke来调用 pinvoke.net 上.

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.

这是一个非常简单的(VB.NET)示例,其中显示了基本概念.在这种情况下,正如@Lloyd指出的那样,您需要的窗口句柄是从 Process.Start 返回的 Process.MainWindowHandle .请注意, Process.MainWindowHandle 不会立即填写.您通常需要调用 WaitForInputIdle 以确保实际上已创建窗口.如果 MainWindowHandle 为0,那么您就会知道它为时过早.

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.

这篇关于使用Process.Start()时如何在活动显示上打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆