123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //using DevExpress.Compression;
- using DevExpress.Compression;
- using DevExpress.LookAndFeel;
- using DevExpress.XtraRichEdit.Model;
- using Ips.Library.Basic;
- using Ips.Library.DxpLib;
- using Ips.Library.DxpLib.Properties;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Ips.Library;
- public class AppHelper
- {
- public const string CultureName = "zh-CN";
- public static void InitAppSettings()
- {
- Application.ThreadException += Application_ThreadException;
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
- AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
- DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("微软雅黑", 9);
- DevExpress.Skins.SkinManager.EnableFormSkins();
- Application.ApplicationExit += (sender, e) =>
- {
- ToolConfig.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName);
- ToolConfig.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName);
- ToolConfig.SetAppSetting("CompactMode", UserLookAndFeel.Default.CompactUIModeForced);
- };
- string skinName = ToolConfig.GetAppSetting("SkinName").IfNullOrWhitespace("WXI");
- string paletteName = ToolConfig.GetAppSetting("PaletteName");
- bool compactMode = ToolConfig.GetAppSetting("CompactMode").To(true);
- if (compactMode)
- UserLookAndFeel.ForceCompactUIMode(true, false);
- if (!string.IsNullOrEmpty(paletteName))
- UserLookAndFeel.Default.SetSkinStyle(skinName, paletteName);
- else UserLookAndFeel.Default.SetSkinStyle(skinName);
- CultureInfo culture = CultureInfo.CreateSpecificCulture(CultureName);
- Application.CurrentCulture = culture;
- Thread.CurrentThread.CurrentUICulture = culture;
- Thread.CurrentThread.CurrentCulture = culture;
- CultureInfo.DefaultThreadCurrentCulture = culture;
- CultureInfo.DefaultThreadCurrentUICulture = culture;
- UserLookAndFeel.Default.StyleChanged += (sender, e) =>
- {
- ToolConfig.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName);
- ToolConfig.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName);
- };
- }
- static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
- {
- string errMsg = "发生未知错误,请联系管理员!" + e.Exception?.Message;
- MsgHelper.ShowError(errMsg, e.Exception);
- }
- static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- var ex = e.ExceptionObject as Exception;
- if (ex == null) return;
- MsgHelper.ShowError("发生未知错误,程序即将退出!" + ex.Message, ex);
- }
- public static void OneStart(string appName, Action act)
- {
- using (Mutex mutex = new Mutex(true, appName))
- {
- if (!mutex.WaitOne(0, false))
- {
- Process currentProcess = Process.GetCurrentProcess();
- Process[] processesByName = Process.GetProcessesByName(currentProcess.ProcessName);
- bool isShow = false;
- for (int i = 0; i < processesByName.Length; i++)
- {
- Process process = processesByName[i];
- if (process.Id != currentProcess.Id && process.MainWindowHandle != IntPtr.Zero)
- {
- isShow = true;
- ToolWinApi.SetForegroundWindow(process.MainWindowHandle);
- ToolWinApi.RestoreWindowAsync(process.MainWindowHandle);
- break;
- }
- }
- if (!isShow)
- {
- MsgHelper.ShowMsg("已有相同程序在运行,不能重复启动!");
- }
- }
- else
- {
- act();
- }
- }
- }
- public static void MultStart(Action act)
- {
- act();
- }
- }
|