如何在 vb.net 中使用 TextRenderer DrawText 换行长字

How to wrap long word with TextRenderer DrawText in vb.net

如何使用 vb.net 中的 TextRenderer DrawText() 在表格 中使用断字 绘制长字?我认为这是一个简单的问题,我尝试了所有 TextFormatFlags 组合,但我找不到任何解决方案。 谁能帮忙? 这是一个示例:

sText = "C:\Users\abiga\OneDrive\Works\AdreNDSzinkron\bin\Debug\AdreService.exe"
TextRenderer.DrawText(e.Graphics, sText, Font, New Rectangle(0, 0, Me.Width, Me.Height),
                      Me.Color,TextFormatFlags.<what is the correct flag?>) 

我需要这个(没有剪裁):

C:\Users\abiga\OneDrive\Works\Adr
eNDSzinkron\bin\Debug\AdreService
.exe

错误的解决方案:

C:\Users\abiga\OneDrive\Works\Adr
C:\Users\abiga\OneDrive\Works\...
C:\Users\ab...bug\AdreService.exe

感谢您的帮助!

首先尝试 TextFormatFlags.WordBreak or TextFormatFlags.TextBoxControl 作为你的旗帜。

文档说:

WordBreak: Breaks the text at the end of a word

TextBoxControl: Specifies the text should be formatted for display on a TextBox control

组合这些标志应该会产生预期的结果。

如果这不起作用,请尝试使用 Graphics.DrawString

e.Graphics.DrawString(sText, Font, Me.Color, New RectangleF(0, 0, Me.Width, Me.Height))

只是添加一些可用于评估 TextRenderer's DrawText() method and the Graphics's DrawString() 方法之间差异的实现细节。

点击窗体,两种方法显示了它们在测量和渲染文本方面的差异。

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"
}

在表单的 Click() 事件中,绘制 sText 条线,使用 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

在表单的 Paint() 事件中,绘制 sText 条线,使用 .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