From 9c5476a6582234e5e772d8308f0c9f1ef2678ab7 Mon Sep 17 00:00:00 2001 From: Elestriel Date: Wed, 11 Jan 2017 14:27:38 -0500 Subject: [PATCH] - Moved the worker thread sleep out of the taskbar foreach. Taskbar updates will now be faster and smoother. - Removed the thread that would constantly check Windows Accent Colour and now using a hook that catches the event from Windows. (Thanks, qwerty12!) - Can now detect when Explorer is restarted. (Thanks again, qwerty12!) - Replaced the Windows Accent Alpha UpDown with a slider. --- .gitignore | 3 +- TaskbarTool/MainWindow.xaml | 11 +- TaskbarTool/MainWindow.xaml.cs | 135 +++++++++++++++++-------- TaskbarTool/Properties/AssemblyInfo.cs | 4 +- TaskbarTool/TaskbarTool.csproj | 6 ++ TaskbarTool/app.manifest | 76 ++++++++++++++ 6 files changed, 184 insertions(+), 51 deletions(-) create mode 100644 TaskbarTool/app.manifest diff --git a/.gitignore b/.gitignore index 8d185e1..11caddd 100644 --- a/.gitignore +++ b/.gitignore @@ -192,4 +192,5 @@ $RECYCLE.BIN/ ##### # End of core ignore list, below put you custom 'per project' settings (patterns or path) -##### \ No newline at end of file +##### +/TaskbarTool/ProcessWatcher diff --git a/TaskbarTool/MainWindow.xaml b/TaskbarTool/MainWindow.xaml index 32c61c7..74d4ec4 100644 --- a/TaskbarTool/MainWindow.xaml +++ b/TaskbarTool/MainWindow.xaml @@ -71,11 +71,12 @@ IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=UseWindowsAccentColor}" Checked="WindowsAccentColorCheckBox_Changed" Unchecked="WindowsAccentColorCheckBox_Changed"/> - + diff --git a/TaskbarTool/MainWindow.xaml.cs b/TaskbarTool/MainWindow.xaml.cs index a9d80e1..6571a3d 100644 --- a/TaskbarTool/MainWindow.xaml.cs +++ b/TaskbarTool/MainWindow.xaml.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Interop; using System.Windows.Media; namespace TaskbarTool @@ -25,6 +26,9 @@ public partial class MainWindow : Window [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 @@ -78,13 +82,20 @@ public WinCompatTrData(WindowCompositionAttribute attribute, IntPtr data, int si #endregion Structs #region Declarations - static Task WindowsAccentColorTask; - static bool RunAccentTask = false; + // Worker thread static Task ApplyTask; static bool RunApplyTask = false; - static AccentPolicy accentPolicy = new AccentPolicy(); + 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 @@ -162,6 +173,7 @@ private void Window_ContentRendered(object sender, EventArgs e) if (Properties.Settings.Default.StartMinimized) { this.WindowState = WindowState.Minimized; } if (Properties.Settings.Default.StartWhenLaunched) { StartStopButton_Click(null, null); } + //ProcessWatcher.Process.Start(); } #endregion Initializations @@ -170,7 +182,6 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs { SysTrayIcon.Dispose(); SaveSettings(); - RunAccentTask = false; RunApplyTask = false; } @@ -184,33 +195,38 @@ private void CloseMainWindow(object sender, RoutedEventArgs e) private void ApplyToAllTaskbars() { List hWndList = new List(); + + while (RunApplyTask) + { + if (FindTaskbarHandles || ProcessWatcher.Process.NewExplorerStarted) + { + hWndList.Add(FindWindow("Shell_TrayWnd", null)); + IntPtr otherBars = IntPtr.Zero; - hWndList.Add(FindWindow("Shell_TrayWnd", null)); - IntPtr otherBars = IntPtr.Zero; + //IntPtr cortana = FindWindowEx(hWndList[0], IntPtr.Zero, "TrayDummySearchControl", null); + //hWndList.Add(cortana); - //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); } + } - while (true) - { - otherBars = FindWindowEx(IntPtr.Zero, otherBars, "Shell_SecondaryTrayWnd", ""); - if (otherBars == IntPtr.Zero) { break; } - else { hWndList.Add(otherBars); } - } + FindTaskbarHandles = false; + ProcessWatcher.Process.NewExplorerStarted = false; + } - while (RunApplyTask) - { foreach (IntPtr hWnd in hWndList) { - SetWindowBlur(hWnd); - Thread.Sleep(10); + SetTaskbarStyle(hWnd); } + Thread.Sleep(10); } } - private void SetWindowBlur(IntPtr hWnd) + private void SetTaskbarStyle(IntPtr hWnd) { - int sizeOfPolicy = Marshal.SizeOf(accentPolicy); IntPtr policyPtr = Marshal.AllocHGlobal(sizeOfPolicy); Marshal.StructureToPtr(accentPolicy, policyPtr, false); @@ -222,12 +238,34 @@ private void SetWindowBlur(IntPtr hWnd) Marshal.FreeHGlobal(policyPtr); } - private void GetWindowsAccentColorLoop() + protected override void OnSourceInitialized(EventArgs e) { - while (RunAccentTask) { - accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt(); - Thread.Sleep(900); + 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 @@ -244,6 +282,8 @@ private void StartStopButton_Click(object sender, RoutedEventArgs e) { StartStopButton.Content = "Stop"; RunApplyTask = true; + if (WindowsAccentColorCheckBox.IsChecked == true) { accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt(); } + ApplyTask = new Task(() => ApplyToAllTaskbars()); ApplyTask.Start(); } @@ -281,18 +321,39 @@ private void WindowsAccentColorCheckBox_Changed(object sender, RoutedEventArgs e { if (WindowsAccentColorCheckBox.IsChecked == true) { + accentPolicy.GradientColor = WindowsAccentColor.GetColorAsInt(); GradientColorPicker.IsEnabled = false; - RunAccentTask = true; - WindowsAccentColorTask = new Task(() => GetWindowsAccentColorLoop()); - WindowsAccentColorTask.Start(); } else { - RunAccentTask = false; + 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 e) + { + if (!alphaDragStarted && Properties.Settings.Default.UseWindowsAccentColor) + { + ApplyAlphaToWindowsAccent((byte)WindowsAccentAlphaSlider.Value); + } + } + #endregion Control Handles } @@ -300,23 +361,12 @@ private void WindowsAccentColorCheckBox_Changed(object sender, RoutedEventArgs e public static class WindowsAccentColor { private static Color accentColor = Color.FromArgb(255, 0, 0, 0); - private static DateTime lastUpdateTime; - private static TimeSpan timeSinceLastUpdate; - - public static Color GetColor() - { - timeSinceLastUpdate = DateTime.Now - lastUpdateTime; - if (timeSinceLastUpdate.TotalSeconds > 1) - { UpdateColor(); } - - return accentColor; - } public static int GetColorAsInt() { - Color color = GetColor(); + UpdateColor(); - return BitConverter.ToInt32(new byte[] { color.R, color.G, color.B, Properties.Settings.Default.WindowsAccentAlpha }, 0); + return BitConverter.ToInt32(new byte[] { accentColor.R, accentColor.G, accentColor.B, Properties.Settings.Default.WindowsAccentAlpha }, 0); } private static void UpdateColor() @@ -325,8 +375,7 @@ private static void UpdateColor() int keyColor = (int)Microsoft.Win32.Registry.GetValue(keyName, "StartColorMenu", 00000000); byte[] bytes = BitConverter.GetBytes(keyColor); - - lastUpdateTime = DateTime.Now; + accentColor = Color.FromArgb(bytes[3], bytes[0], bytes[1], bytes[2]); } } diff --git a/TaskbarTool/Properties/AssemblyInfo.cs b/TaskbarTool/Properties/AssemblyInfo.cs index 24ae3a5..da812cc 100644 --- a/TaskbarTool/Properties/AssemblyInfo.cs +++ b/TaskbarTool/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.5")] -[assembly: AssemblyFileVersion("1.0.5")] +[assembly: AssemblyVersion("1.0.6")] +[assembly: AssemblyFileVersion("1.0.6")] diff --git a/TaskbarTool/TaskbarTool.csproj b/TaskbarTool/TaskbarTool.csproj index 24c8d59..d0bafbc 100644 --- a/TaskbarTool/TaskbarTool.csproj +++ b/TaskbarTool/TaskbarTool.csproj @@ -41,10 +41,14 @@ Resources\Mushroom1UP.ico + + app.manifest + + @@ -82,6 +86,7 @@ + Code @@ -99,6 +104,7 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator diff --git a/TaskbarTool/app.manifest b/TaskbarTool/app.manifest new file mode 100644 index 0000000..a6b46bb --- /dev/null +++ b/TaskbarTool/app.manifest @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +