启动计算器,并获取焦点
using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { class Class1 { [STAThread] static void Main(string[] args) { System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); myProcess.StartInfo.FileName = "calc"; myProcess.Start(); myProcess.WaitForInputIdle(2000); IntPtr hWnd = myProcess.MainWindowHandle; Console.WriteLine(hWnd); bool p = SetForegroundWindow(hWnd); if(!p) Console.WriteLine("Could not set focus"); } [DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet=CharSet.Auto,SetLastError=true)] public static extern IntPtr SetFocus(IntPtr hWnd); } }
出处:https://bytes.com/topic/c-sharp/answers/436986-process-setfocus