Just to add some implementation details that can be used to evaluate the differences between TextRenderer's DrawText() method and the Graphics's DrawString() method.
Clicking on the Form, the two methods show their differences in measuring and rendering the text.
Dim sText As String() = New String() {
"C:\FirstLevelDir\FirstSubDir\AnotherDir\ADeepLevelDir\LostDeepDir\SomeFile.exe",
"C:\FirstLevelDir\AnotherFirstSubDir\AnotherGreatDir\AwsomeDeepLevelDir\LostDeepDir\Some.exe",
"C:\FirstLevelDir\SomeFirstSubDir\SomeOtherDir\AnotherDeepLevelDir\VeryLostDeepDir\FinalBuriedDir\SomeFile.exe"
}
In the Form's Click()
event, draw sText
lines, measuring their Width and Height using TextRenderer.MeasureText() and print them with TextRenderer.DrawText()
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim relPositionY As Integer = 0
Dim lineSpacing As Single = 0.5
Dim paragraphSpacing As Integer = CInt(Font.Height * lineSpacing)
Dim flags As TextFormatFlags = TextFormatFlags.Top Or
TextFormatFlags.WordBreak Or
TextFormatFlags.TextBoxControl
Using g As Graphics = CreateGraphics()
For x = 0 To sText.Length - 1
Dim textSize As Size = TextRenderer.MeasureText(
g, sText(x), Font,
New Size(ClientSize.Width, ClientSize.Height), flags
)
TextRenderer.DrawText(g, sText(x), Font,
New Rectangle(0, relPositionY, textSize.Width, textSize.Height),
ForeColor, flags)
relPositionY += textSize.Height + paragraphSpacing
Next
End Using
End Sub
In the Form's Paint()
event, draw sText
lines, measuring their Width and Height using .Graphics.MeasureString() and print them with .Graphics.DrawString()
Note that the text boxed size in TextRenderer is relative to the
Form.ClientSize, while in Graphics is relative to the Form's full
Width.
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim relPositionY As Single = 0
Dim lineSpacing As Single = 0.5
Dim paragraphSpacing As Single = CSng(Font.Height) * lineSpacing
Dim flags As StringFormatFlags = StringFormatFlags.LineLimit Or
StringFormatFlags.FitBlackBox
Using format As StringFormat = New StringFormat(flags)
For x = 0 To sText.Length - 1
Dim textSize As SizeF = e.Graphics.MeasureString(sText(x), Font,
New SizeF(CSng(Width), CSng(Height)), format
)
e.Graphics.DrawString(sText(x), Font, Brushes.Black,
New RectangleF(0, relPositionY, textSize.Width, textSize.Height))
relPositionY += textSize.Height + paragraphSpacing
Next
End Using
End Sub