Skip to content

Commit cfae159

Browse files
Fix DarkMode StatusStrip background renderer.
1 parent a64fee1 commit cfae159

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

src/System.Windows.Forms/System/Windows/Forms/Control.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ public virtual object? DataContext
814814
{
815815
Properties.RemoveValue(s_dataContextProperty);
816816
OnDataContextChanged(EventArgs.Empty);
817+
817818
return;
818819
}
819820

@@ -7332,6 +7333,18 @@ protected virtual void OnHandleCreated(EventArgs e)
73327333
SetWindowFont();
73337334
}
73347335

7336+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
7337+
if (Application.IsDarkModeEnabled && GetStyle(ControlStyles.ApplyThemingImplicitly))
7338+
{
7339+
HRESULT result = PInvoke.SetWindowTheme(
7340+
hwnd: HWND,
7341+
pszSubAppName: $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}",
7342+
pszSubIdList: null);
7343+
7344+
Debug.Assert(result.Succeeded, "SetWindowTheme failed with HRESULT: " + result);
7345+
}
7346+
#pragma warning restore WFO5001
7347+
73357348
HandleHighDpi();
73367349

73377350
// Restore drag drop status. Ole Initialize happens when the ThreadContext in Application is created.
@@ -10388,7 +10401,9 @@ protected virtual void SetVisibleCore(bool value)
1038810401
PrepareDarkMode(HWND, Application.IsDarkModeEnabled);
1038910402
}
1039010403

10391-
PInvoke.ShowWindow(HWND, value ? ShowParams : SHOW_WINDOW_CMD.SW_HIDE);
10404+
PInvoke.ShowWindow(HWND, value
10405+
? ShowParams
10406+
: SHOW_WINDOW_CMD.SW_HIDE);
1039210407
}
1039310408
}
1039410409
#pragma warning restore WFO5001
@@ -10400,6 +10415,7 @@ protected virtual void SetVisibleCore(bool value)
1040010415

1040110416
SetState(States.Visible, value);
1040210417
fireChange = true;
10418+
1040310419
try
1040410420
{
1040510421
if (value)
@@ -10408,14 +10424,19 @@ protected virtual void SetVisibleCore(bool value)
1040810424
}
1040910425

1041010426
PInvoke.SetWindowPos(
10411-
this,
10412-
HWND.Null,
10413-
0, 0, 0, 0,
10414-
SET_WINDOW_POS_FLAGS.SWP_NOSIZE
10427+
hWnd: this,
10428+
hWndInsertAfter: HWND.Null,
10429+
X: 0,
10430+
Y: 0,
10431+
cx: 0,
10432+
cy: 0,
10433+
uFlags: SET_WINDOW_POS_FLAGS.SWP_NOSIZE
1041510434
| SET_WINDOW_POS_FLAGS.SWP_NOMOVE
1041610435
| SET_WINDOW_POS_FLAGS.SWP_NOZORDER
1041710436
| SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE
10418-
| (value ? SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW : SET_WINDOW_POS_FLAGS.SWP_HIDEWINDOW));
10437+
| (value
10438+
? SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW
10439+
: SET_WINDOW_POS_FLAGS.SWP_HIDEWINDOW));
1041910440
}
1042010441
catch
1042110442
{
@@ -10469,27 +10490,30 @@ protected virtual void SetVisibleCore(bool value)
1046910490
if (IsHandleCreated)
1047010491
{
1047110492
PInvoke.SetWindowPos(
10472-
this,
10473-
HWND.HWND_TOP,
10474-
0, 0, 0, 0,
10475-
SET_WINDOW_POS_FLAGS.SWP_NOSIZE
10493+
hWnd: this,
10494+
hWndInsertAfter: HWND.HWND_TOP,
10495+
X: 0,
10496+
Y: 0,
10497+
cx: 0,
10498+
cy: 0,
10499+
uFlags: SET_WINDOW_POS_FLAGS.SWP_NOSIZE
1047610500
| SET_WINDOW_POS_FLAGS.SWP_NOMOVE
1047710501
| SET_WINDOW_POS_FLAGS.SWP_NOZORDER
1047810502
| SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE
1047910503
| (value ? SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW : SET_WINDOW_POS_FLAGS.SWP_HIDEWINDOW));
1048010504
}
1048110505
}
10506+
}
1048210507

10483-
static unsafe void PrepareDarkMode(HWND hwnd, bool darkModeEnabled)
10484-
{
10485-
BOOL value = darkModeEnabled;
10508+
private static unsafe void PrepareDarkMode(HWND hwnd, bool darkModeEnabled)
10509+
{
10510+
BOOL value = darkModeEnabled;
1048610511

10487-
PInvoke.DwmSetWindowAttribute(
10488-
hwnd,
10489-
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
10490-
&value,
10491-
(uint)sizeof(BOOL)).AssertSuccess();
10492-
}
10512+
PInvoke.DwmSetWindowAttribute(
10513+
hwnd,
10514+
DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE,
10515+
&value,
10516+
(uint)sizeof(BOOL)).AssertSuccess();
1049310517
}
1049410518

1049510519
/// <summary>

src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/StatusStrip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public override DockStyle Dock
118118
set => base.LayoutStyle = value;
119119
}
120120

121-
// we do some custom stuff with padding to accomodate size grip.
121+
// we do some custom stuff with padding to accommodate size grip.
122122
// changing this is not supported at DT
123123
[Browsable(false)]
124124
public new Padding Padding

src/System.Windows.Forms/System/Windows/Forms/Controls/ToolStrips/ToolStripSystemRenderer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,10 @@ private static void RenderStatusStripBorder(ToolStripRenderEventArgs e)
766766
}
767767
}
768768

769+
#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.
769770
private static void RenderStatusStripBackground(ToolStripRenderEventArgs e)
770771
{
771-
if (Application.RenderWithVisualStyles)
772+
if (!Application.IsDarkModeEnabled && Application.RenderWithVisualStyles)
772773
{
773774
VisualStyleRenderer vsRenderer = VisualStyleRenderer!;
774775
vsRenderer.SetParameters(VisualStyleElement.Status.Bar.Normal);
@@ -782,6 +783,7 @@ private static void RenderStatusStripBackground(ToolStripRenderEventArgs e)
782783
}
783784
}
784785
}
786+
#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.
785787

786788
private static void RenderLabelInternal(ToolStripItemRenderEventArgs e)
787789
{

0 commit comments

Comments
 (0)