2025-03-28 09:49:56 +08:00

208 lines
7.4 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace TouchCashier
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
System.Threading.Mutex mutex;
public App()
{
//主线程非预期异常处理事件,记录异常日志,重启应用程序
DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
//子线程非预期异常处理事件,记录异常日志
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//系统正在关机或注销时事件,记录关闭信息
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
//程序运行事件
Startup += new StartupEventHandler(App_Startup);
}
/// <summary>
/// 系统关机或注销事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
SessionEndReasons reason = e.Reason;
switch (reason)
{
case SessionEndReasons.Logoff:
WorkUtils.PublicStore.LoggerHelper.WriteLogger("Windows正在注销。");
break;
case SessionEndReasons.SystemShutdown:
WorkUtils.PublicStore.LoggerHelper.WriteLogger("Windows正在关闭。");
break;
}
}
/// <summary>
/// UI线程异常处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
WorkUtils.PublicStore.LoggerHelper.WriteLogger($"主线程发生异常,重新启动系统。错误信息:{e.Exception.Message}");
e.Handled = true;
try
{
//重启系统
System.Diagnostics.Process.Start("AutoUpdateEx.exe");
System.Environment.Exit(0);
}
catch (Exception) { System.Environment.Exit(0); }
}
/// <summary>
/// 非UI线程异常处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
WorkUtils.PublicStore.LoggerHelper.WriteLogger($"子线程发生异常,重新启动系统。错误信息:{(e.ExceptionObject as Exception).Message}");
try
{
//重启系统
System.Diagnostics.Process.Start("AutoUpdateEx.exe");
System.Environment.Exit(0);
}
catch (Exception) { System.Environment.Exit(0); }
}
/// <summary>
/// 程序启动处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void App_Startup(object sender, StartupEventArgs e)
{
mutex = new System.Threading.Mutex(true, "TouchCashier", out bool ret);
if (!ret)
{
Environment.Exit(0);
}
else
{
MainFontSizeChange();
SecondMainFontSizeChange();
MainWindow _MainWindow = new MainWindow();
_MainWindow.Show();
}
}
/// <summary>
/// 主屏幕窗口字体大小计算
/// </summary>
private void MainFontSizeChange()
{
//超大号字体
double supersize = ((SystemParameters.PrimaryScreenWidth / 6.5) / 3 * 2) / 5;
if (supersize < 28)
{
supersize = 28;
}
Current.Resources.Remove("SuperFontSize");
Current.Resources.Add("SuperFontSize", supersize);
double titlesize = ((SystemParameters.PrimaryScreenWidth / 10) / 3 * 2) / 5;
if (titlesize < 18)
{
titlesize = 18;
}
Current.Resources.Remove("TitleFontSize");
Current.Resources.Add("TitleFontSize", titlesize);
double tabsize = ((SystemParameters.PrimaryScreenWidth / 10) / 3 * 2) / 5 * 0.9;
if (tabsize < 16)
{
tabsize = 16;
}
Current.Resources.Remove("TabFontSize");
Current.Resources.Add("TabFontSize", tabsize);
double gridsize = ((SystemParameters.PrimaryScreenWidth / 10) / 3 * 2) / 5 * 0.8;
if (gridsize < 14)
{
gridsize = 14;
}
Current.Resources.Remove("GridFontSize");
Current.Resources.Add("GridFontSize", gridsize);
double controlsize = ((SystemParameters.PrimaryScreenWidth / 10) / 3 * 2) / 5 * 0.7;
if (controlsize < 12)
{
controlsize = 12;
}
Current.Resources.Remove("ControlFontSize");
Current.Resources.Add("ControlFontSize", controlsize);
}
/// <summary>
/// 副屏幕窗口字体大小计算
/// </summary>
private void SecondMainFontSizeChange()
{
double db_ScreenWidth = SystemParameters.PrimaryScreenWidth;
if (System.Windows.Forms.Screen.AllScreens.Length > 1)
{
db_ScreenWidth = System.Windows.Forms.Screen.AllScreens[1].Bounds.Width;
}
//超大号字体
double supersize = ((db_ScreenWidth / 6.5) / 3 * 2) / 5;
if (supersize < 28)
{
supersize = 28;
}
Current.Resources.Remove("SecondSuperFontSize");
Current.Resources.Add("SecondSuperFontSize", supersize);
double titlesize = ((db_ScreenWidth / 9) / 3 * 2) / 5;
if (titlesize < 20)
{
titlesize = 20;
}
Current.Resources.Remove("SecondTitleFontSize");
Current.Resources.Add("SecondTitleFontSize", titlesize);
double tabsize = ((db_ScreenWidth / 10) / 3 * 2) / 5;
if (tabsize < 16)
{
tabsize = 16;
}
Current.Resources.Remove("SecondTabFontSize");
Current.Resources.Add("SecondTabFontSize", tabsize);
double gridsize = ((db_ScreenWidth / 10) / 3 * 2) / 5 * 0.8;
if (gridsize < 14)
{
gridsize = 14;
}
Current.Resources.Remove("SecondGridFontSize");
Current.Resources.Add("SecondGridFontSize", gridsize);
double controlsize = ((SystemParameters.PrimaryScreenWidth / 10) / 3 * 2) / 5 * 0.7;
if (controlsize < 12)
{
controlsize = 12;
}
Current.Resources.Remove("SecondControlFontSize");
Current.Resources.Add("SecondControlFontSize", controlsize);
}
}
}