Skip to content

Commit ef1be81

Browse files
Change DarkMode theme switching on recreating the handle.
1 parent b294f70 commit ef1be81

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9149,9 +9149,12 @@ internal virtual void RecreateHandleCore()
91499149

91509150
bool focused = ContainsFocus;
91519151

9152-
Debug.WriteLineIf(CoreSwitches.PerfTrack.Enabled, $"RecreateHandle: {GetType().FullName} [Text={Text}]");
9152+
Debug.WriteLineIf(
9153+
CoreSwitches.PerfTrack.Enabled,
9154+
$"RecreateHandle: {GetType().FullName} [Text={Text}]");
91539155

91549156
bool created = GetState(States.Created);
9157+
91559158
if (GetState(States.TrackingMouseEvent))
91569159
{
91579160
SetState(States.MouseEnterPending, true);
@@ -9174,9 +9177,11 @@ internal virtual void RecreateHandleCore()
91749177
if (ChildControls is { } children && children.Count > 0)
91759178
{
91769179
controlSnapshot = new Control[children.Count];
9180+
91779181
for (int i = 0; i < children.Count; i++)
91789182
{
91799183
Control childControl = children[i];
9184+
91809185
if (childControl is not null && childControl.IsHandleCreated)
91819186
{
91829187
// SetParent to parking window
@@ -9236,6 +9241,7 @@ internal virtual void RecreateHandleCore()
92369241
for (int i = 0; i < controlSnapshot.Length; i++)
92379242
{
92389243
Control? childControl = controlSnapshot[i];
9244+
92399245
if (childControl is not null && childControl.IsHandleCreated)
92409246
{
92419247
// Re-parent the control.

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public partial class TabControl : Control
7272
private bool _skipUpdateSize;
7373

7474
private ToolTipBuffer _toolTipBuffer;
75+
private bool _suspendDarkModeChange;
7576

7677
/// <summary>
7778
/// Constructs a TabBase object, usually as the base class for a TabStrip or TabControl.
@@ -157,7 +158,10 @@ public TabAppearance Appearance
157158
SourceGenerated.EnumValidator.Validate(value);
158159

159160
_appearance = value;
161+
162+
_suspendDarkModeChange = true;
160163
RecreateHandle();
164+
ApplyDarkModeOnDemand();
161165

162166
// Fire OnStyleChanged(EventArgs.Empty) here since we are no longer calling UpdateStyles( ) but always reCreating the Handle.
163167
OnStyleChanged(EventArgs.Empty);
@@ -1244,6 +1248,7 @@ protected override void OnHandleCreated(EventArgs e)
12441248
base.OnHandleCreated(e);
12451249
_cachedDisplayRect = Rectangle.Empty;
12461250
ApplyItemSize();
1251+
12471252
if (_imageList is not null)
12481253
{
12491254
PInvokeCore.SendMessage(this, PInvoke.TCM_SETIMAGELIST, 0, _imageList.Handle);
@@ -1289,15 +1294,22 @@ protected override void OnHandleCreated(EventArgs e)
12891294
}
12901295

12911296
UpdateTabSelection(false);
1297+
ApplyDarkModeOnDemand();
1298+
}
12921299

12931300
#pragma warning disable WFO5001
1294-
if (Application.IsDarkModeEnabled)
1301+
private void ApplyDarkModeOnDemand()
1302+
{
1303+
// We need to avoid to apply the DarkMode theme twice on handle recreate.
1304+
if (!_suspendDarkModeChange && Application.IsDarkModeEnabled)
12951305
{
12961306
PInvoke.SetWindowTheme(HWND, null, $"{DarkModeIdentifier}::{BannerContainerThemeIdentifier}");
12971307
PInvokeCore.EnumChildWindows(this, StyleChildren);
12981308
}
1299-
#pragma warning restore WFO5001
1309+
1310+
_suspendDarkModeChange = false;
13001311
}
1312+
#pragma warning restore WFO5001
13011313

13021314
protected override void OnHandleDestroyed(EventArgs e)
13031315
{
@@ -1820,6 +1832,7 @@ protected void UpdateTabSelection(bool updateFocus)
18201832

18211833
// make current panel invisible
18221834
TabPage[] tabPages = GetTabPages();
1835+
18231836
if (index != -1)
18241837
{
18251838
// Changing the bounds of the TabPage during scaling
@@ -1901,6 +1914,7 @@ protected override void OnStyleChanged(EventArgs e)
19011914
{
19021915
base.OnStyleChanged(e);
19031916
_cachedDisplayRect = Rectangle.Empty;
1917+
19041918
UpdateTabSelection(false);
19051919
}
19061920

0 commit comments

Comments
 (0)