StableVersion4.3/HL_FristAidPlatform_Frame/Program.cs

104 lines
4.1 KiB
C#

using DevExpress.XtraEditors;
using HL_FristAidPlatform_Public;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
namespace HL_FristAidPlatform_Frame
{
/// <summary>
/// 应用程序的主入口点
/// </summary>
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.UserSkins.BonusSkins.Register();
//取消重复打开软件判断,减少系统首次加载时间
//int count = 0;
//Process[] myProcess = Process.GetProcesses();
////Process MyProcess = null;
//foreach (Process _Process in myProcess)
//{
// if (_Process.ProcessName == Process.GetCurrentProcess().ProcessName)
// {
// count++;
// //MyProcess = _Process;
// //XtraMessageBox.Show("当前进程ID:" + MyProcess.Id.ToString());
// }
//}
//if (count > 1)
//{
// //HandleRunningInstance(MyProcess);
// XtraMessageBox.Show("请勿重复打开软件!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//}
//else
//{
Action run = () =>
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form_Login login = new Form_Login();
if (login.ShowDialog() == DialogResult.OK)
{
Application.Run(new Form_Main());
}
};
WindowsIdentity wi = WindowsIdentity.GetCurrent();
bool runAsAdmin = wi != null && new WindowsPrincipal(wi).IsInRole(WindowsBuiltInRole.Administrator);
if (!runAsAdmin)
{
try
{
//不可能以管理员方式直接启动一个 ClickOnce 部署的应用程序,所以尝试以管理员方式启动一个新的进程
Process.Start(new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase) { UseShellExecute = true, Verb = "runas" });
}
catch (Exception ex)
{
//MessageBox.Show(string.Format("以管理员方式启动失败,将尝试以普通方式启动!{0}{1}", Environment.NewLine, ex), "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error);
PublicClass.WriteErrorLog("Main", "以管理员方式启动失败,将尝试以普通方式启动!" + ex.Message);
run();//以管理员方式启动失败,则尝试普通方式启动
}
Application.Exit();
}
else
{
run();
}
//}
}
#region 打开之前的程序
// 指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 3;
/// <summary>
/// 如果有另一个同名进程启动,则调用之前的实例
/// </summary>
/// <param name="instance"></param>
private static void HandleRunningInstance(Process instance)
{
XtraMessageBox.Show(instance.MainWindowHandle.ToString());
// 确保窗体不是最小化或者最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
// 将之前启动的进程实例弄到前台窗口
XtraMessageBox.Show(SetForegroundWindow(instance.MainWindowHandle).ToString());
}
#endregion
}
}