123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using BatteryIndicator.Views;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- namespace BatteryIndicator
- {
- static class Program
- {
- public static List<Form> formList = new List<Form>();
- /// <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;
- }
- /// <summary>
- /// new a Form object
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- internal static Form getInstance(Type type) {
- Form form = null;
- foreach(Form formItem in Program.formList)
- {
- if (form.GetType() == type)
- {
- form = formItem;
- form.Activate();
- }
- else if (form == null)
- {
- object obj = Activator.CreateInstance(type);
- if (obj is Form)
- {
- form = obj as Form;
- form.Show();
- }
- }
- }
- return form;
- }
- }
- }
|