1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Reflection;
- using System.Diagnostics;
- using System.Threading;
- using TaskbarGroups.NativeMethods;
- namespace TaskbarGroups
- {
- /// <summary>
- /// enter point
- /// </summary>
- static class Program
- {
- /// <summary>
- /// 全局窗体List,便于相互跳转
- /// </summary>
- 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 frmMain());
- }
- else
- {
- SetForegroundWindow(instance.MainWindowHandle);
- }
- }
- private static void SetForegroundWindow(IntPtr mainWindowHandle)
- {
- IntPtr h = Kernel32.OpenProcess(0x1F0FFF, false, Process.GetCurrentProcess().Id);
-
- }
- /// <summary>
- /// 检测是否已经启动了一个Application实例
- /// </summary>
- /// <returns>返回进程id,没有启动返回null</returns>
- private static Process RunningInstance()
- {
- Process current = Process.GetCurrentProcess();
- Process[] processes = Process.GetProcessesByName(current.ProcessName);
- // 遍历正在有相同名字运行的例程
- foreach (Process process in processes)
- {
- // 忽略现有的例程
- if (process.Id != current.Id)
- {
- // 确保例程从EXE文件运行
- if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
- {
- // 返回另一个例程实例
- return process;
- }
- }
- }
- // 没有其它的例程,返回null
- return null;
- }
- }
- }
|