Possible fix for theme switching

This commit is contained in:
seerge
2023-03-20 19:04:41 +01:00
parent 0b3a75e373
commit 14618ee19e
5 changed files with 89 additions and 44 deletions

View File

@@ -6,37 +6,33 @@ public static class ControlHelper
{ {
static bool _invert = false; static bool _invert = false;
static bool _resize = false;
static float _scale = 1; static float _scale = 1;
static Color formBack; static Color formBack;
static Color backMain; static Color backMain;
static Color backSecond;
static Color foreMain; static Color foreMain;
static Color foreAccent; static Color foreAccent;
static Color borderMain; static Color borderMain;
static Color buttonMain; 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); formBack = Color.FromArgb(255, 35, 35, 35);
backMain = Color.FromArgb(255, 50, 50, 50); backMain = Color.FromArgb(255, 50, 50, 50);
backSecond = Color.FromArgb(255, 125, 125, 125);
foreMain = Color.White; foreMain = Color.White;
foreAccent = Color.FromArgb(255,100, 100, 100); foreAccent = Color.FromArgb(255, 100, 100, 100);
borderMain = Color.FromArgb(255, 50, 50, 50); borderMain = Color.FromArgb(255, 50, 50, 50);
buttonMain = Color.FromArgb(255, 100, 100, 100); buttonMain = Color.FromArgb(255, 80, 80, 80);
} }
else else
{ {
formBack = SystemColors.Control; formBack = SystemColors.Control;
backMain = SystemColors.ControlLightLight; backMain = SystemColors.ControlLightLight;
backSecond = SystemColors.ButtonFace;
foreMain = SystemColors.ControlText; foreMain = SystemColors.ControlText;
foreAccent = Color.LightGray; foreAccent = Color.LightGray;
borderMain = Color.LightGray; borderMain = Color.LightGray;
@@ -51,6 +47,29 @@ public static class ControlHelper
_invert = false; _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) private static void AdjustControls(Control.ControlCollection controls)
{ {
@@ -59,7 +78,7 @@ public static class ControlHelper
var button = control as RButton; var button = control as RButton;
if (button != null) if (button != null)
{ {
button.BackColor = backMain; button.BackColor = button.Secondary ? buttonMain : backMain;
button.ForeColor = foreMain; button.ForeColor = foreMain;
button.FlatStyle = FlatStyle.Flat; button.FlatStyle = FlatStyle.Flat;
@@ -70,11 +89,9 @@ public static class ControlHelper
} }
var pictureBox = control as PictureBox; var pictureBox = control as PictureBox;
if (pictureBox != null) if (pictureBox != null && pictureBox.BackgroundImage is not null)
{ pictureBox.BackgroundImage = AdjustImage(pictureBox.BackgroundImage);
if (pictureBox.BackgroundImage is not null)
pictureBox.BackgroundImage = AdjustImage(pictureBox.BackgroundImage);
}
var combo = control as RComboBox; var combo = control as RComboBox;
if (combo != null) 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 newSize = new Size((int)(image.Width * _scale), (int)(image.Height * _scale));
var pic = new Bitmap(newSize.Width, newSize.Height); var pic = new Bitmap(newSize.Width, newSize.Height);
@@ -139,6 +156,12 @@ public static class ControlHelper
g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(new Point(), newSize)); g.DrawImage(image, new Rectangle(new Point(), newSize));
} }
return pic;
}
private static Image AdjustImage(Image image)
{
var pic = new Bitmap(image);
if (_invert) if (_invert)
{ {
@@ -153,6 +176,7 @@ public static class ControlHelper
} }
return pic; return pic;
} }
} }

View File

