From 14618ee19e91890cf7b3527968eb93033d1c9549 Mon Sep 17 00:00:00 2001 From: seerge Date: Mon, 20 Mar 2023 19:04:41 +0100 Subject: [PATCH] Possible fix for theme switching --- app/ControlHelper.cs | 58 ++++++++++++++++++++++++++++------------ app/CustomControls.cs | 32 +++++++++++++++------- app/Fans.Designer.cs | 27 ++++++++++--------- app/Program.cs | 11 ++++---- app/Settings.Designer.cs | 5 ++++ 5 files changed, 89 insertions(+), 44 deletions(-) diff --git a/app/ControlHelper.cs b/app/ControlHelper.cs index aa5b04c5..c264df17 100644 --- a/app/ControlHelper.cs +++ b/app/ControlHelper.cs @@ -6,37 +6,33 @@ public static class ControlHelper { static bool _invert = false; + static bool _resize = false; + static float _scale = 1; static Color formBack; static Color backMain; - static Color backSecond; static Color foreMain; static Color foreAccent; static Color borderMain; static Color buttonMain; - public static void Adjust(RForm container, float baseScale = 2, bool invert = false) + public static void Adjust(RForm container, bool darkTheme = false, bool invert = false) { - _scale = GetDpiScale(container).Value / baseScale; - if (container.darkTheme) + if (darkTheme) { formBack = Color.FromArgb(255, 35, 35, 35); backMain = Color.FromArgb(255, 50, 50, 50); - backSecond = Color.FromArgb(255, 125, 125, 125); - foreMain = Color.White; - foreAccent = Color.FromArgb(255,100, 100, 100); + foreAccent = Color.FromArgb(255, 100, 100, 100); borderMain = Color.FromArgb(255, 50, 50, 50); - buttonMain = Color.FromArgb(255, 100, 100, 100); + buttonMain = Color.FromArgb(255, 80, 80, 80); } else { formBack = SystemColors.Control; backMain = SystemColors.ControlLightLight; - backSecond = SystemColors.ButtonFace; - foreMain = SystemColors.ControlText; foreAccent = Color.LightGray; borderMain = Color.LightGray; @@ -51,6 +47,29 @@ public static class ControlHelper _invert = false; } + public static void Resize(RForm container, float baseScale = 2) + { + _scale = GetDpiScale(container).Value / baseScale; + ResizeControls(container.Controls); + + } + + private static void ResizeControls(Control.ControlCollection controls) + { + foreach (Control control in controls) + { + var button = control as RButton; + if (button != null && button.Image is not null) + button.Image = ResizeImage(button.Image); + + var pictureBox = control as PictureBox; + if (pictureBox != null && pictureBox.BackgroundImage is not null) + pictureBox.BackgroundImage = ResizeImage(pictureBox.BackgroundImage); + + ResizeControls(control.Controls); + } + } + private static void AdjustControls(Control.ControlCollection controls) { @@ -59,7 +78,7 @@ public static class ControlHelper var button = control as RButton; if (button != null) { - button.BackColor = backMain; + button.BackColor = button.Secondary ? buttonMain : backMain; button.ForeColor = foreMain; button.FlatStyle = FlatStyle.Flat; @@ -70,11 +89,9 @@ public static class ControlHelper } var pictureBox = control as PictureBox; - if (pictureBox != null) - { - if (pictureBox.BackgroundImage is not null) - pictureBox.BackgroundImage = AdjustImage(pictureBox.BackgroundImage); - } + if (pictureBox != null && pictureBox.BackgroundImage is not null) + pictureBox.BackgroundImage = AdjustImage(pictureBox.BackgroundImage); + var combo = control as RComboBox; if (combo != null) @@ -129,7 +146,7 @@ public static class ControlHelper }); } - private static Image AdjustImage(Image image) + private static Image ResizeImage(Image image) { var newSize = new Size((int)(image.Width * _scale), (int)(image.Height * _scale)); var pic = new Bitmap(newSize.Width, newSize.Height); @@ -139,6 +156,12 @@ public static class ControlHelper g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(image, new Rectangle(new Point(), newSize)); } + return pic; + } + + private static Image AdjustImage(Image image) + { + var pic = new Bitmap(image); if (_invert) { @@ -153,6 +176,7 @@ public static class ControlHelper } return pic; + } } diff --git a/app/CustomControls.cs b/app/CustomControls.cs index 2ac36de3..a0b07b85 100644 --- a/app/CustomControls.cs +++ b/app/CustomControls.cs @@ -18,25 +18,29 @@ namespace CustomControls [DllImport("DwmApi")] //System.Runtime.InteropServices private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize); - public bool darkTheme; - public bool invert = false; + public bool darkTheme = false; - public void InitTheme() + public void InitTheme(bool setDPI = true) { bool newDarkTheme = CheckSystemDarkModeStatus(); - invert = (darkTheme != newDarkTheme); + bool changed = (darkTheme != newDarkTheme); darkTheme = newDarkTheme; - ControlHelper.Adjust(this, 2, invert); - try - { - DwmSetWindowAttribute(this.Handle, 20, new[] { darkTheme ? 1 : 0 }, 4); - } catch { } + if (setDPI) + ControlHelper.Resize(this); + + DwmSetWindowAttribute(this.Handle, 20, new[] { darkTheme ? 1 : 0 }, 4); + ControlHelper.Adjust(this, darkTheme, changed); } } + public class RTrackBar : TrackBar + { + + } + public class RComboBox : ComboBox { private Color borderColor = Color.Gray; @@ -224,10 +228,10 @@ namespace CustomControls private int borderRadius = 5; private bool activated = false; + private bool secondary = false; private Color borderColor = Color.Transparent; - public Color BorderColor { get { return borderColor; } @@ -250,6 +254,14 @@ namespace CustomControls } } + public bool Secondary + { + get { return secondary; } + set + { + secondary = value; + } + } public RButton() { diff --git a/app/Fans.Designer.cs b/app/Fans.Designer.cs index 727a5884..b7c2ae82 100644 --- a/app/Fans.Designer.cs +++ b/app/Fans.Designer.cs @@ -31,10 +31,10 @@ namespace GHelper /// private void InitializeComponent() { - ChartArea chartArea3 = new ChartArea(); - Title title3 = new Title(); - ChartArea chartArea4 = new ChartArea(); - Title title4 = new Title(); + ChartArea chartArea1 = new ChartArea(); + Title title1 = new Title(); + ChartArea chartArea2 = new ChartArea(); + Title title2 = new Title(); panelFans = new Panel(); labelTip = new Label(); labelBoost = new Label(); @@ -160,8 +160,8 @@ namespace GHelper // // chartGPU // - chartArea3.Name = "ChartArea1"; - chartGPU.ChartAreas.Add(chartArea3); + chartArea1.Name = "ChartArea1"; + chartGPU.ChartAreas.Add(chartArea1); chartGPU.Dock = DockStyle.Fill; chartGPU.Location = new Point(2, 506); chartGPU.Margin = new Padding(2, 10, 2, 10); @@ -169,13 +169,13 @@ namespace GHelper chartGPU.Size = new Size(760, 476); chartGPU.TabIndex = 17; chartGPU.Text = "chart1"; - title3.Name = "Title1"; - chartGPU.Titles.Add(title3); + title1.Name = "Title1"; + chartGPU.Titles.Add(title1); // // chartCPU // - chartArea4.Name = "ChartArea1"; - chartCPU.ChartAreas.Add(chartArea4); + chartArea2.Name = "ChartArea1"; + chartCPU.ChartAreas.Add(chartArea2); chartCPU.Dock = DockStyle.Fill; chartCPU.Location = new Point(2, 10); chartCPU.Margin = new Padding(2, 10, 2, 10); @@ -183,8 +183,8 @@ namespace GHelper chartCPU.Size = new Size(760, 476); chartCPU.TabIndex = 14; chartCPU.Text = "chartCPU"; - title4.Name = "Title1"; - chartCPU.Titles.Add(title4); + title2.Name = "Title1"; + chartCPU.Titles.Add(title2); // // labelFans // @@ -219,6 +219,7 @@ namespace GHelper buttonReset.Location = new Point(30, 1081); buttonReset.Margin = new Padding(4, 2, 4, 2); buttonReset.Name = "buttonReset"; + buttonReset.Secondary = true; buttonReset.Size = new Size(232, 44); buttonReset.TabIndex = 15; buttonReset.Text = "Factory Defaults"; @@ -234,6 +235,7 @@ namespace GHelper buttonApply.Location = new Point(542, 1081); buttonApply.Margin = new Padding(4, 2, 4, 2); buttonApply.Name = "buttonApply"; + buttonApply.Secondary = true; buttonApply.Size = new Size(248, 44); buttonApply.TabIndex = 14; buttonApply.Text = "Apply Fan Curve"; @@ -303,6 +305,7 @@ namespace GHelper buttonApplyPower.Location = new Point(20, 1081); buttonApplyPower.Margin = new Padding(4, 2, 4, 2); buttonApplyPower.Name = "buttonApplyPower"; + buttonApplyPower.Secondary = true; buttonApplyPower.Size = new Size(324, 44); buttonApplyPower.TabIndex = 24; buttonApplyPower.Text = "Apply Power Limits"; diff --git a/app/Program.cs b/app/Program.cs index 29e19ef9..5d7d2436 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -49,7 +49,8 @@ namespace GHelper } - SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged; + SystemEvents.UserPreferenceChanged += new + UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged); Application.EnableVisualStyles(); @@ -81,7 +82,7 @@ namespace GHelper } - static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) + static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) { if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTheme) < 2000) return; @@ -91,13 +92,13 @@ namespace GHelper { case UserPreferenceCategory.General: Debug.WriteLine("Theme Changed"); - settingsForm.InitTheme(); + settingsForm.InitTheme(false); if (settingsForm.fans is not null && settingsForm.fans.Text != "") - settingsForm.fans.InitTheme(); + settingsForm.fans.InitTheme(false); if (settingsForm.keyb is not null && settingsForm.keyb.Text != "") - settingsForm.keyb.InitTheme(); + settingsForm.keyb.InitTheme(false); break; } diff --git a/app/Settings.Designer.cs b/app/Settings.Designer.cs index 192cee8e..4efec733 100644 --- a/app/Settings.Designer.cs +++ b/app/Settings.Designer.cs @@ -190,6 +190,7 @@ namespace GHelper buttonMatrix.TabIndex = 43; buttonMatrix.Text = "Picture / Gif"; buttonMatrix.UseVisualStyleBackColor = false; + buttonMatrix.Secondary = true; // // comboMatrixRunning // @@ -334,6 +335,7 @@ namespace GHelper buttonQuit.TabIndex = 18; buttonQuit.Text = "Quit"; buttonQuit.UseVisualStyleBackColor = false; + buttonQuit.Secondary = true; // // checkStartup // @@ -498,6 +500,7 @@ namespace GHelper buttonFans.Text = "Fans + Power"; buttonFans.TextImageRelation = TextImageRelation.ImageAboveText; buttonFans.UseVisualStyleBackColor = false; + buttonFans.Secondary = true; // // panelGPU // @@ -833,6 +836,8 @@ namespace GHelper buttonKeyboard.TabIndex = 37; buttonKeyboard.Text = "Extra"; buttonKeyboard.UseVisualStyleBackColor = false; + buttonKeyboard.Secondary = true; + // // comboKeyboard //