Skip to content

Commit 6c3e9e5

Browse files
authored
Removed usage of the Graphics.MeasureString and used TextRenderer.MeasureText instead (JohannBlais#61)
1 parent b660ef8 commit 6c3e9e5

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

InfoBox/Form/InformationBoxForm.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ internal partial class InformationBoxForm : Form
7474
/// </summary>
7575
private readonly string doNotShowAgainText;
7676

77-
/// <summary>
78-
/// Contains the graphics used to measure the strings
79-
/// </summary>
80-
private readonly Graphics measureGraphics;
81-
8277
/// <summary>
8378
/// Contains a reference to the active form
8479
/// </summary>
@@ -288,8 +283,6 @@ internal InformationBoxForm(string text,
288283
InformationBoxSound sound = InformationBoxSound.Default)
289284
{
290285
this.InitializeComponent();
291-
this.measureGraphics = CreateGraphics();
292-
this.measureGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
293286

294287
// Apply default font for message boxes
295288
this.Font = SystemFonts.MessageBoxFont;
@@ -1007,15 +1000,15 @@ private void SetLayout()
10071000
#region Width
10081001

10091002
// Caption width including button
1010-
int captionWidth = Convert.ToInt32(this.measureGraphics.MeasureString(Text, SystemFonts.CaptionFont).Width) + 30;
1003+
int captionWidth = TextRenderer.MeasureText(Text, SystemFonts.CaptionFont, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix).Width + 30;
10111004
if (this.titleStyle != InformationBoxTitleIconStyle.None)
10121005
{
10131006
captionWidth += BorderPadding * 2;
10141007
}
10151008

10161009
// "Do not show this dialog again" width
10171010
int checkBoxWidth = ((this.checkBox & InformationBoxCheckBox.Show) == InformationBoxCheckBox.Show)
1018-
? (int)this.measureGraphics.MeasureString(this.chbDoNotShow.Text, this.chbDoNotShow.Font).Width + BorderPadding * 4
1011+
? TextRenderer.MeasureText(this.chbDoNotShow.Text, this.chbDoNotShow.Font, Size.Empty, TextFormatFlags.NoPadding).Width + BorderPadding * 4
10191012
: 0;
10201013

10211014
// Width of the text and icon.
@@ -1273,7 +1266,7 @@ private void SetText()
12731266
if (this.autoSizeMode == InformationBoxAutoSizeMode.None)
12741267
{
12751268
this.messageText.WordWrap = true;
1276-
this.messageText.Size = (this.measureGraphics.MeasureString(this.messageText.Text, this.messageText.Font, screenWidth / 2) + new SizeF(1, 0)).ToSize();
1269+
this.messageText.Size = TextRenderer.MeasureText(this.messageText.Text, this.messageText.Font, new Size(screenWidth / 2, 0), TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak);
12771270
}
12781271
else
12791272
{
@@ -1290,7 +1283,7 @@ private void SetText()
12901283

12911284
foreach (Match sentence in sentences)
12921285
{
1293-
int sentenceLength = (int)this.measureGraphics.MeasureString(sentence.Value, this.messageText.Font).Width;
1286+
int sentenceLength = TextRenderer.MeasureText(sentence.Value, this.messageText.Font, Size.Empty, TextFormatFlags.TextBoxControl | TextFormatFlags.NoPadding).Width;
12941287
if (currentWidth != 0 && (sentenceLength + currentWidth) > (screenWidth - 50))
12951288
{
12961289
formattedText.Append(Environment.NewLine);
@@ -1315,7 +1308,7 @@ private void SetText()
13151308

13161309
this.messageText.Text = this.internalText.ToString();
13171310

1318-
this.messageText.Size = (this.measureGraphics.MeasureString(this.messageText.Text, this.messageText.Font) + new SizeF(1, 0)).ToSize();
1311+
this.messageText.Size = TextRenderer.MeasureText(this.messageText.Text, this.messageText.Font, Size.Empty, TextFormatFlags.TextBoxControl);
13191312
}
13201313
}
13211314

@@ -1426,7 +1419,7 @@ private void SetButtonsSize()
14261419
// Measures the width of each button
14271420
foreach (Control ctrl in this.pnlButtons.Controls)
14281421
{
1429-
maxSize = Math.Max(Convert.ToInt32(this.measureGraphics.MeasureString(ctrl.Text, ctrl.Font).Width + 40), maxSize);
1422+
maxSize = Math.Max(TextRenderer.MeasureText(ctrl.Text, ctrl.Font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix).Width + 40, maxSize);
14301423
}
14311424

14321425
foreach (Control ctrl in this.pnlButtons.Controls)

InfoBox/InfoBox.nuspec

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
33
<metadata>
44
<id>InformationBox</id>
5-
<version>1.0.0</version>
5+
<version>1.4.0</version>
66
<title>InformationBox</title>
77
<authors>Johann Blais</authors>
88
<owners>Johann Blais</owners>
@@ -15,10 +15,11 @@
1515
InformationBox is the simplest and easiest way to create personalized MessageBox.
1616

1717
Stop wasting time developing your own custom MessageBox, everything you need is already available. Just customize your MessageBox using the visual designer and the code is automatically generated !</description>
18-
<summary>InformationBox (.NET 4.0+) is a flexible alternative to the default MessageBox included in the System.Windows.Forms namespace</summary>
18+
<summary>InformationBox (.NET 4.8+, dotnet core 8.0+) is a flexible alternative to the default MessageBox included in the System.Windows.Forms namespace</summary>
1919
<releaseNotes>
20-
- Removed support for .NET 6.0 (Windows)
21-
- Added support for .NET 10.0 (Windows)
20+
- Improved text measurement accuracy by migrating from Graphics.MeasureString (GDI+) to TextRenderer.MeasureText (GDI)
21+
- Removed Graphics dependency for better testability
22+
- Text sizing now matches actual TextBox rendering more accurately
2223
</releaseNotes>
2324
<copyright>Copyright 2026</copyright>
2425
<language>en-US</language>

InfoBox/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
// Version information for an assembly consists of the following four values:
4343
//
4444
// Major Version
45-
// Minor Version
45+
// Minor Version
4646
// Build Number
4747
// Revision
4848
//
49-
// You can specify all the values or you can default the Revision and Build Numbers
49+
// You can specify all the values or you can default the Revision and Build Numbers
5050
// by using the '*' as shown below:
51-
[assembly: AssemblyVersion("1.3.0.0")]
52-
[assembly: AssemblyFileVersion("1.3.0.0")]
51+
[assembly: AssemblyVersion("1.4.0.0")]
52+
[assembly: AssemblyFileVersion("1.4.0.0")]

0 commit comments

Comments
 (0)