12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using BatteryIndicator.Views;
- using System;
- using System.Diagnostics;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace BatteryIndicator
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Process instance = RunningInstance();
- if (instance == null)
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //Application.Run(new SettingForm());
- TrayIcon trayIcon = new TrayIcon();
- Application.Run();
- }
- else
- {
- // MessageBox.Show("程序已经在运行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- [DllImport("User32.dll", EntryPoint = "FindWindow")]
- private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("User32.dll")]
- private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
- [DllImport("User32.dll")]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
- public 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;
- }
- }
- }
- // 如果没有其他同名进程存在,则返回 null
- return null;
- }
- }
- }
|