| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Threading;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using APS7100TestTool.Forms;
- namespace APS7100TestTool
- {
- static class Program
- {
- // 用于单实例检查的 Mutex 名称
- private const string MutexName = "GoodWill_APS7100TestTool_SingleInstance";
-
- // Windows API 用于激活已存在的窗口
- [DllImport("user32.dll")]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
-
- [DllImport("user32.dll")]
- private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
-
- [DllImport("user32.dll")]
- private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
-
- private const int SW_RESTORE = 9;
- private const int SW_SHOW = 5;
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- // 尝试创建 Mutex,如果已存在则说明程序已在运行
- bool createdNew;
- using (Mutex mutex = new Mutex(true, MutexName, out createdNew))
- {
- if (!createdNew)
- {
- // 程序已在运行,尝试激活已存在的窗口
- IntPtr hWnd = FindWindow(null, "固纬电源测试工具");
- if (hWnd != IntPtr.Zero)
- {
- ShowWindow(hWnd, SW_RESTORE);
- SetForegroundWindow(hWnd);
- }
-
- MessageBox.Show("程序已在运行中!\n请查看系统托盘或任务栏。",
- "固纬电源测试工具",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- return;
- }
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.SetHighDpiMode(HighDpiMode.SystemAware);
- Application.Run(new MainForm());
- }
- }
- }
- }
|