From 1cd808de077133d01caf17d2f398bda6379d5b30 Mon Sep 17 00:00:00 2001 From: seerge Date: Mon, 20 Mar 2023 16:42:51 +0100 Subject: [PATCH] Auto theme change --- app/ControlHelper.cs | 66 ++++++++++++++++++++++++++-------------- app/CustomControls.cs | 32 ++++++++++++++----- app/Fans.cs | 3 +- app/Keyboard.Designer.cs | 16 +++++----- app/Keyboard.cs | 7 +++-- app/Program.cs | 27 ++++++++++++++++ app/Settings.cs | 7 ++--- 7 files changed, 112 insertions(+), 46 deletions(-) diff --git a/app/ControlHelper.cs b/app/ControlHelper.cs index 31f4427a..3de99ba6 100644 --- a/app/ControlHelper.cs +++ b/app/ControlHelper.cs @@ -1,4 +1,5 @@ -using System.Drawing.Drawing2D; +using System; +using System.Drawing.Drawing2D; using System.Windows.Forms.DataVisualization.Charting; using CustomControls; @@ -6,27 +7,43 @@ public static class ControlHelper { static bool _invert = false; + static bool _darkTheme = false; + static float _scale = 1; - static Color formBack = Color.FromArgb(255, 35, 35, 35); - static Color backMain = Color.FromArgb(255, 50, 50, 50); - static Color foreMain = Color.White; - static Color borderMain = Color.FromArgb(255, 50, 50, 50); - static Color buttonMain = Color.FromArgb(255, 100,100,100); + static Color formBack; + static Color backMain; + static Color foreMain; + static Color borderMain; + static Color buttonMain; - public static void Adjust(Control container, float baseScale = 2, bool invert = false) + public static void Adjust(RForm container, float baseScale = 2) { _scale = GetDpiScale(container).Value / baseScale; - _invert = invert; + - if (_invert) + if (container.DarkTheme) { - container.BackColor = formBack; - container.ForeColor = foreMain; + formBack = Color.FromArgb(255, 35, 35, 35); + backMain = Color.FromArgb(255, 50, 50, 50); + foreMain = Color.White; + borderMain = Color.FromArgb(255, 50, 50, 50); + buttonMain = Color.FromArgb(255, 100, 100, 100); + } else + { + formBack = SystemColors.Control; + backMain = SystemColors.ControlLightLight; + foreMain = SystemColors.ControlText; + borderMain = Color.LightGray; + buttonMain = SystemColors.ControlLight; } + container.BackColor = formBack; + container.ForeColor = foreMain; + _invert = container.invert; AdjustControls(container.Controls); + _invert = false; } @@ -37,15 +54,11 @@ public static class ControlHelper var button = control as Button; if (button != null) { + button.BackColor = backMain; + button.ForeColor = foreMain; - if (_invert) - { - button.BackColor = backMain; - button.ForeColor = foreMain; - - button.FlatStyle = FlatStyle.Flat; - button.FlatAppearance.BorderColor = borderMain; - } + button.FlatStyle = FlatStyle.Flat; + button.FlatAppearance.BorderColor = borderMain; if (button.Image is not null) button.Image = AdjustImage(button.Image); @@ -59,16 +72,23 @@ public static class ControlHelper } var combo = control as RComboBox; - if (combo != null && _invert) + if (combo != null) { - combo.BackColor = borderMain; + combo.BackColor = backMain; combo.ForeColor = foreMain; - combo.BorderColor = borderMain; + combo.BorderColor = backMain; combo.ButtonColor = buttonMain; } + + var gb = control as GroupBox; + if (gb != null) + { + gb.ForeColor = foreMain; + } + var chart = control as Chart; - if (chart != null && _invert) + if (chart != null) { chart.BackColor = backMain; chart.ChartAreas[0].BackColor = backMain; diff --git a/app/CustomControls.cs b/app/CustomControls.cs index 62ed72ac..13724fa8 100644 --- a/app/CustomControls.cs +++ b/app/CustomControls.cs @@ -1,11 +1,10 @@ -using GHelper; -using System.ComponentModel; +using System.ComponentModel; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; namespace CustomControls { - + public class RForm : Form { @@ -16,15 +15,31 @@ namespace CustomControls [DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")] public static extern bool CheckSystemDarkModeStatus(); - public static bool darkTheme = CheckSystemDarkModeStatus(); [DllImport("DwmApi")] //System.Runtime.InteropServices private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize); - protected override void OnHandleCreated(EventArgs e) + protected bool _darkTheme; + public bool invert = false; + + public bool DarkTheme { - if (darkTheme && DwmSetWindowAttribute(Handle, 19, new[] { 1 }, 4) != 0) - DwmSetWindowAttribute(Handle, 20, new[] { 1 }, 4); + get { return _darkTheme; } + set + { + if (_darkTheme != value) + { + _darkTheme = value; + invert = true; + } + } + } + + public void InitTheme() + { + DarkTheme = CheckSystemDarkModeStatus(); + ControlHelper.Adjust(this, 2); + DwmSetWindowAttribute(this.Handle, 20, new[] { DarkTheme ? 1 : 0 }, 4); } } @@ -290,7 +305,8 @@ namespace CustomControls if (!Enabled && ForeColor != SystemColors.ControlText) { var rect = pevent.ClipRectangle; - if (Image is not null) { + if (Image is not null) + { rect.Y += Image.Height; rect.Height -= Image.Height; } diff --git a/app/Fans.cs b/app/Fans.cs index 6d380539..5db3f0e8 100644 --- a/app/Fans.cs +++ b/app/Fans.cs @@ -82,8 +82,7 @@ namespace GHelper { InitializeComponent(); - - ControlHelper.Adjust(this, 2, darkTheme); + InitTheme(); labelTip.Visible = false; labelTip.BackColor = Color.Transparent; diff --git a/app/Keyboard.Designer.cs b/app/Keyboard.Designer.cs index f6c96e53..3babfc8b 100644 --- a/app/Keyboard.Designer.cs +++ b/app/Keyboard.Designer.cs @@ -1,4 +1,6 @@ -namespace GHelper +using CustomControls; + +namespace GHelper { partial class Keyboard { @@ -31,12 +33,12 @@ groupBox1 = new GroupBox(); textM4 = new TextBox(); textM3 = new TextBox(); - comboM4 = new ComboBox(); + comboM4 = new RComboBox(); labelM4 = new Label(); - comboM3 = new ComboBox(); + comboM3 = new RComboBox(); labelM3 = new Label(); textFNF4 = new TextBox(); - comboFNF4 = new ComboBox(); + comboFNF4 = new RComboBox(); labelFNF4 = new Label(); groupBox1.SuspendLayout(); SuspendLayout(); @@ -161,13 +163,13 @@ private GroupBox groupBox1; private Label labelM3; - private ComboBox comboM3; - private ComboBox comboM4; + private RComboBox comboM3; + private RComboBox comboM4; private Label labelM4; private TextBox textM4; private TextBox textM3; private TextBox textFNF4; - private ComboBox comboFNF4; + private RComboBox comboFNF4; private Label labelFNF4; } } \ No newline at end of file diff --git a/app/Keyboard.cs b/app/Keyboard.cs index 02ee24b9..38aeff90 100644 --- a/app/Keyboard.cs +++ b/app/Keyboard.cs @@ -1,6 +1,8 @@ -namespace GHelper +using CustomControls; + +namespace GHelper { - public partial class Keyboard : Form + public partial class Keyboard : RForm { Dictionary customActions = new Dictionary @@ -51,6 +53,7 @@ public Keyboard() { InitializeComponent(); + InitTheme(); SetKeyCombo(comboM3, textM3, "m3"); SetKeyCombo(comboM4, textM4, "m4"); diff --git a/app/Program.cs b/app/Program.cs index 9956db8a..29e19ef9 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -27,6 +27,7 @@ namespace GHelper private static IntPtr ds; private static long lastAuto; + private static long lastTheme; // The main entry point for the application public static void Main() @@ -48,6 +49,8 @@ namespace GHelper } + SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged; + Application.EnableVisualStyles(); ds = settingsForm.Handle; @@ -77,6 +80,30 @@ namespace GHelper } + + static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) + { + + if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTheme) < 2000) return; + lastTheme = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + + switch (e.Category) + { + case UserPreferenceCategory.General: + Debug.WriteLine("Theme Changed"); + settingsForm.InitTheme(); + + if (settingsForm.fans is not null && settingsForm.fans.Text != "") + settingsForm.fans.InitTheme(); + + if (settingsForm.keyb is not null && settingsForm.keyb.Text != "") + settingsForm.keyb.InitTheme(); + + break; + } + } + + static async void CheckForUpdates() { diff --git a/app/Settings.cs b/app/Settings.cs index f7c8f32e..d4a9ce24 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -17,16 +17,15 @@ namespace GHelper public string perfName = "Balanced"; - Fans fans; - Keyboard keyb; + public Fans fans; + public Keyboard keyb; static AnimeMatrixDevice mat; public SettingsForm() { InitializeComponent(); - - ControlHelper.Adjust(this, 2, darkTheme); + InitTheme(); FormClosing += SettingsForm_FormClosing;