Auto theme change

This commit is contained in:
seerge
2023-03-20 16:42:51 +01:00
parent d82f1c8d70
commit 1cd808de07
7 changed files with 112 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
using System.Drawing.Drawing2D; using System;
using System.Drawing.Drawing2D;
using System.Windows.Forms.DataVisualization.Charting; using System.Windows.Forms.DataVisualization.Charting;
using CustomControls; using CustomControls;
@@ -6,27 +7,43 @@ public static class ControlHelper
{ {
static bool _invert = false; static bool _invert = false;
static bool _darkTheme = false;
static float _scale = 1; static float _scale = 1;
static Color formBack = Color.FromArgb(255, 35, 35, 35); static Color formBack;
static Color backMain = Color.FromArgb(255, 50, 50, 50); static Color backMain;
static Color foreMain = Color.White; static Color foreMain;
static Color borderMain = Color.FromArgb(255, 50, 50, 50); static Color borderMain;
static Color buttonMain = Color.FromArgb(255, 100,100,100); 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; _scale = GetDpiScale(container).Value / baseScale;
_invert = invert;
if (_invert) if (container.DarkTheme)
{ {
container.BackColor = formBack; formBack = Color.FromArgb(255, 35, 35, 35);
container.ForeColor = foreMain; 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); AdjustControls(container.Controls);
_invert = false;
} }
@@ -37,15 +54,11 @@ public static class ControlHelper
var button = control as Button; var button = control as Button;
if (button != null) if (button != null)
{ {
button.BackColor = backMain;
button.ForeColor = foreMain;
if (_invert) button.FlatStyle = FlatStyle.Flat;
{ button.FlatAppearance.BorderColor = borderMain;
button.BackColor = backMain;
button.ForeColor = foreMain;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderColor = borderMain;
}
if (button.Image is not null) if (button.Image is not null)
button.Image = AdjustImage(button.Image); button.Image = AdjustImage(button.Image);
@@ -59,16 +72,23 @@ public static class ControlHelper
} }
var combo = control as RComboBox; var combo = control as RComboBox;
if (combo != null && _invert) if (combo != null)
{ {
combo.BackColor = borderMain; combo.BackColor = backMain;
combo.ForeColor = foreMain; combo.ForeColor = foreMain;
combo.BorderColor = borderMain; combo.BorderColor = backMain;
combo.ButtonColor = buttonMain; combo.ButtonColor = buttonMain;
} }
var gb = control as GroupBox;
if (gb != null)
{
gb.ForeColor = foreMain;
}
var chart = control as Chart; var chart = control as Chart;
if (chart != null && _invert) if (chart != null)
{ {
chart.BackColor = backMain; chart.BackColor = backMain;
chart.ChartAreas[0].BackColor = backMain; chart.ChartAreas[0].BackColor = backMain;

View File

@@ -1,11 +1,10 @@
using GHelper; using System.ComponentModel;
using System.ComponentModel;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace CustomControls namespace CustomControls
{ {
public class RForm : Form public class RForm : Form
{ {
@@ -16,15 +15,31 @@ namespace CustomControls
[DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")] [DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")]
public static extern bool CheckSystemDarkModeStatus(); public static extern bool CheckSystemDarkModeStatus();
public static bool darkTheme = CheckSystemDarkModeStatus();
[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);
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) get { return _darkTheme; }
DwmSetWindowAttribute(Handle, 20, new[] { 1 }, 4); 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) if (!Enabled && ForeColor != SystemColors.ControlText)
{ {
var rect = pevent.ClipRectangle; var rect = pevent.ClipRectangle;
if (Image is not null) { if (Image is not null)
{
rect.Y += Image.Height; rect.Y += Image.Height;
rect.Height -= Image.Height; rect.Height -= Image.Height;
} }

View File

@@ -82,8 +82,7 @@ namespace GHelper
{ {
InitializeComponent(); InitializeComponent();
InitTheme();
ControlHelper.Adjust(this, 2, darkTheme);
labelTip.Visible = false; labelTip.Visible = false;
labelTip.BackColor = Color.Transparent; labelTip.BackColor = Color.Transparent;

View File

@@ -1,4 +1,6 @@
namespace GHelper using CustomControls;
namespace GHelper
{ {
partial class Keyboard partial class Keyboard
{ {
@@ -31,12 +33,12 @@
groupBox1 = new GroupBox(); groupBox1 = new GroupBox();
textM4 = new TextBox(); textM4 = new TextBox();
textM3 = new TextBox(); textM3 = new TextBox();
comboM4 = new ComboBox(); comboM4 = new RComboBox();
labelM4 = new Label(); labelM4 = new Label();
comboM3 = new ComboBox(); comboM3 = new RComboBox();
labelM3 = new Label(); labelM3 = new Label();
textFNF4 = new TextBox(); textFNF4 = new TextBox();
comboFNF4 = new ComboBox(); comboFNF4 = new RComboBox();
labelFNF4 = new Label(); labelFNF4 = new Label();
groupBox1.SuspendLayout(); groupBox1.SuspendLayout();
SuspendLayout(); SuspendLayout();
@@ -161,13 +163,13 @@
private GroupBox groupBox1; private GroupBox groupBox1;
private Label labelM3; private Label labelM3;
private ComboBox comboM3; private RComboBox comboM3;
private ComboBox comboM4; private RComboBox comboM4;
private Label labelM4; private Label labelM4;
private TextBox textM4; private TextBox textM4;
private TextBox textM3; private TextBox textM3;
private TextBox textFNF4; private TextBox textFNF4;
private ComboBox comboFNF4; private RComboBox comboFNF4;
private Label labelFNF4; private Label labelFNF4;
} }
} }

View File

@@ -1,6 +1,8 @@
namespace GHelper using CustomControls;
namespace GHelper
{ {
public partial class Keyboard : Form public partial class Keyboard : RForm
{ {
Dictionary<string, string> customActions = new Dictionary<string, string> Dictionary<string, string> customActions = new Dictionary<string, string>
@@ -51,6 +53,7 @@
public Keyboard() public Keyboard()
{ {
InitializeComponent(); InitializeComponent();
InitTheme();
SetKeyCombo(comboM3, textM3, "m3"); SetKeyCombo(comboM3, textM3, "m3");
SetKeyCombo(comboM4, textM4, "m4"); SetKeyCombo(comboM4, textM4, "m4");

View File

@@ -27,6 +27,7 @@ namespace GHelper
private static IntPtr ds; private static IntPtr ds;
private static long lastAuto; private static long lastAuto;
private static long lastTheme;
// The main entry point for the application // The main entry point for the application
public static void Main() public static void Main()
@@ -48,6 +49,8 @@ namespace GHelper
} }
SystemEvents.UserPreferenceChanged += OnUserPreferenceChanged;
Application.EnableVisualStyles(); Application.EnableVisualStyles();
ds = settingsForm.Handle; 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() static async void CheckForUpdates()
{ {

View File

@@ -17,16 +17,15 @@ namespace GHelper
public string perfName = "Balanced"; public string perfName = "Balanced";
Fans fans; public Fans fans;
Keyboard keyb; public Keyboard keyb;
static AnimeMatrixDevice mat; static AnimeMatrixDevice mat;
public SettingsForm() public SettingsForm()
{ {
InitializeComponent(); InitializeComponent();
InitTheme();
ControlHelper.Adjust(this, 2, darkTheme);
FormClosing += SettingsForm_FormClosing; FormClosing += SettingsForm_FormClosing;