@@ -72,6 +72,7 @@ public partial class TabControl : Control
72
72
private bool _skipUpdateSize ;
73
73
74
74
private ToolTipBuffer _toolTipBuffer ;
75
+ private bool _suspendDarkModeChange ;
75
76
76
77
/// <summary>
77
78
/// Constructs a TabBase object, usually as the base class for a TabStrip or TabControl.
@@ -157,7 +158,10 @@ public TabAppearance Appearance
157
158
SourceGenerated . EnumValidator . Validate ( value ) ;
158
159
159
160
_appearance = value ;
161
+
162
+ _suspendDarkModeChange = true ;
160
163
RecreateHandle ( ) ;
164
+ ApplyDarkModeOnDemand ( ) ;
161
165
162
166
// Fire OnStyleChanged(EventArgs.Empty) here since we are no longer calling UpdateStyles( ) but always reCreating the Handle.
163
167
OnStyleChanged ( EventArgs . Empty ) ;
@@ -1244,6 +1248,7 @@ protected override void OnHandleCreated(EventArgs e)
1244
1248
base . OnHandleCreated ( e ) ;
1245
1249
_cachedDisplayRect = Rectangle . Empty ;
1246
1250
ApplyItemSize ( ) ;
1251
+
1247
1252
if ( _imageList is not null )
1248
1253
{
1249
1254
PInvokeCore . SendMessage ( this , PInvoke . TCM_SETIMAGELIST , 0 , _imageList . Handle ) ;
@@ -1289,15 +1294,22 @@ protected override void OnHandleCreated(EventArgs e)
1289
1294
}
1290
1295
1291
1296
UpdateTabSelection ( false ) ;
1297
+ ApplyDarkModeOnDemand ( ) ;
1298
+ }
1292
1299
1293
1300
#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 )
1295
1305
{
1296
1306
PInvoke . SetWindowTheme ( HWND , null , $ "{ DarkModeIdentifier } ::{ BannerContainerThemeIdentifier } ") ;
1297
1307
PInvokeCore . EnumChildWindows ( this , StyleChildren ) ;
1298
1308
}
1299
- #pragma warning restore WFO5001
1309
+
1310
+ _suspendDarkModeChange = false ;
1300
1311
}
1312
+ #pragma warning restore WFO5001
1301
1313
1302
1314
protected override void OnHandleDestroyed ( EventArgs e )
1303
1315
{
@@ -1820,6 +1832,7 @@ protected void UpdateTabSelection(bool updateFocus)
1820
1832
1821
1833
// make current panel invisible
1822
1834
TabPage [ ] tabPages = GetTabPages ( ) ;
1835
+
1823
1836
if ( index != - 1 )
1824
1837
{
1825
1838
// Changing the bounds of the TabPage during scaling
@@ -1901,6 +1914,7 @@ protected override void OnStyleChanged(EventArgs e)
1901
1914
{
1902
1915
base . OnStyleChanged ( e ) ;
1903
1916
_cachedDisplayRect = Rectangle . Empty ;
1917
+
1904
1918
UpdateTabSelection ( false ) ;
1905
1919
}
1906
1920
0 commit comments