@@ -18,25 +18,29 @@ namespace CustomControls
[DllImport("DwmApi")] //System.Runtime.InteropServices [DllImport("DwmApi")] //System.Runtime.InteropServices
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize); private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
public bool darkTheme; public bool darkTheme = false;
public bool invert = false;
public void InitTheme() public void InitTheme(bool setDPI = true)
{ {
bool newDarkTheme = CheckSystemDarkModeStatus(); bool newDarkTheme = CheckSystemDarkModeStatus();
invert = (darkTheme != newDarkTheme); bool changed = (darkTheme != newDarkTheme);
darkTheme = newDarkTheme; darkTheme = newDarkTheme;
ControlHelper.Adjust(this, 2, invert); if (setDPI)
try ControlHelper.Resize(this);
{
DwmSetWindowAttribute(this.Handle, 20, new[] { darkTheme ? 1 : 0 }, 4); DwmSetWindowAttribute(this.Handle, 20, new[] { darkTheme ? 1 : 0 }, 4);
} catch { } ControlHelper.Adjust(this, darkTheme, changed);
} }
} }
public class RTrackBar : TrackBar
{
}
public class RComboBox : ComboBox public class RComboBox : ComboBox
{ {
private Color borderColor = Color.Gray; private Color borderColor = Color.Gray;
@@ -224,10 +228,10 @@ namespace CustomControls
private int borderRadius = 5; private int borderRadius = 5;
private bool activated = false; private bool activated = false;
private bool secondary = false;
private Color borderColor = Color.Transparent; private Color borderColor = Color.Transparent;
public Color BorderColor public Color BorderColor
{ {
get { return borderColor; } get { return borderColor; }
@@ -250,6 +254,14 @@ namespace CustomControls
} }
} }
public bool Secondary
{
get { return secondary; }
set
{
secondary = value;
}
}
public RButton() public RButton()
{ {

27
app/Fans.Designer.cs generated
View File

@@ -31,10 +31,10 @@ namespace GHelper
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
ChartArea chartArea3 = new ChartArea(); ChartArea chartArea1 = new ChartArea();
Title title3 = new Title(); Title title1 = new Title();
ChartArea chartArea4 = new ChartArea(); ChartArea chartArea2 = new ChartArea();
Title title4 = new Title(); Title title2 = new Title();
panelFans = new Panel(); panelFans = new Panel();
labelTip = new Label(); labelTip = new Label();
labelBoost = new Label(); labelBoost = new Label();
@@ -160,8 +160,8 @@ namespace GHelper
// //
// chartGPU // chartGPU
// //
chartArea3.Name = "ChartArea1"; chartArea1.Name = "ChartArea1";
chartGPU.ChartAreas.Add(chartArea3); chartGPU.ChartAreas.Add(chartArea1);
chartGPU.Dock = DockStyle.Fill; chartGPU.Dock = DockStyle.Fill;
chartGPU.Location = new Point(2, 506); chartGPU.Location = new Point(2, 506);
chartGPU.Margin = new Padding(2, 10, 2, 10); chartGPU.Margin = new Padding(2, 10, 2, 10);
@@ -169,13 +169,13 @@ namespace GHelper
chartGPU.Size = new Size(760, 476); chartGPU.Size = new Size(760, 476);
chartGPU.TabIndex = 17; chartGPU.TabIndex = 17;
chartGPU.Text = "chart1"; chartGPU.Text = "chart1";
title3.Name = "Title1"; title1.Name = "Title1";
chartGPU.Titles.Add(title3); chartGPU.Titles.Add(title1);
// //
// chartCPU // chartCPU
// //
chartArea4.Name = "ChartArea1"; chartArea2.Name = "ChartArea1";
chartCPU.ChartAreas.Add(chartArea4); chartCPU.ChartAreas.Add(chartArea2);
chartCPU.Dock = DockStyle.Fill; chartCPU.Dock = DockStyle.Fill;
chartCPU.Location = new Point(2, 10); chartCPU.Location = new Point(2, 10);
chartCPU.Margin = new Padding(2, 10, 2, 10); chartCPU.Margin = new Padding(2, 10, 2, 10);
@@ -183,8 +183,8 @@ namespace GHelper
chartCPU.Size = new Size(760, 476); chartCPU.Size = new Size(760, 476);
chartCPU.TabIndex = 14; chartCPU.TabIndex = 14;
chartCPU.Text = "chartCPU"; chartCPU.Text = "chartCPU";
title4.Name = "Title1"; title2.Name = "Title1";
chartCPU.Titles.Add(title4); chartCPU.Titles.Add(title2);
// //
// labelFans // labelFans
// //
@@ -219,6 +219,7 @@ namespace GHelper
buttonReset.Location = new Point(30, 1081); buttonReset.Location = new Point(30, 1081);
buttonReset.Margin = new Padding(4, 2, 4, 2); buttonReset.Margin = new Padding(4, 2, 4, 2);
buttonReset.Name = "buttonReset"; buttonReset.Name = "buttonReset";
buttonReset.Secondary = true;
buttonReset.Size = new Size(232, 44); buttonReset.Size = new Size(232, 44);
buttonReset.TabIndex = 15; buttonReset.TabIndex = 15;
buttonReset.Text = "Factory Defaults"; buttonReset.Text = "Factory Defaults";
@@ -234,6 +235,7 @@ namespace GHelper
buttonApply.Location = new Point(542, 1081); buttonApply.Location = new Point(542, 1081);
buttonApply.Margin = new Padding(4, 2, 4, 2); buttonApply.Margin = new Padding(4, 2, 4, 2);
buttonApply.Name = "buttonApply"; buttonApply.Name = "buttonApply";
buttonApply.Secondary = true;
buttonApply.Size = new Size(248, 44); buttonApply.Size = new Size(248, 44);
buttonApply.TabIndex = 14; buttonApply.TabIndex = 14;
buttonApply.Text = "Apply Fan Curve"; buttonApply.Text = "Apply Fan Curve";
@@ -303,6 +305,7 @@ namespace GHelper
buttonApplyPower.Location = new Point(20, 1081); buttonApplyPower.Location = new Point(20, 1081);
buttonApplyPower.Margin = new Padding(4, 2, 4, 2); buttonApplyPower.Margin = new Padding(4, 2, 4, 2);
buttonApplyPower.Name = "buttonApplyPower"; buttonApplyPower.Name = "buttonApplyPower";
buttonApplyPower.Secondary = true;
buttonApplyPower.Size = new Size(324, 44); buttonApplyPower.Size = new Size(324, 44);
buttonApplyPower.TabIndex = 24; buttonApplyPower.TabIndex = 24;
buttonApplyPower.Text = "Apply Power Limits"; buttonApplyPower.Text = "Apply Power Limits";

View File

@@ -49,7 +49,8 @@ namespace GHelper
} }
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged; SystemEvents.UserPreferenceChanged += new
UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
Application.EnableVisualStyles(); 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; if (Math.Abs(DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastTheme) < 2000) return;
@@ -91,13 +92,13 @@ namespace GHelper
{ {
case UserPreferenceCategory.General: case UserPreferenceCategory.General:
Debug.WriteLine("Theme Changed"); Debug.WriteLine("Theme Changed");
settingsForm.InitTheme(); settingsForm.InitTheme(false);
if (settingsForm.fans is not null && settingsForm.fans.Text != "") 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 != "") if (settingsForm.keyb is not null && settingsForm.keyb.Text != "")
settingsForm.keyb.InitTheme(); settingsForm.keyb.InitTheme(false);
break; break;
} }

