首页 > 解决方案 > 如何将重点放在流程上?

问题描述

我有一个名为 _program 的 System.Diagnostics.Process 变量。

我知道不是每个进程都有用户界面,所以它永远不会有焦点(我认为)。

但是,假设这个进程有一个接口,是否可以设置焦点呢?也许我需要使用 Process.StandardInput?

标签: c#wpf

解决方案


使用 PInvoke 调用原生函数来设置前台窗口:

using System.Runtime.InteropServices;

DllImport("User32.dll")]
private static extern Int32 SetForegroundWindow(int hWnd);

void YourMethod()
{
  Process p = ... //However you create your process
  SetForegroundWindow(p.MainWindowHandle);  //Set this process's main window as the foreground window
}

推荐阅读