/
MainWindow.xaml.cs
383 lines (321 loc) · 13.6 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
namespace TaskbarTool
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Invokes
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WinCompatTrData data);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern uint RegisterWindowMessage(string msgString);
#endregion Invokes
#region Enums
internal enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_INVALID_STATE = 4
}
internal enum WindowCompositionAttribute
{
WCA_ACCENT_POLICY = 19
}
#endregion Enums
#region Structs
[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
public AccentPolicy(AccentState accentState, int accentFlags, int gradientColor, int animationId)
{
AccentState = accentState;
AccentFlags = accentFlags;
GradientColor = gradientColor;
AnimationId = animationId;
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct WinCompatTrData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
public WinCompatTrData(WindowCompositionAttribute attribute, IntPtr data, int sizeOfData)
{
Attribute = attribute;
Data = data;
SizeOfData = sizeOfData;
}
}
#endregion Structs
#region Declarations
// Worker thread
static Task ApplyTask;
static bool RunApplyTask = false;
public static bool FindTaskbarHandles = true;
static System.Windows.Forms.NotifyIcon SysTrayIcon;
ContextMenu SysTrayContextMenu;
private static bool alphaDragStarted = false;
static AccentPolicy accentPolicy = new AccentPolicy();
private static readonly uint WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");
private const uint WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320;
#endregion Declarations
#region Initializations
public MainWindow()
{
InitializeComponent();
SysTrayContextMenu = this.FindResource("TrayContextMenu") as ContextMenu;
SysTrayIcon = new System.Windows.Forms.NotifyIcon();
Stream iconStream = Application.GetResourceStream(new Uri("Resources/Mushroom1UP.ico", UriKind.Relative)).Stream;
SysTrayIcon.Icon = new System.Drawing.Icon(iconStream);
SysTrayIcon.Visible = true;
SysTrayIcon.MouseClick += SysTrayIcon_MouseClick;
SysTrayIcon.DoubleClick +=
delegate (object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
private void PopulateComboBoxes()
{
AccentStateComboBox.ItemsSource = Enum.GetValues(typeof(AccentState)).Cast<AccentState>();
AccentStateComboBox.SelectedIndex = 0;
}
private void LoadSettings()
{
try
{
AccentStateComboBox.SelectedItem = (AccentState)Properties.Settings.Default.AccentState;
GradientColorPicker.SelectedColor = (Color)ColorConverter.ConvertFromString(Properties.Settings.Default.GradientColor);
ColorizeBlurCheckBox.IsChecked = Properties.Settings.Default.Colorize;
}
catch (Exception ex)
{
AccentStateComboBox.SelectedIndex = 0;
GradientColorPicker.SelectedColor = Color.FromArgb(127, 64, 127, 255);
ColorizeBlurCheckBox.IsChecked = true;
MessageBox.Show(ex.Message, "Error loading settings.");
}
}
private void SaveSettings()
{
try
{
Properties.Settings.Default.AccentState = (int)AccentStateComboBox.SelectedItem;
Properties.Settings.Default.GradientColor = GradientColorPicker.SelectedColor.ToString();
Properties.Settings.Default.Colorize = ColorizeBlurCheckBox.IsChecked ?? false;
Properties.Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error saving settings.");
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
PopulateComboBoxes();
LoadSettings();
if (Properties.Settings.Default.StartMinimized) { this.WindowState = WindowState.Minimized; }
if (Properties.Settings.Default.StartWhenLaunched) { StartStopButton_Click(null, null); }
//ProcessWatcher.Process.Start();
}
#endregion Initializations
#region Destructors
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SysTrayIcon.Dispose();
SaveSettings();
RunApplyTask = false;
}
private void CloseMainWindow(object sender, RoutedEventArgs e)
{
this.Close();
}
#endregion Destructors
#region Functions
private void ApplyToAllTaskbars()
{
List<IntPtr> hWndList = new List<IntPtr>();
while (RunApplyTask)
{
if (FindTaskbarHandles || ProcessWatcher.Process.NewExplorerStarted)
{
hWndList.Add(FindWindow("Shell_TrayWnd", null));
IntPtr otherBars = IntPtr.Zero;
//IntPtr cortana = FindWindowEx(hWndList[0], IntPtr.Zero, "TrayDummySearchControl", null);
//hWndList.Add(cortana);
while (true)
{
otherBars = FindWindowEx(IntPtr.Zero, otherBars, "Shell_SecondaryTrayWnd", "");
if (otherBars == IntPtr.Zero) { break; }
else { hWndList.Add(otherBars); }
}
FindTaskbarHandles = false;
ProcessWatcher.Process.NewExplorerStarted = false;
}
foreach (IntPtr hWnd in hWndList)
{
SetTaskbarStyle(hWnd);
}
Thread.Sleep(10);
}
}
private void SetTaskbarStyle(IntPtr hWnd)
{
int sizeOfPolicy = Marshal.SizeOf(accentPolicy);
IntPtr policyPtr = Marshal.AllocHGlobal(sizeOfPolicy);
Marshal.StructureToPtr(accentPolicy, policyPtr, false);
WinCompatTrData data = new WinCompatTrData(WindowCompositionAttribute.WCA_ACCENT_POLICY, policyPtr, sizeOfPolicy);
SetWindowCompositionAttribute(hWnd, ref data);
Marshal.FreeHGlobal(policyPtr);
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_TASKBARCREATED)
{
FindTaskbarHandles = true;
handled = true;
} else if (msg == WM_DWMCOLORIZATIONCOLORCHANGED) {
accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt(); // TODO: use colour from wParam
handled = true;
}
return IntPtr.Zero;
}
private void ApplyAlphaToWindowsAccent(byte alpha)
{
byte[] bytes = BitConverter.GetBytes(accentPolicy.GradientColor);
int colorInt = BitConverter.ToInt32(new byte[] { bytes[0], bytes[1], bytes[2], alpha }, 0);
accentPolicy.GradientColor = colorInt;
}
#endregion Functions
#region Control Handles
private void StartStopButton_Click(object sender, RoutedEventArgs e)
{
if (RunApplyTask)
{
StartStopButton.Content = "Start";
RunApplyTask = false;
}
else
{
StartStopButton.Content = "Stop";
RunApplyTask = true;
if (WindowsAccentColorCheckBox.IsChecked == true) { accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt(); }
ApplyTask = new Task(() => ApplyToAllTaskbars());
ApplyTask.Start();
}
}
private void AccentStateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
accentPolicy.AccentState = (AccentState)AccentStateComboBox.SelectedItem;
}
private void GradientColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
{
Color gradientColor = GradientColorPicker.SelectedColor ?? Color.FromArgb(255, 255, 255, 255);
accentPolicy.GradientColor = BitConverter.ToInt32(new byte[] { gradientColor.R, gradientColor.G, gradientColor.B, gradientColor.A }, 0);
}
private void ColorizeBlurCheckBox_Changed(object sender, RoutedEventArgs e)
{
if (ColorizeBlurCheckBox.IsChecked == true) { accentPolicy.AccentFlags = 2; }
else { accentPolicy.AccentFlags = 0; }
}
private void SysTrayIcon_MouseClick(object sender, EventArgs e)
{
System.Windows.Forms.MouseEventArgs me = (System.Windows.Forms.MouseEventArgs)e;
if (me.Button == System.Windows.Forms.MouseButtons.Right)
{
SysTrayContextMenu.PlacementTarget = sender as Button;
SysTrayContextMenu.IsOpen = true;
this.Activate();
}
}
private void WindowsAccentColorCheckBox_Changed(object sender, RoutedEventArgs e)
{
if (WindowsAccentColorCheckBox.IsChecked == true)
{
accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt();
GradientColorPicker.IsEnabled = false;
}
else
{
Color gradientColor = GradientColorPicker.SelectedColor ?? Color.FromArgb(255, 255, 255, 255);
accentPolicy.GradientColor = BitConverter.ToInt32(new byte[] { gradientColor.R, gradientColor.G, gradientColor.B, gradientColor.A }, 0);
GradientColorPicker.IsEnabled = true;
}
}
private void WindowsAccentAlphaSlider_DragCompleted(object sender, RoutedEventArgs e)
{
alphaDragStarted = false;
if (Properties.Settings.Default.UseWindowsAccentColor)
{
ApplyAlphaToWindowsAccent((byte)WindowsAccentAlphaSlider.Value);
}
}
private void WindowsAccentAlphaSlider_DragStarted(object sender, RoutedEventArgs e)
{
alphaDragStarted = true;
}
private void WindowsAccentAlphaSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!alphaDragStarted && Properties.Settings.Default.UseWindowsAccentColor)
{
ApplyAlphaToWindowsAccent((byte)WindowsAccentAlphaSlider.Value);
}
}
#endregion Control Handles
}
#region Helper Classes
public static class WindowsAccentColor
{
private static Color accentColor = Color.FromArgb(255, 0, 0, 0);
public static int GetColorAsInt()
{
UpdateColor();
return BitConverter.ToInt32(new byte[] { accentColor.R, accentColor.G, accentColor.B, Properties.Settings.Default.WindowsAccentAlpha }, 0);
}
private static void UpdateColor()
{
string keyName = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent";
int keyColor = (int)Microsoft.Win32.Registry.GetValue(keyName, "StartColorMenu", 00000000);
byte[] bytes = BitConverter.GetBytes(keyColor);
accentColor = Color.FromArgb(bytes[3], bytes[0], bytes[1], bytes[2]);
}
}
#endregion Helper Classes
}