Skip to content

Commit a5d56e5

Browse files
Implement DarkMode applying with [Re]CreateHandle.
1 parent 206e231 commit a5d56e5

File tree

1 file changed

+27
-10
lines changed
  • src/System.Windows.Forms/System/Windows/Forms

1 file changed

+27
-10
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7361,16 +7361,6 @@ protected virtual void OnHandleCreated(EventArgs e)
73617361
PInvoke.SetWindowText(this, _text);
73627362
}
73637363

7364-
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
7365-
if (Application.IsDarkModeEnabled && GetStyle(ControlStyles.ApplyThemingImplicitly))
7366-
{
7367-
_ = PInvoke.SetWindowTheme(
7368-
hwnd: HWND,
7369-
pszSubAppName: $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}",
7370-
pszSubIdList: null);
7371-
}
7372-
#pragma warning restore WFO5001
7373-
73747364
if (this is not ScrollableControl
73757365
&& !IsMirrored
73767366
&& GetExtendedState(ExtendedStates.SetScrollPosition)
@@ -7380,6 +7370,18 @@ protected virtual void OnHandleCreated(EventArgs e)
73807370
SetExtendedState(ExtendedStates.HaveInvoked, true);
73817371
SetExtendedState(ExtendedStates.SetScrollPosition, false);
73827372
}
7373+
7374+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
7375+
if (Application.IsDarkModeEnabled
7376+
&& GetStyle(ControlStyles.ApplyThemingImplicitly)
7377+
&& !RecreatingHandle)
7378+
{
7379+
_ = PInvoke.SetWindowTheme(
7380+
hwnd: HWND,
7381+
pszSubAppName: $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}",
7382+
pszSubIdList: null);
7383+
}
7384+
#pragma warning restore WFO5001
73837385
}
73847386

73857387
((EventHandler?)Events[s_handleCreatedEvent])?.Invoke(this, e);
@@ -9323,6 +9325,21 @@ internal virtual void RecreateHandleCore()
93239325
}
93249326
}
93259327

9328+
// Note that we need to take DarkMode theming into account at a different point in time
9329+
// than when we create the handle for the first time. The reason is that recreating the handle
9330+
// usually also recreates the handles of any child controls, and we want to
9331+
// ensure that the theming is applied to all child controls as well.
9332+
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
9333+
if (Application.IsDarkModeEnabled
9334+
&& GetStyle(ControlStyles.ApplyThemingImplicitly))
9335+
{
9336+
_ = PInvoke.SetWindowTheme(
9337+
hwnd: HWND,
9338+
pszSubAppName: $"{DarkModeIdentifier}_{ExplorerThemeIdentifier}",
9339+
pszSubIdList: null);
9340+
}
9341+
#pragma warning restore WFO5001
9342+
93269343
// Restore control focus
93279344
if (focused)
93289345
{

0 commit comments

Comments
 (0)