@@ -2735,6 +2735,13 @@ private unsafe void CustomDraw(ref Message m)
2735
2735
// as background color.
2736
2736
if ( _drawMode == TreeViewDrawMode . OwnerDrawText )
2737
2737
{
2738
+ if ( node is { IsEditing : true } )
2739
+ {
2740
+ nmtvcd ->clrText = ColorTranslator . ToWin32 ( BackColor ) ;
2741
+ nmtvcd ->clrTextBk = ColorTranslator . ToWin32 ( BackColor ) ;
2742
+ goto default ;
2743
+ }
2744
+
2738
2745
nmtvcd ->clrText = nmtvcd ->clrTextBk ;
2739
2746
m . ResultInternal = ( LRESULT ) ( nint ) ( PInvoke . CDRF_NEWFONT | PInvoke . CDRF_NOTIFYPOSTPAINT ) ;
2740
2747
return ;
@@ -2793,6 +2800,11 @@ private unsafe void CustomDraw(ref Message m)
2793
2800
nmtvcd ->clrTextBk = ColorTranslator . ToWin32 ( riBack ) ;
2794
2801
}
2795
2802
2803
+ if ( node is { IsEditing : true } )
2804
+ {
2805
+ nmtvcd ->clrTextBk = ColorTranslator . ToWin32 ( BackColor ) ;
2806
+ }
2807
+
2796
2808
if ( renderinfo is not null && renderinfo . Font is not null )
2797
2809
{
2798
2810
// Mess with the DC directly...
@@ -2827,8 +2839,30 @@ private unsafe void CustomDraw(ref Message m)
2827
2839
{
2828
2840
Rectangle bounds = node . Bounds ;
2829
2841
Size textSize = TextRenderer . MeasureText ( node . Text , node . TreeView ! . Font ) ;
2830
- Point textLoc = new ( LocalAppContextSwitches . MoveTreeViewTextLocationOnePixel ? bounds . X : bounds . X - 1 , bounds . Y ) ;
2831
- bounds = new Rectangle ( textLoc , new Size ( textSize . Width , bounds . Height ) ) ;
2842
+ Point textLocation = new ( LocalAppContextSwitches . MoveTreeViewTextLocationOnePixel ? bounds . X : bounds . X - 1 , bounds . Y ) ;
2843
+ bounds = new Rectangle ( textLocation , new Size ( textSize . Width , bounds . Height ) ) ;
2844
+
2845
+ Rectangle fillRectangle = new Rectangle ( textLocation , new ( textSize . Width , bounds . Height ) ) ;
2846
+ Rectangle focusRectangle = new Rectangle ( textLocation , new ( textSize . Width , bounds . Height ) ) ;
2847
+
2848
+ if ( RightToLeft == RightToLeft . Yes && RightToLeftLayout )
2849
+ {
2850
+ // Reverse the X-axis drawing coordinates of the rectangle.
2851
+ // Offset the text four pixels to the left to ensure that the text is centered on the fill rectangle.
2852
+ int invertedX = Width - bounds . X - textSize . Width - 4 ;
2853
+
2854
+ // Subtract the scroll bar width when the scroll bar appears.
2855
+ if ( Height <= PreferredHeight )
2856
+ {
2857
+ float dpiScale = ( float ) DeviceDpi / ( float ) ScaleHelper . InitialSystemDpi ;
2858
+ invertedX -= ( int ) ( SystemInformation . VerticalScrollBarWidth * Math . Round ( dpiScale , 2 ) ) ;
2859
+ }
2860
+
2861
+ // To ensure that the right side of the fillRectangle does not
2862
+ // touch the left edge of the node prefix symbol, 1 pixel is subtracted here.
2863
+ fillRectangle = new Rectangle ( new Point ( invertedX - 1 , bounds . Y ) , new ( textSize . Width , bounds . Height ) ) ;
2864
+ focusRectangle = new Rectangle ( new Point ( invertedX , bounds . Y ) , new ( textSize . Width , bounds . Height ) ) ;
2865
+ }
2832
2866
2833
2867
DrawTreeNodeEventArgs e = new ( g , node , bounds , ( TreeNodeStates ) ( nmtvcd ->nmcd . uItemState ) ) ;
2834
2868
OnDrawNode ( e ) ;
@@ -2844,14 +2878,14 @@ private unsafe void CustomDraw(ref Message m)
2844
2878
// Draw the actual node.
2845
2879
if ( ( curState & TreeNodeStates . Selected ) == TreeNodeStates . Selected )
2846
2880
{
2847
- g . FillRectangle ( SystemBrushes . Highlight , bounds ) ;
2848
- ControlPaint . DrawFocusRectangle ( g , bounds , color , SystemColors . Highlight ) ;
2881
+ g . FillRectangle ( SystemBrushes . Highlight , fillRectangle ) ;
2882
+ ControlPaint . DrawFocusRectangle ( g , focusRectangle , color , SystemColors . Highlight ) ;
2849
2883
TextRenderer . DrawText ( g , node . Text , font , bounds , color , TextFormatFlags . Default ) ;
2850
2884
}
2851
2885
else
2852
2886
{
2853
2887
using var brush = BackColor . GetCachedSolidBrushScope ( ) ;
2854
- g . FillRectangle ( brush , bounds ) ;
2888
+ g . FillRectangle ( brush , fillRectangle ) ;
2855
2889
2856
2890
TextRenderer . DrawText ( g , node . Text , font , bounds , color , TextFormatFlags . Default ) ;
2857
2891
}
@@ -2871,6 +2905,35 @@ private unsafe void CustomDraw(ref Message m)
2871
2905
}
2872
2906
}
2873
2907
2908
+ private int PreferredHeight
2909
+ {
2910
+ get
2911
+ {
2912
+ int height = 0 ;
2913
+ foreach ( TreeNode node in Nodes )
2914
+ {
2915
+ height += GetNodeHeight ( node ) ;
2916
+ }
2917
+
2918
+ return height ;
2919
+ }
2920
+ }
2921
+
2922
+ private int GetNodeHeight ( TreeNode node )
2923
+ {
2924
+ int height = ItemHeight ;
2925
+
2926
+ if ( node . IsExpanded )
2927
+ {
2928
+ foreach ( TreeNode childNode in node . Nodes )
2929
+ {
2930
+ height += GetNodeHeight ( childNode ) ;
2931
+ }
2932
+ }
2933
+
2934
+ return height ;
2935
+ }
2936
+
2874
2937
/// <summary>
2875
2938
/// Generates colors for each item. This can be overridden to provide colors on a per state/per node
2876
2939
/// basis, rather than using the ForeColor/BackColor/NodeFont properties on TreeNode.
0 commit comments