-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
160 lines (136 loc) · 5.82 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Frogy.Classes;
using Frogy.Views;
using HandyControl.Tools;
using Hardcodet.Wpf.TaskbarNotification;
using Hardcodet.Wpf.TaskbarNotification.Interop;
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.Input;
using System.Threading;
using System.Security.Principal;
using Frogy.Resources.Language;
using Frogy.Methods;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;
namespace Frogy
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private MyAppData appData = new MyAppData();
public MyAppData AppData
{
get { return appData; }
}
private TaskbarIcon taskbarIcon;
private static Mutex mutex;
public App()
{
Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}
private void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
appData.StopLogic();
if (appData.Saving)
e.Cancel = true;
else
Environment.Exit(0);
}
protected override void OnStartup(StartupEventArgs e)
{
//Launage switch
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri(@"Resources\Language\" + LanguageHelper.PreferenceLanguage + ".xaml", UriKind.Relative);
ConfigHelper.Instance.SetLang(LanguageHelper.PreferenceLanguage == "zh-CN" ? "zh-cn" : "en");
Current.Resources.MergedDictionaries.Add(dict);
//theme switch
ResourceDictionary themeDictionary = new ResourceDictionary();
ResourceDictionary chartDictionary = new ResourceDictionary();
switch (appData.ThemeSetting)
{
case 0:
themeDictionary.Source = new Uri(@"pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml", UriKind.Absolute);
chartDictionary.Source = new Uri(@"Resources\Theme\DefaultTheme.xaml", UriKind.Relative);
break;
case 1:
themeDictionary.Source = new Uri(@"pack://application:,,,/HandyControl;component/Themes/SkinDark.xaml", UriKind.Absolute);
chartDictionary.Source = new Uri(@"Resources\Theme\DarkTheme.xaml", UriKind.Relative);
break;
default: //bright default
themeDictionary.Source = new Uri(@"pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml", UriKind.Absolute);
chartDictionary.Source = new Uri(@"Resources\Theme\DefaultTheme.xaml", UriKind.Relative);
break;
}
Current.Resources.MergedDictionaries.Add(themeDictionary);
Current.Resources.MergedDictionaries.Add(chartDictionary);
//tray icon
mutex = new Mutex(true, "FrogyMainProgram");
string startupArg = e.Args.Count() > 0 ? e.Args[0] : null;
//if frogy not running
if (mutex.WaitOne(0, false) || startupArg == "restart")
{
//Promote permission if not Administrator
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
MyDeviceHelper.PromotePermission();
taskbarIcon = (TaskbarIcon)FindResource("icon");
ShowNotification(
LanguageHelper.InquireLocalizedWord("TaskBar_AppName"),
LanguageHelper.InquireLocalizedWord("TaskBar_StartUp"),
BalloonIcon.Info);
appData.InitializeMyAppData();
appData.StartLogic();
base.OnStartup(e);
}
else
{
MessageBox.Show(
LanguageHelper.InquireLocalizedWord("TaskBar_Overlap"),
LanguageHelper.InquireLocalizedWord("TaskBar_AppName"),
MessageBoxButton.OK,
MessageBoxImage.Information);
Environment.Exit(1);
}
}
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
}
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(LanguageHelper.InquireLocalizedWord("System_ExcptionInfo") + e.Exception.Message + sender.ToString(),
LanguageHelper.InquireLocalizedWord("TaskBar_AppName"),
MessageBoxButton.OK,
MessageBoxImage.Error);
Environment.Exit(1);
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
protected override void OnExit(ExitEventArgs e)
{
Current.DispatcherUnhandledException -= Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
AppDomain.CurrentDomain.ProcessExit -= new EventHandler(CurrentDomain_ProcessExit);
appData.Save();
base.OnExit(e);
}
public void ShowNotification(string title, string content, BalloonIcon icon)
{
taskbarIcon.ShowBalloonTip(
title,
content,
icon);
}
}
}