Skip to content

Commit 8cd6f2b

Browse files
memoarfaaKlausLoeffelmann
authored andcommitted
Add Dark Mode theme to rows/columns and ColumnHeader/RowHeader in the DataGridView
1 parent 184224d commit 8cd6f2b

8 files changed

+64
-11
lines changed

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewButtonCell.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public partial class DataGridViewButtonCell : DataGridViewCell
1717
private static readonly int s_propButtonCellFlatStyle = PropertyStore.CreateKey();
1818
private static readonly int s_propButtonCellState = PropertyStore.CreateKey();
1919
private static readonly int s_propButtonCellUseColumnTextForButtonValue = PropertyStore.CreateKey();
20-
private static readonly VisualStyleElement s_buttonElement = VisualStyleElement.Button.PushButton.Normal;
20+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
21+
private static readonly VisualStyleElement s_darkButtonElement = VisualStyleElement.CreateElement("DarkMode_Explorer::BUTTON", 1, 1);
22+
private static readonly VisualStyleElement s_lightButtonElement = VisualStyleElement.Button.PushButton.Normal;
23+
private static readonly VisualStyleElement s_buttonElement = Application.IsDarkModeEnabled ? s_darkButtonElement : s_lightButtonElement;
24+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2125

2226
private const byte DATAGRIDVIEWBUTTONCELL_themeMargin = 100; // Used to calculate the margins required for theming rendering
2327
private const byte DATAGRIDVIEWBUTTONCELL_horizontalTextMargin = 2;

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewCheckBoxCell.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public partial class DataGridViewCheckBoxCell : DataGridViewCell, IDataGridViewE
2020
private const DataGridViewContentAlignment AnyBottom = DataGridViewContentAlignment.BottomRight | DataGridViewContentAlignment.BottomCenter | DataGridViewContentAlignment.BottomLeft;
2121
private const DataGridViewContentAlignment AnyMiddle = DataGridViewContentAlignment.MiddleRight | DataGridViewContentAlignment.MiddleCenter | DataGridViewContentAlignment.MiddleLeft;
2222

