From 762a7713d8e229824d5a83d704d256dd1813387a Mon Sep 17 00:00:00 2001 From: CreateAndInject Date: Mon, 9 Dec 2024 03:43:13 +0800 Subject: [PATCH 1/2] Fix high Dpi --- ILSpy/Controls/CustomDialog.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ILSpy/Controls/CustomDialog.cs b/ILSpy/Controls/CustomDialog.cs index 994bedffc..b3f33dd51 100644 --- a/ILSpy/Controls/CustomDialog.cs +++ b/ILSpy/Controls/CustomDialog.cs @@ -53,7 +53,7 @@ namespace ICSharpCode.ILSpy.Controls { Rectangle screen = Screen.PrimaryScreen.WorkingArea; SizeF size = g.MeasureString(message, label.Font, screen.Width - 20); - Size clientSize = size.ToSize(); + Size clientSize = new Size((int)(size.Width * 96 / g.DpiX), (int)(size.Height * 96 / g.DpiY)); Button[] buttons = new Button[buttonLabels.Length]; int[] positions = new int[buttonLabels.Length]; int pos = 0; @@ -66,7 +66,7 @@ namespace ICSharpCode.ILSpy.Controls newButton.Text = buttonLabel; newButton.Click += new EventHandler(ButtonClick); SizeF buttonSize = g.MeasureString(buttonLabel, newButton.Font); - newButton.Width = Math.Max(newButton.Width, ((int)Math.Ceiling(buttonSize.Width / 8.0) + 1) * 8); + newButton.Width = Math.Max(newButton.Width, ((int)Math.Ceiling(buttonSize.Width * 96 / g.DpiX / 8.0) + 1) * 8); positions[i] = pos; buttons[i] = newButton; pos += newButton.Width + 4; From 19d4f0136563c6a53205b9b47b6435a3447ff1a4 Mon Sep 17 00:00:00 2001 From: CreateAndInject Date: Sun, 15 Dec 2024 08:28:22 +0800 Subject: [PATCH 2/2] Use TextRenderer.MeasureText instead of Graphics.MeasureString to get exact result --- ILSpy/Controls/CustomDialog.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ILSpy/Controls/CustomDialog.cs b/ILSpy/Controls/CustomDialog.cs index b3f33dd51..d51f69c0f 100644 --- a/ILSpy/Controls/CustomDialog.cs +++ b/ILSpy/Controls/CustomDialog.cs @@ -51,9 +51,8 @@ namespace ICSharpCode.ILSpy.Controls using (Graphics g = this.CreateGraphics()) { - Rectangle screen = Screen.PrimaryScreen.WorkingArea; - SizeF size = g.MeasureString(message, label.Font, screen.Width - 20); - Size clientSize = new Size((int)(size.Width * 96 / g.DpiX), (int)(size.Height * 96 / g.DpiY)); + SizeF size = TextRenderer.MeasureText(message, label.Font); + Size clientSize = new Size((int)(size.Width * 96 / g.DpiX) + DockPadding.Left + DockPadding.Right, (int)(size.Height * 96 / g.DpiY) + DockPadding.Top + DockPadding.Bottom); Button[] buttons = new Button[buttonLabels.Length]; int[] positions = new int[buttonLabels.Length]; int pos = 0; @@ -65,7 +64,7 @@ namespace ICSharpCode.ILSpy.Controls string buttonLabel = buttonLabels[i]; newButton.Text = buttonLabel; newButton.Click += new EventHandler(ButtonClick); - SizeF buttonSize = g.MeasureString(buttonLabel, newButton.Font); + SizeF buttonSize = TextRenderer.MeasureText(buttonLabel, newButton.Font); newButton.Width = Math.Max(newButton.Width, ((int)Math.Ceiling(buttonSize.Width * 96 / g.DpiX / 8.0) + 1) * 8); positions[i] = pos; buttons[i] = newButton;