-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
247 lines (219 loc) · 9.7 KB
/
MainWindow.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using Windows.UI.Notifications;
using Application = System.Windows.Application;
using Task = System.Threading.Tasks.Task;
namespace TaskbarHider
{
public partial class MainWindow : Window
{
private static readonly string ProcessNamesFile = @$"{AppContext.BaseDirectory}\process_names.txt";
private static List<string> ProcessNamesList = [];
private static List<string> ProcessNamesListAltPos = [];
private static Process? currentGameInstance = null;
private static bool watcherRunning = true;
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, [MarshalAs(UnmanagedType.Bool)] bool bRepaint);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
public MainWindow()
{
// Init stuff
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleUnCaughtException);
Application.Current.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
Dispatcher.UnhandledException += OnDispatcherUnhandledException;
FileStream fs = new(ProcessNamesFile, FileMode.OpenOrCreate); fs.Close();
ReadProcessNames();
try
{
TaskService ts = new();
TaskDefinition startupTask = ts.NewTask();
startupTask.Triggers.Add(new LogonTrigger());
startupTask.Actions.Add(new ExecAction(Environment.ProcessPath, null, null));
startupTask.Principal.RunLevel = TaskRunLevel.Highest;
ts.RootFolder.RegisterTaskDefinition("TaskbarHider", startupTask);
}
catch (Exception _ex) { };
// Get the icon from resource for the tray icon
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/TaskbarHider;component/icon.ico")).Stream;
Icon myIcon = new(iconStream);
iconStream.Close();
// Context menu for the tray icon
ContextMenuStrip trayContextMenu = new();
ToolStripMenuItem editProcessesItem = new("Edit Processes");
ToolStripMenuItem refreshItem = new("Refresh");
ToolStripMenuItem resizeItem = new("Resize");
ToolStripMenuItem exitItem = new("Exit");
// Tray menu function for editProcess
editProcessesItem.Click += (sender, args) =>
{
Process? editor = Process.Start(new ProcessStartInfo()
{
FileName = ProcessNamesFile,
UseShellExecute = true,
});
if (editor == null) return;
ShowToast("Processes Editor", "Waiting for editor to exit.");
editor.WaitForExit();
ReadProcessNames();
};
// Tray menu function for refresh
refreshItem.Click += (sender, args) =>
{
currentGameInstance = null;
ShowToast("Refreshed", "Looking for new process");
};
// Tray menu function for resize
resizeItem.Click += (sender, args) =>
{
if (currentGameInstance == null) return;
if (ProcessNamesListAltPos.Contains(currentGameInstance.ProcessName))
{
MoveWindow(currentGameInstance.MainWindowHandle, 1272, -31, 2576, 1478, true);
}
else
{
MoveWindow(currentGameInstance.MainWindowHandle, 1280, 0, 2560, 1440, true);
}
};
// Tray menu function for exit
exitItem.Click += async (sender, args) =>
{
watcherRunning = false;
await Task.Delay(200);
Taskbar.Show();
Application.Current.Shutdown();
};
// Add items to the context menu
trayContextMenu.Items.Add(editProcessesItem);
trayContextMenu.Items.Add(refreshItem);
trayContextMenu.Items.Add(resizeItem);
trayContextMenu.Items.Add(exitItem);
// Create the tray notify icon
NotifyIcon systemTray = new()
{
Icon = myIcon,
Visible = true,
Text = "TaskbarHider",
ContextMenuStrip = trayContextMenu
};
ProcessWatcher();
ExitGSkill();
HandleEthernet();
}
private static async void ProcessWatcher()
{
// Set default taskbar state to show
Taskbar.Show();
while (watcherRunning)
{
await Task.Delay(10);
if (currentGameInstance != null) goto Check;
// Loop over and check for the running processes
foreach (string game in ProcessNamesList)
{
Process[] processList = Process.GetProcessesByName(game);
Process? currProcess = processList.Length > 0 ? processList[0] : null;
if (currProcess != null)
{
Debug.WriteLine(currProcess.ProcessName);
// Get main window handle after process is fully launched
try
{
currentGameInstance = currProcess;
currProcess.EnableRaisingEvents = true;
currProcess.Exited += (sender, e) =>
{
ShowToast("Process Exited", $"{game} with PID {currentGameInstance.Id}");
currentGameInstance = null;
};
ShowToast("Process Started", $"{game} with PID {currentGameInstance.Id}");
break;
}
catch { }
}
}
// Skip to hide/show logic if we already have a process
Check:
if (currentGameInstance == null) continue;
bool gameIsForeground = GetForegroundWindow() == currentGameInstance.MainWindowHandle;
if (!Taskbar.IS_HIDDEN && gameIsForeground) Taskbar.Hide();
if (Taskbar.IS_HIDDEN && !gameIsForeground) Taskbar.Show();
}
}
private static void ReadProcessNames()
{
string[] names = File.ReadAllLines(ProcessNamesFile);
ProcessNamesList = [];
ProcessNamesListAltPos = [];
foreach (string name in names)
{
string temp = name;
if (temp.Contains("****"))
{
temp = temp.Replace("****", string.Empty);
ProcessNamesListAltPos.Add(temp);
}
ProcessNamesList.Add(temp);
}
ShowToast("Processes Editor", "Successfully update process list");
}
private static void ShowToast(string title, string message)
{
ToastContent toast = new ToastContentBuilder()
.AddHeader(Guid.NewGuid().ToString(), title, "")
.SetToastDuration(ToastDuration.Short)
.AddText(message)
.GetToastContent();
ToastNotification toastNotification = new(toast.GetXml());
toastNotification.ExpirationTime = DateTimeOffset.Now.AddSeconds(2);
ToastNotificationManagerCompat.CreateToastNotifier().Show(toastNotification);
}
private static void HandleUnCaughtException(object sender, UnhandledExceptionEventArgs e)
{
System.Windows.MessageBox.Show(e.ExceptionObject.ToString());
}
private static void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
System.Windows.MessageBox.Show(e.Exception.Message + "\n" + e.Exception.StackTrace);
}
private static async void HandleEthernet()
{
MessageBoxResult result = System.Windows.MessageBox.Show("Reset adapter?", "Taskbar hider", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (result != MessageBoxResult.Yes) return;
await Task.Delay(1000 * 30);
string script = $@"
Disable-NetAdapter -Name 'Ethernet' -Confirm:$false
Start-sleep 2
Enable-NetAdapter -Name 'Ethernet' -Confirm:$false
";
Process task = new();
task.StartInfo.FileName = "powershell.exe";
task.StartInfo.Arguments = script;
task.StartInfo.CreateNoWindow = false;
task.Start();
}
private static async void ExitGSkill()
{
await Task.Delay(1000 * 40);
Process[] processes = Process.GetProcessesByName("hid");
for (int i = 0; i < processes.Length; i++)
{
Process p = processes[i];
if (p.MainModule.FileVersionInfo.FileDescription.Contains("Trident Z"))
{
p.Kill();
}
}
}
}
}