如何使用中的TextRenderer DrawText()在表单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
谢谢你的帮助!
发布于 2018-01-10 16:13:06
只是为了增加一些实现细节,这些细节可以用来评估TextRenderer的DrawText()方法和图形的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()测量它们的宽度和高度,然后用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()测量它们的宽度和高度,然后用.Graphics.DrawString()打印它们
请注意,TextRenderer中的文本框大小相对于Form.ClientSize,而图形中的文本框大小相对于窗体的全宽度。
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
https://stackoverflow.com/questions/48187599
复制相似问题