【源码】Program启动 两种不同的登录切换窗口的方法

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zone;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace SmsSend
{
static class Program
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

internal static ApplicationContext context = new ApplicationContext(new Login());

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Diagnostics.Process instance = GetRunningInstance();
if (instance == null)
{
//************第一种跳转窗口的方法******************

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login login = new Login();
Application.Run(login);
if (Login.flag == true)
{
Application.Run(new MainFm());
}
//*************第二种跳转窗口的方法*****************
Application.EnableVisualStyles();
Application.Run(context);
//**************************************************

}
else
{
HandleRunningInstance(instance);
}

}
public static Process GetRunningInstance()
{
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;
}
return null;
}
public static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(instance.MainWindowHandle);
}
}
}

posted @ 2011-10-25 18:03  心_远  阅读(322)  评论(0编辑  收藏  举报