Changeset 14 for Common/Theme.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/Theme.cs
r13 r14 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Text;5 using System.Threading.Tasks;6 1 using System.Windows.Forms; 7 2 using System.Drawing; 8 3 using System.ComponentModel; 9 using System.IO;10 4 11 5 namespace Common … … 14 8 { 15 9 public delegate void ApplyThemeHandler(Component component); 16 static public event ApplyThemeHandler OnApplyTheme = null;17 static public Color ColorWindow;18 static public Color ColorWindowText;19 static public Color ColorControl;20 static public Color ColorControlText;21 static public Color ColorControlSelected;22 static public bool used = false;23 static private stringname;24 static public string Name25 { 26 get { return name; }10 public static event ApplyThemeHandler OnApplyTheme; 11 public static Color ColorWindow; 12 public static Color ColorWindowText; 13 public static Color ColorControl; 14 public static Color ColorControlText; 15 public static Color ColorControlSelected; 16 public static bool Used; 17 private static string _name; 18 public static string Name 19 { 20 get => _name; 27 21 set 28 22 { 29 name = value; 30 if (value == "Dark") 31 { 32 ColorWindow = Color.FromArgb(0x20, 0x20, 0x20); 33 ColorWindowText = Color.White; 34 ColorControl = Color.FromArgb(0x40, 0x40, 0x40); 35 ColorControlText = Color.White; 36 ColorControlSelected = Color.FromArgb(255, 96, 125, 155); 37 } 38 else 39 if (value == "Light") 40 { 41 ColorWindow = Color.White; 42 ColorWindowText = Color.Black; 43 ColorControl = Color.FromArgb(0xe0, 0xe0, 0xe0); 44 ColorControlText = Color.Black; 45 ColorControlSelected = Color.FromArgb(255, 196, 225, 255); 46 } 47 else 48 { 49 name = "System"; 50 ColorWindow = Color.FromKnownColor(KnownColor.Window); 51 ColorWindowText = Color.FromKnownColor(KnownColor.WindowText); 52 ColorControl = Color.FromKnownColor(KnownColor.Control); 53 ColorControlText = Color.FromKnownColor(KnownColor.ControlText); 54 ColorControlSelected = Color.FromArgb(255, 196, 225, 255); 55 } 23 _name = value; 24 switch (value) 25 { 26 case "Dark": 27 ColorWindow = Color.FromArgb(0x20, 0x20, 0x20); 28 ColorWindowText = Color.White; 29 ColorControl = Color.FromArgb(0x40, 0x40, 0x40); 30 ColorControlText = Color.White; 31 ColorControlSelected = Color.FromArgb(255, 96, 125, 155); 32 break; 33 case "Light": 34 ColorWindow = Color.White; 35 ColorWindowText = Color.Black; 36 ColorControl = Color.FromArgb(0xe0, 0xe0, 0xe0); 37 ColorControlText = Color.Black; 38 ColorControlSelected = Color.FromArgb(255, 196, 225, 255); 39 break; 40 default: 41 _name = "System"; 42 ColorWindow = Color.FromKnownColor(KnownColor.Window); 43 ColorWindowText = Color.FromKnownColor(KnownColor.WindowText); 44 ColorControl = Color.FromKnownColor(KnownColor.Control); 45 ColorControlText = Color.FromKnownColor(KnownColor.ControlText); 46 ColorControlSelected = Color.FromArgb(255, 196, 225, 255); 47 break; 48 } 56 49 } 57 50 } … … 72 65 // Exchange dark with light colors and vice versa 73 66 if (color == Color.DarkBlue) color = Color.LightBlue; 74 if (color == Color.LightBlue) color = Color.DarkBlue;67 else if (color == Color.LightBlue) color = Color.DarkBlue; 75 68 else if (color == Color.DarkGreen) color = Color.LightGreen; 76 69 else if (color == Color.LightGreen) color = Color.DarkGreen; … … 97 90 public static void ApplyTheme(Component component) 98 91 { 99 if (component is Control )100 { 101 foreach (Control child in (component as Control).Controls)92 if (component is Control control) 93 { 94 foreach (Control child in control.Controls) 102 95 { 103 96 ApplyTheme(child); 104 97 } 105 98 106 if ((co mponent is TextBox) || (component is NumericUpDown) || (component is ComboBox) || (componentis RichTextBox)107 || (co mponentis ListView))108 { 109 (component as Control).BackColor = ColorWindow;110 (component as Control).ForeColor = ColorWindowText;99 if ((control is TextBox) || (control is NumericUpDown) || (control is ComboBox) || (control is RichTextBox) 100 || (control is ListView)) 101 { 102 control.BackColor = ColorWindow; 103 control.ForeColor = ColorWindowText; 111 104 } 112 105 else 113 106 { 114 (component as Control).BackColor = ColorControl; 115 (component as Control).ForeColor = ColorControlText; 116 } 117 if ((component as Control).ContextMenuStrip != null) 118 { 119 ApplyTheme((component as Control).ContextMenuStrip); 120 } 121 } 122 123 if ((component is Button) && (Name == "System")) 124 { 125 (component as Button).UseVisualStyleBackColor = true; 126 } else 127 if (component is LinkLabel) 128 { 129 (component as LinkLabel).LinkColor = ColorControlText; 130 (component as LinkLabel).ActiveLinkColor = ColorControlText; 131 (component as LinkLabel).VisitedLinkColor = ColorControlText; 132 } else 133 if (component is TabControl) 134 { 135 foreach (TabPage tabPage in (component as TabControl).TabPages) 107 control.BackColor = ColorControl; 108 control.ForeColor = ColorControlText; 109 } 110 if (control.ContextMenuStrip != null) 111 { 112 ApplyTheme(control.ContextMenuStrip); 113 } 114 } 115 116 if (component is SplitButton splitButton) 117 { 118 ApplyTheme(splitButton.Menu); 119 } 120 if ((component is Button button) && (Name == "System")) 121 { 122 button.UseVisualStyleBackColor = true; 123 } else 124 if (component is LinkLabel linkLabel) 125 { 126 linkLabel.LinkColor = ColorControlText; 127 linkLabel.ActiveLinkColor = ColorControlText; 128 linkLabel.VisitedLinkColor = ColorControlText; 129 } else 130 if (component is TabControl tabControl) 131 { 132 foreach (TabPage tabPage in tabControl.TabPages) 136 133 { 137 134 ApplyTheme(tabPage); 138 135 } 139 136 } else 140 if (component is ToolStrip )141 { 142 (component as ToolStrip).RenderMode = ToolStripRenderMode.Professional;143 (component as ToolStrip).Renderer = new ToolStripProfessionalRenderer(new ThemeColorTable());144 foreach (ToolStripItem item in (component as ToolStrip).Items)145 { 146 if (item is ToolStripMenuItem )137 if (component is ToolStrip toolStrip) 138 { 139 toolStrip.RenderMode = ToolStripRenderMode.Professional; 140 toolStrip.Renderer = new ToolStripProfessionalRenderer(new ThemeColorTable()); 141 foreach (ToolStripItem item in toolStrip.Items) 142 { 143 if (item is ToolStripMenuItem toolStripMenuItem) 147 144 { 148 149 if ((item as ToolStripMenuItem).HasDropDownItems)150 foreach (ToolStripItem dropDownItem in (item as ToolStripMenuItem).DropDownItems)145 if (toolStripMenuItem.DropDownItems.Count > 0) 146 { 147 foreach (ToolStripItem dropDownItem in toolStripMenuItem.DropDownItems) 151 148 { 152 149 ApplyTheme(dropDownItem); 153 150 } 151 } 154 152 } 155 153 ApplyTheme(item); 156 154 } 157 155 } else 158 if (component is ToolStripItem) 159 { 160 (component as ToolStripItem).ForeColor = ColorControlText; 161 (component as ToolStripItem).BackColor = ColorControl; 162 } else 163 if (component is DataGridView) 164 { 165 (component as DataGridView).BackgroundColor = ColorWindow; 166 (component as DataGridView).DefaultCellStyle.BackColor = ColorWindow; 167 (component as DataGridView).DefaultCellStyle.ForeColor = ColorWindowText; 168 (component as DataGridView).ColumnHeadersDefaultCellStyle.BackColor = ColorWindow; 169 (component as DataGridView).ColumnHeadersDefaultCellStyle.ForeColor = ColorWindowText; 170 } else 171 if (component is RichTextBox) 172 { 173 (component as RichTextBox).SelectAll(); 174 (component as RichTextBox).SelectionColor = ColorWindowText; 175 (component as RichTextBox).SelectionLength = 0; 176 } else 177 178 if (OnApplyTheme != null) OnApplyTheme(component); 156 if (component is ToolStripItem toolStripItem) 157 { 158 toolStripItem.ForeColor = ColorControlText; 159 toolStripItem.BackColor = ColorControl; 160 if (toolStripItem is ToolStripMenuItem toolStripMenuItem) 161 { 162 if (toolStripMenuItem.DropDownItems.Count > 0) 163 { 164 foreach (ToolStripItem dropDownItem in toolStripMenuItem.DropDownItems) 165 { 166 ApplyTheme(dropDownItem); 167 } 168 } 169 } 170 } else 171 if (component is DataGridView dataGridView) 172 { 173 dataGridView.BackgroundColor = ColorWindow; 174 dataGridView.DefaultCellStyle.BackColor = ColorWindow; 175 dataGridView.DefaultCellStyle.ForeColor = ColorWindowText; 176 dataGridView.ColumnHeadersDefaultCellStyle.BackColor = ColorWindow; 177 dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = ColorWindowText; 178 } else 179 if (component is RichTextBox richTextBox) 180 { 181 richTextBox.SelectAll(); 182 richTextBox.SelectionColor = ColorWindowText; 183 richTextBox.SelectionLength = 0; 184 } else 185 { 186 OnApplyTheme?.Invoke(component); 187 } 179 188 } 180 189 181 190 public static void UseTheme(Form form) 182 191 { 183 if (! used && (name == "System")) return;192 if (!Used && (_name == "System")) return; 184 193 ApplyTheme(form); 185 used = true;194 Used = true; 186 195 } 187 196 } … … 189 198 public class ThemeColorTable : ProfessionalColorTable 190 199 { 191 public override Color MenuStripGradientBegin 192 { 193 get 194 { 195 return Theme.ColorControl; 196 } 197 } 198 199 public override Color MenuStripGradientEnd 200 { 201 get 202 { 203 return Theme.ColorControl; 204 } 205 } 206 207 public override Color ToolStripContentPanelGradientBegin 208 { 209 get 210 { 211 return Theme.ColorControl; 212 } 213 } 214 215 public override Color ToolStripContentPanelGradientEnd 216 { 217 get 218 { 219 return Theme.ColorControl; 220 } 221 } 222 223 public override Color ToolStripDropDownBackground 224 { 225 get 226 { 227 return Theme.ColorControl; 228 } 229 } 230 231 public override Color ToolStripGradientBegin 232 { 233 get 234 { 235 return Theme.ColorControl; 236 } 237 } 238 239 public override Color ToolStripGradientMiddle 240 { 241 get 242 { 243 return Theme.ColorControl; 244 } 245 } 246 247 public override Color ToolStripGradientEnd 248 { 249 get 250 { 251 return Theme.ColorControl; 252 } 253 } 254 255 public override Color MenuItemSelected 256 { 257 get 258 { 259 return Theme.ColorControlSelected; 260 } 261 } 262 263 public override Color MenuItemSelectedGradientBegin 264 { 265 get 266 { 267 return Theme.ColorControlSelected; 268 } 269 } 270 271 public override Color MenuItemSelectedGradientEnd 272 { 273 get 274 { 275 return Theme.ColorControlSelected; 276 } 277 } 278 279 public override Color ImageMarginGradientBegin 280 { 281 get 282 { 283 return Theme.ColorControl; 284 } 285 } 286 287 public override Color ImageMarginGradientMiddle 288 { 289 get 290 { 291 return Theme.ColorControl; 292 } 293 } 294 295 public override Color ImageMarginGradientEnd 296 { 297 get 298 { 299 return Theme.ColorControl; 300 } 301 } 302 303 public override Color MenuItemPressedGradientBegin 304 { 305 get 306 { 307 return Theme.ColorControlSelected; 308 } 309 } 310 311 public override Color MenuItemPressedGradientMiddle 312 { 313 get 314 { 315 return Theme.ColorControlSelected; 316 } 317 } 318 319 public override Color MenuItemPressedGradientEnd 320 { 321 get 322 { 323 return Theme.ColorControlSelected; 324 } 325 } 200 public override Color MenuStripGradientBegin => Theme.ColorControl; 201 public override Color MenuStripGradientEnd => Theme.ColorControl; 202 public override Color ToolStripContentPanelGradientBegin => Theme.ColorControl; 203 public override Color ToolStripContentPanelGradientEnd => Theme.ColorControl; 204 public override Color ToolStripDropDownBackground => Theme.ColorControl; 205 public override Color ToolStripGradientBegin => Theme.ColorControl; 206 public override Color ToolStripGradientMiddle => Theme.ColorControl; 207 public override Color ToolStripGradientEnd => Theme.ColorControl; 208 public override Color MenuItemSelected => Theme.ColorControlSelected; 209 public override Color MenuItemSelectedGradientBegin => Theme.ColorControlSelected; 210 public override Color MenuItemSelectedGradientEnd => Theme.ColorControlSelected; 211 public override Color ImageMarginGradientBegin => Theme.ColorControl; 212 public override Color ImageMarginGradientMiddle => Theme.ColorControl; 213 public override Color ImageMarginGradientEnd => Theme.ColorControl; 214 public override Color MenuItemPressedGradientBegin => Theme.ColorControlSelected; 215 public override Color MenuItemPressedGradientMiddle => Theme.ColorControlSelected; 216 public override Color MenuItemPressedGradientEnd => Theme.ColorControlSelected; 326 217 } 327 218 }
Note:
See TracChangeset
for help on using the changeset viewer.