From 15ce890c64a7d829f4c0772aca8ca16c97cf7c88 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Fri, 29 May 2026 13:58:04 +0800 Subject: [PATCH 1/2] Fix cursor editor icon width calculation for proper DPI scaling --- .../System/Drawing/Design/CursorEditor.CursorUI.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs b/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs index 43335501da0..8a53a663b20 100644 --- a/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs +++ b/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs @@ -17,6 +17,7 @@ private class CursorUI : ListBox private IWindowsFormsEditorService? _editorService; private readonly TypeConverter _cursorConverter; private readonly Dictionary<(Cursor cursor, int dpi), int> _cursorWidthCache = new(); + private readonly int _cursorWidth; public CursorUI() { @@ -39,6 +40,10 @@ public CursorUI() Items.Add(obj); } } + + using Icon defaultIcon = Icon.FromHandle(Cursors.Default.Handle); + using Icon scaledDefaultIcon = ScaleHelper.ScaleSmallIconToDpi(defaultIcon, DeviceDpi); + _cursorWidth = scaledDefaultIcon.Size.Width; } public object? Value { get; private set; } @@ -70,11 +75,11 @@ protected override void OnDrawItem(DrawItemEventArgs e) int cursorWidth = GetCursorWidthForDpi(cursor, DeviceDpi); e.DrawBackground(); - e.Graphics.FillRectangle(SystemBrushes.Control, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, cursorWidth, e.Bounds.Height - 4)); - e.Graphics.DrawRectangle(SystemPens.WindowText, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, cursorWidth - 1, e.Bounds.Height - 4 - 1)); + e.Graphics.FillRectangle(SystemBrushes.Control, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, _cursorWidth, e.Bounds.Height - 4)); + e.Graphics.DrawRectangle(SystemPens.WindowText, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, _cursorWidth - 1, e.Bounds.Height - 4 - 1)); - cursor.DrawStretched(e.Graphics, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, cursorWidth, e.Bounds.Height - 4)); - e.Graphics.DrawString(text, font, brushText, e.Bounds.X + cursorWidth + 4, e.Bounds.Y + (e.Bounds.Height - font.Height) / 2); + cursor.DrawStretched(e.Graphics, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, _cursorWidth, e.Bounds.Height - 4)); + e.Graphics.DrawString(text, font, brushText, e.Bounds.X + _cursorWidth + 4, e.Bounds.Y + (e.Bounds.Height - font.Height) / 2); } } From 608216e4a1fe5cef8cb6b60528cbaad46c5e31b2 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 15 Jun 2026 17:00:03 +0800 Subject: [PATCH 2/2] Handle feedback --- .../Drawing/Design/CursorEditor.CursorUI.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs b/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs index 8a53a663b20..72a39e9f665 100644 --- a/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs +++ b/src/System.Windows.Forms.Design/src/System/Drawing/Design/CursorEditor.CursorUI.cs @@ -17,7 +17,7 @@ private class CursorUI : ListBox private IWindowsFormsEditorService? _editorService; private readonly TypeConverter _cursorConverter; private readonly Dictionary<(Cursor cursor, int dpi), int> _cursorWidthCache = new(); - private readonly int _cursorWidth; + private int _cursorWidth; public CursorUI() { @@ -41,9 +41,7 @@ public CursorUI() } } - using Icon defaultIcon = Icon.FromHandle(Cursors.Default.Handle); - using Icon scaledDefaultIcon = ScaleHelper.ScaleSmallIconToDpi(defaultIcon, DeviceDpi); - _cursorWidth = scaledDefaultIcon.Size.Width; + _cursorWidth = GetCursorWidthForDpi(Cursors.Default, DeviceDpi); } public object? Value { get; private set; } @@ -72,7 +70,6 @@ protected override void OnDrawItem(DrawItemEventArgs e) string? text = _cursorConverter.ConvertToString(cursor); Font font = e.Font!; using var brushText = e.ForeColor.GetCachedSolidBrushScope(); - int cursorWidth = GetCursorWidthForDpi(cursor, DeviceDpi); e.DrawBackground(); e.Graphics.FillRectangle(SystemBrushes.Control, new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, _cursorWidth, e.Bounds.Height - 4)); @@ -129,5 +126,14 @@ public void Start(IWindowsFormsEditorService editorService, object? value) } } } + + protected override void RescaleConstantsForDpi(int deviceDpiOld, int deviceDpiNew) + { + base.RescaleConstantsForDpi(deviceDpiOld, deviceDpiNew); + + // Recalculate the representative width using the new DPI; all rows continue to share it to avoid the layout issues seen in #14167. + _cursorWidth = GetCursorWidthForDpi(Cursors.Default, deviceDpiNew); + Invalidate(); + } } }