Dim mark As Single
Dim g As Graphics = e.Graphics
Dim str As String
Dim i As Integer
Dim temp() As String
Dim myfix As Drawing2D.Matrix = New Drawing2D.Matrix
Dim size As SizeF
Dim length As Single
str = ttbBuildName.Text.TrimStart("、") '所有文本
size = g.MeasureString(str, myfont3)
length = size.Width '所有文本在myfont3状态下的长度(像素)
If length > 4082 Then '4082:供打印的文本框的长度(像素)
mark = 4082 / length
Else
mark = 1
End If
myfix.Scale(mark, 1) '按比例压缩宽度
g.Transform = myfix
Dim j As Integer = 0
Dim from As Integer = 0
Dim strtemp As String = " "
'第一行
Do While (g.MeasureString(strtemp, myfont3).Width < 302 * (1 / mark) And 302 * (1 / mark) - g.MeasureString(strtemp, myfont3).Width >= g.MeasureString(str.Substring(from + j, 1), myfont3).Width)
j = j + 1
strtemp = str.Substring(from, j)
If (j + from) = str.Length Then Exit Do
Loop
from = from + j
g.DrawString(strtemp, myfont3, Brushes.Black, New RectangleF((x1 + 679) * (1 / mark), y1 + 80, length, 30), formatl)
j = 0
'后9行
If (j + from) <= str.Length - 1 Then
For i = 1 To 9
strtemp = " "
Do While (g.MeasureString(strtemp, myfont3).Width < 420 * (1 / mark) And 420 * (1 / mark) - g.MeasureString(strtemp, myfont3).Width >= g.MeasureString(str.Substring(from + j, 1), myfont3).Width)
j = j + 1
strtemp = str.Substring(from, j)
If (j + from) = str.Length Then Exit Do
Loop
from = from + j
g.DrawString(strtemp, myfont3, Brushes.Black, New RectangleF((x1 + 561) * (1 / mark), y1 + 130 + 51 * (i - 1), length, 30), formatl)
j = 0
If (j + from) = str.Length Then Exit For
Next
End If
myfix.Scale(1 / mark, 1) '恢复原始比例(1:1)
g.Transform = myfix 
本文介绍了一种用于调整文本布局和实现等比例缩放的算法,确保长文本能在限定区域内完整显示,通过精确计算每行字符数并利用绘图API进行绘制。
3699

被折叠的 条评论
为什么被折叠?



