diff --git a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs index b0b09d5dec3..87dce5de0ff 100644 --- a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs +++ b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs @@ -28,6 +28,7 @@ internal static partial class LocalAppContextSwitches internal const string TreeNodeCollectionAddRangeRespectsSortOrderSwitchName = "System.Windows.Forms.TreeNodeCollectionAddRangeRespectsSortOrder"; internal const string ClipboardDragDropEnableUnsafeBinaryFormatterSerializationSwitchName = "Windows.ClipboardDragDrop.EnableUnsafeBinaryFormatterSerialization"; internal const string ClipboardDragDropEnableNrbfSerializationSwitchName = "Windows.ClipboardDragDrop.EnableNrbfSerialization"; + internal const string MoveTreeViewTextLocationOnePixelSwitchName = "System.Windows.Forms.TreeView.MoveTreeViewTextLocationOnePixel"; private static int s_scaleTopLevelFormMinMaxSizeForDpi; private static int s_anchorLayoutV2; @@ -41,6 +42,7 @@ internal static partial class LocalAppContextSwitches private static int s_treeNodeCollectionAddRangeRespectsSortOrder; private static int s_clipboardDragDropEnableUnsafeBinaryFormatterSerialization; private static int s_clipboardDragDropEnableNrbfSerialization; + private static int s_moveTreeViewTextLocationOnePixel; private static FrameworkName? s_targetFrameworkName; @@ -260,4 +262,14 @@ public static bool ClipboardDragDropEnableNrbfSerialization [MethodImpl(MethodImplOptions.AggressiveInlining)] get => GetCachedSwitchValue(ClipboardDragDropEnableNrbfSerializationSwitchName, ref s_clipboardDragDropEnableNrbfSerialization); } + + /// + /// Indicates whether to move the text position of a TreeView node one pixel + /// to the right relative to the upper-left corner of the TreeView control. + /// + public static bool MoveTreeViewTextLocationOnePixel + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => GetCachedSwitchValue(MoveTreeViewTextLocationOnePixelSwitchName, ref s_moveTreeViewTextLocationOnePixel); + } } diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs index 16ce5ca76fd..beecea28ebc 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/TreeView/TreeView.cs @@ -7,6 +7,7 @@ using System.Drawing.Design; using System.Runtime.InteropServices; using System.Windows.Forms.Layout; +using System.Windows.Forms.Primitives; using System.Windows.Forms.VisualStyles; using Windows.Win32.System.Variant; using Windows.Win32.UI.Accessibility; @@ -2830,7 +2831,7 @@ private unsafe void CustomDraw(ref Message m) { Rectangle bounds = node.Bounds; Size textSize = TextRenderer.MeasureText(node.Text, node.TreeView!.Font); - Point textLoc = new(bounds.X - 1, bounds.Y); // required to center the text + Point textLoc = new(LocalAppContextSwitches.MoveTreeViewTextLocationOnePixel ? bounds.X : bounds.X - 1, bounds.Y); bounds = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height)); DrawTreeNodeEventArgs e = new(g, node, bounds, (TreeNodeStates)(nmtvcd->nmcd.uItemState));