C#啓動計算器並設計算器爲活動窗口

啓動計算器,並獲取焦點spa

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-setfocuscode

相關文章
相關標籤/搜索