c#防软件多开源码
By
admin
at 2023-08-23 • 0人收藏 • 330人看过
打开program.cs启动文件, 修改里面的代码:
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { internal static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool createdNew; Mutex mutex = new Mutex(true, "MyAppMutexabc", out createdNew); if (!createdNew) { // 应用程序已经在运行中,激活其主窗口 Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); if (processes.Length > 0) { Process process = processes[0]; ShowWindowAsync(process.MainWindowHandle, SW_RESTORE); SetForegroundWindow(process.MainWindowHandle); } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new mainForm());//Form1 mutex.ReleaseMutex(); } [DllImport("user32.dll")] static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); const int SW_RESTORE = 9; } }
登录后方可回帖