基于固定宽度的字符串高度度量



我想得到宽度为280的矩形中字符串的高度。有时String_2_Print包含一个vbctrl。

Dim textfont = New Font("Arial", 12, FontStyle.Bold)
Dim Txt_Width As New SizeF
Dim Printer_Y as integer = 0
Txt_Width = e.Graphics.MeasureString("1", textfont)
Dim Text_Height as integer '= the height of the string to print
e.Graphics.DrawString(UCase(String_2_Print), textfont, Brushes.Black, New Rectangle(0, Printer_Y, 280, Text_Height), StringFormat.GenericTypographic)
Printer_Y +=  Txt_Width.Height + Text_Height



我找到了。你必须制作一个与你想要的矩形宽度相同的动态标签,然后你才能获得label.height。

Dim textfont = New Font("Arial", 12, FontStyle.Bold)
Dim Txt_Width As New SizeF
Dim Printer_Y as integer = 0
Txt_Width = e.Graphics.MeasureString("1", textfont)                
Dim myLabel As New Label
myLabel.Font = New Font("Arial", textfont , FontStyle.Italic)
myLabel.AutoSize = True
myLabel.MaximumSize = New Size(285, 0)
myLabel.Location = New Point(0, 0)
myLabel.Visible = False
myLabel.Text = The_Msg
Panel1.Controls.Add(myLabel)
Dim myMsg_Height As Integer = myLabel.Height + 1
Panel1.Controls.Clear()
e.Graphics.DrawString(The_Msg, textfont, Brushes.Black, New Rectangle(5, Printer_Y, 280, myMsg_Height), StringFormat.GenericTypographic)
Printer_Y += myMsg_Height + 15

最新更新