|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Drawing; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows.Forms; |
| 10 | + |
| 11 | + |
| 12 | +namespace RacingDSX |
| 13 | +{ |
| 14 | + |
| 15 | + public class ThemeManager |
| 16 | + { |
| 17 | + public static AppTheme CurrentTheme { get; set; } = Themes.Dark; |
| 18 | + |
| 19 | + public static bool IsDesignMode => |
| 20 | + LicenseManager.UsageMode == LicenseUsageMode.Designtime; |
| 21 | + |
| 22 | + public static void ApplyTheme(Control control) |
| 23 | + { |
| 24 | + if (control == null) |
| 25 | + return; |
| 26 | + |
| 27 | + Suspend(control); |
| 28 | + |
| 29 | + ApplyToControl(control); |
| 30 | + |
| 31 | + Resume(control); |
| 32 | + } |
| 33 | + |
| 34 | + private static void ApplyToControl(Control control) |
| 35 | + { |
| 36 | + control.BackColor = CurrentTheme.BackColor; |
| 37 | + control.ForeColor = CurrentTheme.ForeColor; |
| 38 | + |
| 39 | + if (control is Button button) |
| 40 | + { |
| 41 | + button.ForeColor = CurrentTheme.ButtonForeColor; |
| 42 | + button.BackColor = CurrentTheme.ButtonBackColor; |
| 43 | + } |
| 44 | + |
| 45 | + if (control is TextBox || control is ListView || control is ListBox || control is ComboBox) |
| 46 | + { |
| 47 | + control.ForeColor = CurrentTheme.TextBoxForeColor; |
| 48 | + control.BackColor = CurrentTheme.TextBoxBackColor; |
| 49 | + } |
| 50 | + |
| 51 | + if (control is ToolStrip toolStrip) |
| 52 | + { |
| 53 | + ApplyToolStripTheme(toolStrip); |
| 54 | + } |
| 55 | + |
| 56 | + foreach (Control child in control.Controls) |
| 57 | + { |
| 58 | + ApplyToControl(child); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static void ApplyToolStripTheme(ToolStrip toolStrip) |
| 63 | + { |
| 64 | + toolStrip.BackColor = CurrentTheme.SurfaceColor; |
| 65 | + toolStrip.ForeColor = CurrentTheme.ForeColor; |
| 66 | + |
| 67 | + foreach (ToolStripItem item in toolStrip.Items) |
| 68 | + { |
| 69 | + item.BackColor = CurrentTheme.SurfaceColor; |
| 70 | + item.ForeColor = CurrentTheme.ForeColor; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private static void Suspend(Control control) |
| 76 | + { |
| 77 | + control.SuspendLayout(); |
| 78 | + |
| 79 | + foreach (Control child in control.Controls) |
| 80 | + { |
| 81 | + Suspend(child); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void Resume(Control control) |
| 86 | + { |
| 87 | + foreach (Control child in control.Controls) |
| 88 | + { |
| 89 | + Resume(child); |
| 90 | + } |
| 91 | + |
| 92 | + control.ResumeLayout(); |
| 93 | + } |
| 94 | + |
| 95 | + public static bool IsDarkMode() |
| 96 | + { |
| 97 | + using var key = Registry.CurrentUser.OpenSubKey( |
| 98 | + @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); |
| 99 | + |
| 100 | + if (key?.GetValue("AppsUseLightTheme") is int value) |
| 101 | + { |
| 102 | + return value == 0; |
| 103 | + } |
| 104 | + |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public class AppTheme |
| 110 | + { |
| 111 | + public Color BackColor { get; set; } |
| 112 | + public Color SurfaceColor { get; set; } |
| 113 | + public Color ForeColor { get; set; } |
| 114 | + public Color BorderColor { get; set; } |
| 115 | + public Color AccentColor { get; set; } |
| 116 | + public Color ButtonBackColor { get; set; } |
| 117 | + public Color ButtonForeColor { get; set; } |
| 118 | + public Color TextBoxBackColor { get; set; } |
| 119 | + public Color TextBoxForeColor { get; set; } |
| 120 | + } |
| 121 | + |
| 122 | + public static class Themes |
| 123 | + { |
| 124 | + public static readonly AppTheme Plain = new() |
| 125 | + { |
| 126 | + BackColor = SystemColors.Control, |
| 127 | + SurfaceColor = SystemColors.ControlLight, |
| 128 | + ForeColor = SystemColors.ControlText, |
| 129 | + BorderColor = SystemColors.ControlDark, |
| 130 | + AccentColor = SystemColors.Highlight, |
| 131 | + ButtonBackColor = SystemColors.Control, |
| 132 | + ButtonForeColor = SystemColors.ControlText, |
| 133 | + TextBoxBackColor = SystemColors.Window, |
| 134 | + TextBoxForeColor = SystemColors.WindowText |
| 135 | + }; |
| 136 | + |
| 137 | + public static AppTheme Dark = new() |
| 138 | + { |
| 139 | + BackColor = Color.FromArgb(30, 30, 30), |
| 140 | + SurfaceColor = Color.FromArgb(45, 45, 45), |
| 141 | + ForeColor = Color.White, |
| 142 | + BorderColor = Color.FromArgb(70, 70, 70), |
| 143 | + AccentColor = Color.FromArgb(0, 120, 215), |
| 144 | + ButtonBackColor = Color.FromArgb(60, 60, 60), |
| 145 | + ButtonForeColor = Color.White, |
| 146 | + TextBoxBackColor = Color.FromArgb(50, 50, 50), |
| 147 | + TextBoxForeColor = Color.White |
| 148 | + }; |
| 149 | + |
| 150 | + public static AppTheme Light = new() |
| 151 | + { |
| 152 | + BackColor = Color.White, |
| 153 | + SurfaceColor = Color.FromArgb(240, 240, 240), |
| 154 | + ForeColor = Color.Black, |
| 155 | + BorderColor = Color.LightGray, |
| 156 | + AccentColor = Color.FromArgb(0, 120, 215), |
| 157 | + ButtonBackColor = Color.FromArgb(230, 230, 230), |
| 158 | + ButtonForeColor = Color.Black, |
| 159 | + TextBoxBackColor = Color.White, |
| 160 | + TextBoxForeColor = Color.Black |
| 161 | + }; |
| 162 | + } |
| 163 | +} |
0 commit comments