-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.xaml.cs
217 lines (169 loc) · 6.89 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using Handheld_Hardware_Tools.AppWindows.MainWindow;
using Handheld_Hardware_Tools.Classes;
using Handheld_Hardware_Tools;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Threading;
using Handheld_Hardware_Tools.Classes.Devices;
using System.Diagnostics;
namespace Handheld_Hardware_Tools
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public Device device = null;
public SplashScreenStartUp splashWindow;
public static System.Windows.Forms.NotifyIcon icon;
public Thread splashScreenThread = null;
public App()
{
//run this so that everything shuts down when the main window closes
Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
SetUpNotifyIcon();
//string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
//string stringsFile = Path.Combine(dir, "Styles", "DefaultTheme.xaml");
//LoadStyleDictionaryFromFile(stringsFile);
}
private void SetUpNotifyIcon()
{
App.icon = new System.Windows.Forms.NotifyIcon();
icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.BaseDirectory + "Handheld Hardware Tools.exe");
icon.Visible = true;
icon.Text = "Handheld Hardware Tools";
icon.Click += Icon_Click;
}
private void Icon_Click(object? sender, EventArgs e)
{
System.Windows.Forms.MouseEventArgs mouseEventArgs = (System.Windows.Forms.MouseEventArgs)e;
if (mouseEventArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
QuickAccessMenu qam = Local_Object.Instance.GetQAMWindow();
if (qam != null)
{
qam.ToggleWindow();
}
}
}
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string message = "An unhandled exception just occurred: " + e.Exception.Message + ". Stack Trace: " + e.Exception.StackTrace + ". Source: " + e.Exception.Source + ". Inner Exception: " + e.Exception.InnerException;
MessageBox.Show(message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
Log_Writer.Instance.writeLog(message);
Environment.Exit(0);
}
protected override async void OnStartup(StartupEventArgs e)
{
CheckMultipleHHTProcessRunning();
//determine if this is an autostart by checking if running from system32
bool quietStart = false;
//if start is from system32 (task scheduled start) then set quietStart to true, means auto start
if (String.Equals("C:\\Windows\\System32", Directory.GetCurrentDirectory(), StringComparison.OrdinalIgnoreCase))
{
quietStart = true;
}
//lets check if quiet start is enabled (no splashscreen)
Settings settings = (Settings)XML_Management.Instance.LoadXML("Settings");
//run start up routines
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
device = new Device_Management().device;
SetDefaultTDP(settings.defaultTDP);
}));
QuickAccessMenu qam = null;
//Thread splashScreenThread = null;
if (!settings.hideSplashScreen && !quietStart)
{
//if not quiet start then show splashscreen using separate thread so we can load the
//QAM on the UI thread but at the same time not block a UI thread for the splashscreen so the window loading spins :) <-- smiley face for me
splashScreenThread = new Thread(StartSplashScreenThread);
splashScreenThread.SetApartmentState(ApartmentState.STA);
splashScreenThread.IsBackground = true;
splashScreenThread.Start();
qam = new QuickAccessMenu();
}
else
{
qam = new QuickAccessMenu();
}
this.MainWindow = qam;
qam.Show();
if (quietStart)
{
qam.Hide();
}
else
{
if (settings.launchLargeWindow)
{
qam.Hide();
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
else
{
qam.Show();
}
}
}
private void StartSplashScreenThread()
{
splashWindow = new SplashScreenStartUp();
splashWindow.Show();
System.Windows.Threading.Dispatcher.Run();
}
private void SetDefaultTDP(int defaultTDP)
{
if (defaultTDP != 0)
{
TDP_Management.Instance.ChangeSustainedBoostTDP(defaultTDP, defaultTDP);
}
}
private void CheckMultipleHHTProcessRunning()
{
Process[] hhtProcesses = Process.GetProcessesByName("Handheld Hardware Tools");
if (hhtProcesses != null)
{
if (hhtProcesses.Length > 1)
{
this.Shutdown();
}
}
}
public void CancelSplashScreen()
{
if(splashWindow != null)
{
splashWindow.Dispatcher.Invoke(() => splashWindow.Close());
}
}
/// <summary>
/// This funtion loads a ResourceDictionary from a file at runtime
/// </summary>
public void LoadStyleDictionaryFromFile(string inFileName)
{
if (File.Exists(inFileName))
{
try
{
using (var fs = new FileStream(inFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Read in ResourceDictionary File
var dic = (ResourceDictionary)XamlReader.Load(fs);
// Clear any previous dictionaries loaded
Resources.MergedDictionaries.Clear();
// Add in newly loaded Resource Dictionary
Resources.MergedDictionaries.Add(dic);
}
}
catch
{
}
}
}
}
}