using DaJiaoYan.Utils; using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; namespace DaJiaoYan { internal static class Program { [STAThread] [DllImport("User32.dll")] private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(System.IntPtr hWnd); /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Process current = Process.GetCurrentProcess(); Process instance = ProcessExts.RunningInstance(ref current); if (instance == null) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); } else { HandleRunningInstance(instance); } } #region 确保程序只运行一个实例 private static void HandleRunningInstance(Process instance) { MessageBox.Show("程序已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口 SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端 } #endregion } }