@@ -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.
@@ -154,7 +155,10 @@ public TabAppearance Appearance
154
155
SourceGenerated . EnumValidator . Validate ( value ) ;
155
156
156
157
_appearance = value ;
158
+
159
+ _suspendDarkModeChange = true ;
157
160
RecreateHandle ( ) ;
161
+ ApplyDarkModeOnDemand ( ) ;
158
162
159
163
// Fire OnStyleChanged(EventArgs.Empty) here since we are no longer calling UpdateStyles( ) but always reCreating the Handle.
160
164
OnStyleChanged ( EventArgs . Empty ) ;
@@ -1245,6 +1249,7 @@ protected override void OnHandleCreated(EventArgs e)
1245
1249
base . OnHandleCreated ( e ) ;
1246
1250
_cachedDisplayRect = Rectangle . Empty ;
1247
1251
ApplyItemSize ( ) ;
1252
+
1248
1253
if ( _imageList is not null )
1249
1254
{
1250
1255
PInvokeCore . SendMessage ( this , PInvoke . TCM_SETIMAGELIST , 0 , _imageList . Handle ) ;
@@ -1290,7 +1295,22 @@ protected override void OnHandleCreated(EventArgs e)
1290
1295
}
1291
1296
1292
1297
UpdateTabSelection ( false ) ;
1298
+ ApplyDarkModeOnDemand ( ) ;
1299
+ }
1300
+
1301
+ #pragma warning disable WFO5001
1302
+ private void ApplyDarkModeOnDemand ( )
1303
+ {
1304
+ // We need to avoid to apply the DarkMode theme twice on handle recreate.
1305
+ if ( ! _suspendDarkModeChange && Application . IsDarkModeEnabled )
1306
+ {
1307
+ PInvoke . SetWindowTheme ( HWND , null , $ "{ DarkModeIdentifier } ::{ BannerContainerThemeIdentifier } ") ;
1308
+ PInvokeCore . EnumChildWindows ( this , StyleChildren ) ;
1309
+ }
1310
+
1311
+ _suspendDarkModeChange = false ;
1293
1312
}
1313
+ #pragma warning restore WFO5001
1294
1314
1295
1315
protected override void OnHandleDestroyed ( EventArgs e )
1296
1316
{
@@ -1554,6 +1574,8 @@ internal override void RecreateHandleCore()
1554
1574
TabPages . Add ( tabPages [ i ] ) ;
1555
1575
}
1556
1576
1577
+ ApplyDarkModeOnDemand ( ) ;
1578
+
1557
1579
try
1558
1580
{
1559
1581
SetState ( State . FromCreateHandles , true ) ;
@@ -1772,6 +1794,10 @@ private bool ShouldSerializeItemSize()
1772
1794
return ! _padding . Equals ( s_defaultPaddingPoint ) ;
1773
1795
}
1774
1796
1797
+ private BOOL StyleChildren ( HWND handle ) =>
1798
+ PInvoke . SetWindowTheme ( handle , $ "{ DarkModeIdentifier } _{ ExplorerThemeIdentifier } ", null )
1799
+ . Succeeded ;
1800
+
1775
1801
/// <summary>
1776
1802
/// Returns a string representation for this control.
1777
1803
/// </summary>
@@ -1809,6 +1835,7 @@ protected void UpdateTabSelection(bool updateFocus)
1809
1835
1810
1836
// make current panel invisible
1811
1837
TabPage [ ] tabPages = GetTabPages ( ) ;
1838
+
1812
1839
if ( index != - 1 )
1813
1840
{
1814
1841
// Changing the bounds of the TabPage during scaling
@@ -1890,6 +1917,7 @@ protected override void OnStyleChanged(EventArgs e)
1890
1917
{
1891
1918
base . OnStyleChanged ( e ) ;
1892
1919
_cachedDisplayRect = Rectangle . Empty ;
1920
+
1893
1921
UpdateTabSelection ( false ) ;
1894
1922
}
1895
1923
0 commit comments