23-
private static readonly VisualStyleElement s_checkBoxElement = VisualStyleElement.Button.CheckBox.UncheckedNormal;
23+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
24+
private static readonly VisualStyleElement s_darkCheckBoxElement = VisualStyleElement.CreateElement("DarkMode_Explorer::BUTTON", 3, 1);
25+
private static readonly VisualStyleElement s_lightCheckBoxElement = VisualStyleElement.Button.CheckBox.UncheckedNormal;
26+
private static readonly VisualStyleElement s_checkBoxElement = Application.IsDarkModeEnabled ? s_darkCheckBoxElement : s_lightCheckBoxElement;
27+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
28+
2429
private static readonly int s_propButtonCellState = PropertyStore.CreateKey();
2530
private static readonly int s_propTrueValue = PropertyStore.CreateKey();
2631
private static readonly int s_propFalseValue = PropertyStore.CreateKey();

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewColumnHeaderCell.DataGridViewColumnHeaderCellRenderer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public static VisualStyleRenderer VisualStyleRenderer
1616
{
1717
get
1818
{
19-
s_visualStyleRenderer ??= new VisualStyleRenderer(s_headerElement);
19+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
20+
s_visualStyleRenderer ??= new VisualStyleRenderer(Application.IsDarkModeEnabled ?
21+
VisualStyleElement.CreateElement("DarkMode_ItemsView::Header", 1, 1)
22+
: VisualStyleElement.Header.Item.Normal);
23+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2024

2125
return s_visualStyleRenderer;
2226
}

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewColumnHeaderCell.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ namespace System.Windows.Forms;
1111

1212
public partial class DataGridViewColumnHeaderCell : DataGridViewHeaderCell
1313
{
14-
private static readonly VisualStyleElement s_headerElement = VisualStyleElement.Header.Item.Normal;
14+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
15+
private static readonly VisualStyleElement s_headerElement = Application.IsDarkModeEnabled ?
16+
VisualStyleElement.CreateElement("DarkMode_ItemsView::Header", 1, 1)
17+
: VisualStyleElement.Header.Item.Normal;
18+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1519

1620
private const byte SortGlyphSeparatorWidth = 2; // additional 2 pixels between caption and glyph
1721
private const byte SortGlyphHorizontalMargin = 4; // 4 pixels on left & right of glyph

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewComboBoxCell.DataGridViewComboBoxCellRenderer.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,29 @@ private static class DataGridViewComboBoxCellRenderer
1212
{
1313
[ThreadStatic]
1414
private static VisualStyleRenderer? t_visualStyleRenderer;
15-
private static readonly VisualStyleElement s_comboBoxBorder = VisualStyleElement.ComboBox.Border.Normal;
16-
private static readonly VisualStyleElement s_comboBoxDropDownButtonRight = VisualStyleElement.ComboBox.DropDownButtonRight.Normal;
17-
private static readonly VisualStyleElement s_comboBoxDropDownButtonLeft = VisualStyleElement.ComboBox.DropDownButtonLeft.Normal;
18-
private static readonly VisualStyleElement s_comboBoxReadOnlyButton = VisualStyleElement.ComboBox.ReadOnlyButton.Normal;
15+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
16+
private static readonly VisualStyleElement s_comboBoxBorder = Application.IsDarkModeEnabled ?
17+
VisualStyleElement.CreateElement("DarkMode_CFD::COMBOBOX", 4, 1)
18+
: VisualStyleElement.ComboBox.Border.Normal;
19+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
20+
21+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
22+
private static readonly VisualStyleElement s_comboBoxDropDownButtonRight = Application.IsDarkModeEnabled ?
23+
VisualStyleElement.CreateElement("DarkMode_CFD::COMBOBOX", 6, 1)
24+
: VisualStyleElement.ComboBox.DropDownButtonRight.Normal;
25+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
26+
27+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
28+
private static readonly VisualStyleElement s_comboBoxDropDownButtonLeft = Application.IsDarkModeEnabled ?
29+
VisualStyleElement.CreateElement("DarkMode_CFD::COMBOBOX", 7, 1) :
30+
VisualStyleElement.ComboBox.DropDownButtonLeft.Normal;
31+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
32+
33+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
34+
private static readonly VisualStyleElement s_comboBoxReadOnlyButton = Application.IsDarkModeEnabled ?
35+
VisualStyleElement.CreateElement("DarkMode_CFD::COMBOBOX", 5, 1)
36+
: VisualStyleElement.ComboBox.ReadOnlyButton.Normal;
37+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1938

2039
public static VisualStyleRenderer VisualStyleRenderer
2140
{

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewRowHeaderCell.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ namespace System.Windows.Forms;
1111

1212
public partial class DataGridViewRowHeaderCell : DataGridViewHeaderCell
1313
{
14-
private static readonly VisualStyleElement s_headerElement = VisualStyleElement.Header.Item.Normal;
14+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
15+
private static readonly VisualStyleElement s_headerElement = Application.IsDarkModeEnabled ?
16+
VisualStyleElement.CreateElement("DarkMode_ItemsView::Header", 1, 1)
17+
: VisualStyleElement.Header.Item.Normal;
18+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1519

1620
private static Bitmap? s_rightArrowBmp;
1721
private static Bitmap? s_leftArrowBmp;

src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewTopLeftHeaderCell.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ namespace System.Windows.Forms;
88

99
public partial class DataGridViewTopLeftHeaderCell : DataGridViewColumnHeaderCell
1010
{
11-
private static readonly VisualStyleElement s_headerElement = VisualStyleElement.Header.Item.Normal;
11+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
12+
private static readonly VisualStyleElement s_headerElement = Application.IsDarkModeEnabled ?
13+
VisualStyleElement.CreateElement("DarkMode_ItemsView::Header", 1, 1)
14+
: VisualStyleElement.Header.Item.Normal;
15+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1216

1317
private const byte DATAGRIDVIEWTOPLEFTHEADERCELL_horizontalTextMarginLeft = 1;
1418
private const byte DATAGRIDVIEWTOPLEFTHEADERCELL_horizontalTextMarginRight = 2;

src/System.Windows.Forms/System/Windows/Forms/Controls/TabControl/TabRenderer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,16 @@ public static void DrawTabPage(Graphics g, Rectangle bounds)
144144

145145
internal static void DrawTabPage(IDeviceContext deviceContext, Rectangle bounds)
146146
{
147-
InitializeRenderer(VisualStyleElement.Tab.Pane.Normal, 0);
147+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
148+
// Using DarkMode Theme Subclass.
149+
// see https://learn.microsoft.com/en-us/windows/win32/controls/theme-subclasses.
150+
VisualStyleElement darkTabPaneElement = VisualStyleElement.CreateElement("DarkMode::ExplorerNavPane", 0, 0);
151+
VisualStyleElement lightTabPaneElement = VisualStyleElement.Tab.Pane.Normal;
152+
VisualStyleElement tabPaneElement = Application.IsDarkModeEnabled
153+
? darkTabPaneElement
154+
: lightTabPaneElement;
155+
InitializeRenderer(tabPaneElement, 0);
156+
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
148157
t_visualStyleRenderer.DrawBackground(deviceContext, bounds);
149158
}
150159

0 commit comments

Comments
 (0)