1.添加系统DLL引用
[DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
2.得到窗口句柄
应用程序运行时
Process instance = null;
Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历与当前进程名称相同的进程列表 foreach (Process process in processes) { //如果实例已经存在则忽略当前进程 if (process.Id != current.Id) { //得到已经存在的进程 instance = process;
break;
}
}
应用程序运行中
Process instance = Process.GetCurrentProcess();
3.调用API
SwitchToThisWindow(instance.MainWindowHandle, true);