mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Auto theme change
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,16 +53,12 @@ public static class ControlHelper
|
||||
{
|
||||
var button = control as Button;
|
||||
if (button != null)
|
||||
{
|
||||
|
||||
if (_invert)
|
||||
{
|
||||
button.BackColor = backMain;
|
||||
button.ForeColor = foreMain;
|
||||
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using GHelper;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -82,8 +82,7 @@ namespace GHelper
|
||||
{
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
ControlHelper.Adjust(this, 2, darkTheme);
|
||||
InitTheme();
|
||||
|
||||
labelTip.Visible = false;
|
||||
labelTip.BackColor = Color.Transparent;
|
||||
|
||||
16
app/Keyboard.Designer.cs
generated
16
app/Keyboard.Designer.cs
generated
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -51,6 +53,7 @@
|
||||
public Keyboard()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitTheme();
|
||||
|
||||
SetKeyCombo(comboM3, textM3, "m3");
|
||||
SetKeyCombo(comboM4, textM4, "m4");
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user