[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
static void Main(string[] args)
{
Process p = Process.GetProcessesByName("QQ").FirstOrDefault();
IntPtr myPtr = p.MainWindowHandle;
判断这个窗体是否有效
if (myPtr != IntPtr.Zero)
{
Console.WriteLine("找到窗口");
SetForegroundWindow(myPtr);
ShowWindow(myPtr,3);
}
else
{
Console.WriteLine("没有找到窗口");
}
Console.ReadKey();
}