-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThemeManager.cs
More file actions
158 lines (135 loc) · 4.96 KB
/
Copy pathThemeManager.cs
File metadata and controls
158 lines (135 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Yotepad
{
public class ThemeManager
{
public bool IsDarkMode { get; private set; }
public Color BackgroundColor => IsDarkMode ? Color.FromArgb(30, 30, 30) : Color.White;
public Color TextColor => IsDarkMode ? Color.FromArgb(220, 220, 220) : Color.Black;
public Color MenuBackgroundColor => IsDarkMode ? Color.FromArgb(45, 45, 45) : SystemColors.Control;
public void InitializeTheme()
{
try
{
using (RegistryKey? key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"))
{
if (key?.GetValue("AppsUseLightTheme") is int lightTheme)
{
IsDarkMode = (lightTheme == 0);
}
}
}
catch { IsDarkMode = true; }
}
public void ToggleTheme() => IsDarkMode = !IsDarkMode;
public void ApplyTheme(Form form, TextBox textBox, MenuStrip menu, StatusStrip status)
{
// Call the undocumented API: 2 = ForceDark, 0 = Default Light
try { NativeMethods.SetPreferredAppMode(IsDarkMode ? 2 : 0); } catch { }
int darkVal = IsDarkMode ? 1 : 0;
NativeMethods.DwmSetWindowAttribute(form.Handle, NativeMethods.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkVal, sizeof(int));
NativeMethods.SetWindowTheme(textBox.Handle, IsDarkMode ? "DarkMode_Explorer" : "Explorer", null);
form.BackColor = BackgroundColor;
textBox.BackColor = BackgroundColor;
textBox.ForeColor = TextColor;
menu.Renderer = IsDarkMode ? new YotePadMenuRenderer(this) : new ToolStripProfessionalRenderer();
menu.BackColor = MenuBackgroundColor;
menu.ForeColor = TextColor;
status.BackColor = MenuBackgroundColor;
status.ForeColor = TextColor;
foreach (ToolStripMenuItem item in menu.Items)
{
ApplyMenuTheme(item);
}
}
private void ApplyMenuTheme(ToolStripMenuItem item)
{
item.ForeColor = TextColor;
if (item.DropDown is ToolStripDropDownMenu dropDown)
{
dropDown.ShowImageMargin = false;
dropDown.BackColor = MenuBackgroundColor;
dropDown.ForeColor = TextColor;
}
foreach (ToolStripItem subItem in item.DropDownItems)
{
if (subItem is ToolStripMenuItem subMenu)
{
subMenu.BackColor = MenuBackgroundColor;
ApplyMenuTheme(subMenu);
}
}
}
}
public class YotePadMenuRenderer : ToolStripProfessionalRenderer
{
private readonly ThemeManager _theme;
public YotePadMenuRenderer(ThemeManager theme) : base()
{
_theme = theme;
}
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
if (!_theme.IsDarkMode)
{
base.OnRenderMenuItemBackground(e);
return;
}
Rectangle rect = new Rectangle(Point.Empty, e.Item.Size);
if (e.Item.Selected || e.Item.Pressed)
{
using (SolidBrush brush = new SolidBrush(Color.FromArgb(80, 80, 80)))
{
e.Graphics.FillRectangle(brush, rect);
}
}
else
{
using (SolidBrush brush = new SolidBrush(_theme.MenuBackgroundColor))
{
e.Graphics.FillRectangle(brush, rect);
}
}
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
if (!_theme.IsDarkMode)
{
base.OnRenderToolStripBackground(e);
return;
}
using (SolidBrush brush = new SolidBrush(_theme.MenuBackgroundColor))
{
e.Graphics.FillRectangle(brush, e.AffectedBounds);
}
}
protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
{
if (!_theme.IsDarkMode)
{
base.OnRenderSeparator(e);
return;
}
int y = e.Item.Height / 2;
using (Pen pen = new Pen(Color.FromArgb(70, 70, 70)))
{
e.Graphics.DrawLine(pen, 4, y, e.Item.Width - 4, y);
}
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if (!_theme.IsDarkMode)
{
base.OnRenderItemText(e);
return;
}
e.TextColor = e.Item.Enabled
? Color.FromArgb(220, 220, 220)
: Color.FromArgb(110, 110, 110);
base.OnRenderItemText(e);
}
}
}