View File

@@ -190,6 +190,7 @@ namespace GHelper
buttonMatrix.TabIndex = 43; buttonMatrix.TabIndex = 43;
buttonMatrix.Text = "Picture / Gif"; buttonMatrix.Text = "Picture / Gif";
buttonMatrix.UseVisualStyleBackColor = false; buttonMatrix.UseVisualStyleBackColor = false;
buttonMatrix.Secondary = true;
// //
// comboMatrixRunning // comboMatrixRunning
// //
@@ -334,6 +335,7 @@ namespace GHelper
buttonQuit.TabIndex = 18; buttonQuit.TabIndex = 18;
buttonQuit.Text = "Quit"; buttonQuit.Text = "Quit";
buttonQuit.UseVisualStyleBackColor = false; buttonQuit.UseVisualStyleBackColor = false;
buttonQuit.Secondary = true;
// //
// checkStartup // checkStartup
// //
@@ -498,6 +500,7 @@ namespace GHelper
buttonFans.Text = "Fans + Power"; buttonFans.Text = "Fans + Power";
buttonFans.TextImageRelation = TextImageRelation.ImageAboveText; buttonFans.TextImageRelation = TextImageRelation.ImageAboveText;
buttonFans.UseVisualStyleBackColor = false; buttonFans.UseVisualStyleBackColor = false;
buttonFans.Secondary = true;
// //
// panelGPU // panelGPU
// //
@@ -833,6 +836,8 @@ namespace GHelper
buttonKeyboard.TabIndex = 37; buttonKeyboard.TabIndex = 37;
buttonKeyboard.Text = "Extra"; buttonKeyboard.Text = "Extra";
buttonKeyboard.UseVisualStyleBackColor = false; buttonKeyboard.UseVisualStyleBackColor = false;
buttonKeyboard.Secondary = true;
// //
// comboKeyboard // comboKeyboard
// //