AppHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //using DevExpress.Compression;
  2. using DevExpress.Compression;
  3. using DevExpress.LookAndFeel;
  4. using DevExpress.XtraRichEdit.Model;
  5. using Ips.Library.Basic;
  6. using Ips.Library.DxpLib;
  7. using Ips.Library.DxpLib.Properties;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. namespace Ips.Library;
  19. public class AppHelper
  20. {
  21. public const string CultureName = "zh-CN";
  22. public static void InitAppSettings()
  23. {
  24. Application.ThreadException += Application_ThreadException;
  25. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  26. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  27. DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("微软雅黑", 9);
  28. DevExpress.Skins.SkinManager.EnableFormSkins();
  29. Application.ApplicationExit += (sender, e) =>
  30. {
  31. ToolConfig.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName);
  32. ToolConfig.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName);
  33. ToolConfig.SetAppSetting("CompactMode", UserLookAndFeel.Default.CompactUIModeForced);
  34. };
  35. string skinName = ToolConfig.GetAppSetting("SkinName").IfNullOrWhitespace("WXI");
  36. string paletteName = ToolConfig.GetAppSetting("PaletteName");
  37. bool compactMode = ToolConfig.GetAppSetting("CompactMode").To(true);
  38. if (compactMode)
  39. UserLookAndFeel.ForceCompactUIMode(true, false);
  40. if (!string.IsNullOrEmpty(paletteName))
  41. UserLookAndFeel.Default.SetSkinStyle(skinName, paletteName);
  42. else UserLookAndFeel.Default.SetSkinStyle(skinName);
  43. CultureInfo culture = CultureInfo.CreateSpecificCulture(CultureName);
  44. Application.CurrentCulture = culture;
  45. Thread.CurrentThread.CurrentUICulture = culture;
  46. Thread.CurrentThread.CurrentCulture = culture;
  47. CultureInfo.DefaultThreadCurrentCulture = culture;
  48. CultureInfo.DefaultThreadCurrentUICulture = culture;
  49. UserLookAndFeel.Default.StyleChanged += (sender, e) =>
  50. {
  51. ToolConfig.SetAppSetting("SkinName", UserLookAndFeel.Default.ActiveSkinName);
  52. ToolConfig.SetAppSetting("PaletteName", UserLookAndFeel.Default.ActiveSvgPaletteName);
  53. };
  54. }
  55. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  56. {
  57. string errMsg = "发生未知错误,请联系管理员!" + e.Exception?.Message;
  58. MsgHelper.ShowError(errMsg, e.Exception);
  59. }
  60. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  61. {
  62. var ex = e.ExceptionObject as Exception;
  63. if (ex == null) return;
  64. MsgHelper.ShowError("发生未知错误,程序即将退出!" + ex.Message, ex);
  65. }
  66. public static void OneStart(string appName, Action act)
  67. {
  68. using (Mutex mutex = new Mutex(true, appName))
  69. {
  70. if (!mutex.WaitOne(0, false))
  71. {
  72. Process currentProcess = Process.GetCurrentProcess();
  73. Process[] processesByName = Process.GetProcessesByName(currentProcess.ProcessName);
  74. bool isShow = false;
  75. for (int i = 0; i < processesByName.Length; i++)
  76. {
  77. Process process = processesByName[i];
  78. if (process.Id != currentProcess.Id && process.MainWindowHandle != IntPtr.Zero)
  79. {
  80. isShow = true;
  81. ToolWinApi.SetForegroundWindow(process.MainWindowHandle);
  82. ToolWinApi.RestoreWindowAsync(process.MainWindowHandle);
  83. break;
  84. }
  85. }
  86. if (!isShow)
  87. {
  88. MsgHelper.ShowMsg("已有相同程序在运行,不能重复启动!");
  89. }
  90. }
  91. else
  92. {
  93. act();
  94. }
  95. }
  96. }
  97. public static void MultStart(Action act)
  98. {
  99. act();
  100. }
  101. }