窗體啟動(dòng)后,再次啟動(dòng)時(shí)判斷進(jìn)程是否存在,如果已經(jīng)存在則直接拉起當(dāng)前進(jìn)程,前置顯示并聚焦窗口。
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = RunningInstance();
if (instance == null)
{
Application.Run(new Form1(args));
}
else
{
HandleRunningInstance(instance, args);
}
}
private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return process;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance, string[] args)
{
//ShowWindowAsync(instance.MainWindowHandle, 1); //調(diào)用api函數(shù),正常顯示窗口
//SetForegroundWindow(instance.MainWindowHandle); //將窗口放置最前端
IntPtr handle = FindWindow(null, WindowUtils.getWindowTitle(args[7]));
if (handle == IntPtr.Zero)
{
return;
}
SwitchToThisWindow(handle, true);
}