@@ -627,6 +627,10 @@ protected override CreateParams CreateParams
627
627
{
628
628
get
629
629
{
630
+ #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.
631
+ SetStyle ( ControlStyles . ApplyThemingImplicitly , true ) ;
632
+ #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.
633
+
630
634
CreateParams cp = base . CreateParams ;
631
635
632
636
cp . ClassName = PInvoke . WC_LISTVIEW ;
@@ -2553,6 +2557,8 @@ private int CompensateColumnHeaderResize(int columnIndex, bool columnResizeCance
2553
2557
2554
2558
protected override unsafe void CreateHandle ( )
2555
2559
{
2560
+ base . CreateHandle ( ) ;
2561
+
2556
2562
if ( ! RecreatingHandle )
2557
2563
{
2558
2564
using ThemingScope scope = new ( Application . UseVisualStyles ) ;
@@ -2563,8 +2569,6 @@ protected override unsafe void CreateHandle()
2563
2569
} ) ;
2564
2570
}
2565
2571
2566
- base . CreateHandle ( ) ;
2567
-
2568
2572
if ( BackgroundImage is not null )
2569
2573
{
2570
2574
SetBackgroundImage ( ) ;
@@ -2582,7 +2586,7 @@ protected override unsafe void CreateHandle()
2582
2586
/// </remarks>
2583
2587
private unsafe void CustomDraw ( ref Message m )
2584
2588
{
2585
- bool dontmess = false ;
2589
+ bool lockReturnValue = false ;
2586
2590
bool itemDrawDefault = false ;
2587
2591
2588
2592
try
@@ -2678,7 +2682,7 @@ private unsafe void CustomDraw(ref Message m)
2678
2682
if ( _viewStyle is View . Details or View . Tile )
2679
2683
{
2680
2684
m . ResultInternal = ( LRESULT ) ( nint ) ( PInvoke . CDRF_NOTIFYSUBITEMDRAW | PInvoke . CDRF_NEWFONT ) ;
2681
- dontmess = true ; // don't mess with our return value!
2685
+ lockReturnValue = true ; // Let's make sure we're not changing this.
2682
2686
2683
2687
// ITEMPREPAINT is used to work out the rect for the first column!!! GAH!!!
2684
2688
// (which means we can't just do our color/font work on SUBITEM|ITEM_PREPAINT)
@@ -2760,7 +2764,7 @@ private unsafe void CustomDraw(ref Message m)
2760
2764
// get the node
2761
2765
ListViewItem item = Items [ ( int ) nmcd ->nmcd . dwItemSpec ] ;
2762
2766
// if we're doing the whole row in one style, change our result!
2763
- if ( dontmess && item . UseItemStyleForSubItems )
2767
+ if ( lockReturnValue && item . UseItemStyleForSubItems )
2764
2768
{
2765
2769
m . ResultInternal = ( LRESULT ) ( nint ) PInvoke . CDRF_NEWFONT ;
2766
2770
}
@@ -2911,7 +2915,7 @@ private unsafe void CustomDraw(ref Message m)
2911
2915
PInvokeCore . SelectObject ( nmcd ->nmcd . hdc , _odCacheFontHandleWrapper . Handle ) ;
2912
2916
}
2913
2917
2914
- if ( ! dontmess )
2918
+ if ( ! lockReturnValue )
2915
2919
{
2916
2920
m . ResultInternal = ( LRESULT ) ( nint ) PInvoke . CDRF_NEWFONT ;
2917
2921
}
@@ -4539,14 +4543,15 @@ protected override void OnHandleCreated(EventArgs e)
4539
4543
4540
4544
UpdateExtendedStyles ( ) ;
4541
4545
RealizeProperties ( ) ;
4546
+
4542
4547
PInvokeCore . SendMessage ( this , PInvoke . LVM_SETBKCOLOR , ( WPARAM ) 0 , ( LPARAM ) BackColor ) ;
4543
4548
PInvokeCore . SendMessage ( this , PInvoke . LVM_SETTEXTCOLOR , ( WPARAM ) 0 , ( LPARAM ) ForeColor ) ;
4544
4549
4545
4550
// The native list view will not invalidate the entire list view item area if the BkColor is not CLR_NONE.
4546
4551
// This not noticeable if the customer paints the items w/ the same background color as the list view itself.
4547
4552
// However, if the customer paints the items w/ a color different from the list view's back color
4548
4553
// then when the user changes selection the native list view will not invalidate the entire list view item area.
4549
- PInvokeCore . SendMessage ( this , PInvoke . LVM_SETTEXTBKCOLOR , ( WPARAM ) 0 , ( LPARAM ) PInvokeCore . CLR_NONE ) ;
4554
+ // PInvokeCore.SendMessage(this, PInvoke.LVM_SETTEXTBKCOLOR, (WPARAM)0, (LPARAM)PInvokeCore.CLR_NONE);
4550
4555
4551
4556
// LVS_NOSCROLL does not work well when the list view is in View.Details or in View.List modes.
4552
4557
// we have to set this style after the list view was created and before we position the native list view items.
@@ -4590,18 +4595,22 @@ protected override void OnHandleCreated(EventArgs e)
4590
4595
4591
4596
// Use a copy of the list items array so that we can maintain the (handle created || listItemsArray is not null) invariant
4592
4597
ListViewItem [ ] ? listViewItemsToAdd = null ;
4598
+
4593
4599
if ( _listViewItems is not null )
4594
4600
{
4595
4601
listViewItemsToAdd = [ .. _listViewItems ] ;
4596
4602
_listViewItems = null ;
4597
4603
}
4598
4604
4599
- int columnCount = _columnHeaders is null ? 0 : _columnHeaders . Length ;
4605
+ int columnCount = _columnHeaders is null
4606
+ ? 0
4607
+ : _columnHeaders . Length ;
4600
4608
4601
4609
if ( columnCount > 0 )
4602
4610
{
4603
4611
int [ ] indices = new int [ columnCount ] ;
4604
4612
int index = 0 ;
4613
+
4605
4614
foreach ( ColumnHeader column in _columnHeaders ! )
4606
4615
{
4607
4616
indices [ index ] = column . DisplayIndex ;
@@ -4655,16 +4664,16 @@ protected override void OnHandleCreated(EventArgs e)
4655
4664
}
4656
4665
}
4657
4666
4658
- if ( ! RecreatingHandle )
4659
- {
4660
- ApplyDarkModeOnDemand ( ) ;
4661
- }
4667
+ // We need to wait for the next message loop
4668
+ // to apply dark mode on demand.
4669
+ BeginInvoke ( ApplyDarkModeOnDemand ) ;
4662
4670
}
4663
4671
4664
4672
#pragma warning disable WFO5001
4665
4673
private void ApplyDarkModeOnDemand ( )
4666
4674
{
4667
- if ( Application . IsDarkModeEnabled )
4675
+ if ( Application . IsDarkModeEnabled
4676
+ && GetStyle ( ControlStyles . ApplyThemingImplicitly ) )
4668
4677
{
4669
4678
// Enable double buffering when in dark mode to reduce flicker.
4670
4679
uint exMask = PInvoke . LVS_EX_ONECLICKACTIVATE | PInvoke . LVS_EX_TWOCLICKACTIVATE |
@@ -4689,10 +4698,17 @@ private void ApplyDarkModeOnDemand()
4689
4698
null ) ;
4690
4699
4691
4700
// Get the ListView's ColumnHeader handle:
4692
- HWND columnHeaderHandle = ( HWND ) PInvokeCore . SendMessage ( this , PInvoke . LVM_GETHEADER , ( WPARAM ) 0 , ( LPARAM ) 0 ) ;
4701
+ HWND columnHeaderHandle = ( HWND ) PInvokeCore . SendMessage (
4702
+ this ,
4703
+ PInvoke . LVM_GETHEADER ,
4704
+ ( WPARAM ) 0 ,
4705
+ ( LPARAM ) 0 ) ;
4693
4706
4694
4707
// Apply dark mode theme to the ColumnHeader
4695
- PInvoke . SetWindowTheme ( columnHeaderHandle , $ "{ DarkModeIdentifier } _{ ItemsViewThemeIdentifier } ", null ) ;
4708
+ PInvoke . SetWindowTheme (
4709
+ columnHeaderHandle ,
4710
+ $ "{ DarkModeIdentifier } _{ ItemsViewThemeIdentifier } ",
4711
+ null ) ;
4696
4712
}
4697
4713
}
4698
4714
#pragma warning restore WFO5001
@@ -4971,11 +4987,9 @@ private void RealizeAllSubItems()
4971
4987
4972
4988
protected void RealizeProperties ( )
4973
4989
{
4974
- // Realize state information
4975
- Color c ;
4976
-
4977
- c = BackColor ;
4978
4990
#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.
4991
+ Color c = BackColor ;
4992
+
4979
4993
if ( c != SystemColors . Window || Application . IsDarkModeEnabled )
4980
4994
{
4981
4995
PInvokeCore . SendMessage ( this , PInvoke . LVM_SETBKCOLOR , ( WPARAM ) 0 , ( LPARAM ) c ) ;
@@ -4989,6 +5003,7 @@ protected void RealizeProperties()
4989
5003
}
4990
5004
#pragma warning restore WFO5001
4991
5005
5006
+ // Realize state information
4992
5007
if ( _imageListLarge is not null )
4993
5008
{
4994
5009
PInvokeCore . SendMessage ( this , PInvoke . LVM_SETIMAGELIST , ( WPARAM ) PInvoke . LVSIL_NORMAL , ( LPARAM ) _imageListLarge . Handle ) ;
@@ -6441,16 +6456,16 @@ private int UpdateGroupCollapse(MessageId clickType)
6441
6456
6442
6457
internal void RecreateHandleInternal ( )
6443
6458
{
6444
- // For some reason, if CheckBoxes are set to true and the list view has a state imageList, then the native
6445
- // listView destroys the state imageList.
6446
- // (Yes, it does exactly that even though our wrapper sets LVS_SHAREIMAGELISTS on the native listView.)
6459
+ // For some reason, if CheckBoxes are set to true and the list view has a state imageList,
6460
+ // then the native listView destroys the state imageList.
6461
+ // (Yes, it does exactly that even though our wrapper sets LVS_SHAREIMAGELISTS
6462
+ // on the native listView.)
6447
6463
if ( IsHandleCreated && StateImageList is not null )
6448
6464
{
6449
6465
PInvokeCore . SendMessage ( this , PInvoke . LVM_SETIMAGELIST , ( WPARAM ) PInvoke . LVSIL_STATE ) ;
6450
6466
}
6451
6467
6452
6468
RecreateHandle ( ) ;
6453
- ApplyDarkModeOnDemand ( ) ;
6454
6469
}
6455
6470
6456
6471
private unsafe void WmReflectNotify ( ref Message m )
0 commit comments