static class Program
{
public static string strUserId;
public static string strUserName;
[DllImport("user32.dll")]//设置窗体位活动窗体
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]//设置窗体状态
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private const int SW_RESTORE = 9; //用原来的大小和位置显示一个窗口
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (CheckAppOnlyOne()) //如果程序已经启动..
{
Process current = Process.GetCurrentProcess();
foreach (Process p in Process.GetProcessesByName(current.ProcessName))
{
if (p.Id != current.Id)//会查上来两个 将不是自己的那个显示出来
{
SetForegroundWindow(p.MainWindowHandle);//激活窗体
ShowWindowAsync(p.MainWindowHandle, SW_RESTORE);//设置窗体状态
}
}
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
/// <summary>
/// True表示已经打开, False表示第一次打开
/// </summary>
private static bool CheckAppOnlyOne()
{
bool isok = true;
System.Diagnostics.Process[] localByName = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
//MessageBox.Show(Process.GetCurrentProcess().ProcessName);
if (localByName != null && localByName.Length >= 2)
{
//do nothing
}
else
{
isok = false;
}
return isok;
}
}
窗口最小化后,可在另一程序中点击程序图标,让程序显示出来,唯一程序运行
最新推荐文章于 2022-06-08 09:52:37 发布