Private Sub Command1_Click() Print Me.TextWidth("abcdefg") \ Screen.TwipsPerPixelX Print Me.TextHeight("abcdefg") \ Screen.TwipsPerPixelY End Sub
Private Function GetTextWidth(ByRef sText As String) As Long GetTextWidth = TextWidth(sText) End Function Private Function GetTextHeight(ByRef sText As String) As Long GetTextHeight = TextHeight(sText) End Function Private Sub Command1_Click() MsgBox GetTextWidth(Text1) MsgBox GetTextHeight(Text1) End Sub
Me.ScaleMode = vbPixels
Label1.AutoSize = True
Label1.Caption = "abcdef"MsgBox Label1.Height & vbTab & Label1.Width
End Sub这样就可以了.
使用TextWidth 方法 用以返回按 Form,PictureBox 或 Printer 的当前字体被打印的文本字符串的宽度。不支持命名参数。TextHeight 方法
用以返回按 Form、PictureBox 或 Printer 的当前字体将被打印的文本字符串的高度。不支持命名参数。 TextHeight 方法被用来在一个窗体上使正文行垂直居中。要检验此示例,可将本例代码粘贴到一个窗体的声明部分,然后按 F5 键并单击该窗体。Private Sub Form_Click ()
Dim HalfWidth, HalfHeight, Msg ' 声明变量。
AutoRedraw = -1 ' 打开AutoRedraw。
BackColor = QBColor(4) ' 设置背景颜色。
ForeColor = QBColor(15) ' 设置前景颜色。
Msg = "Visual Basic" ' 创建信息。
FontSize = 48 ' 设置字体大小。
HalfWidth = TextWidth(Msg) / 2 ' 计算半宽。
HalfHeight = TextHeight(Msg) / 2 ' 计算半高。
CurrentX = ScaleWidth / 2 - HalfWidth ' 设置X。
CurrentY = ScaleHeight / 2 - HalfHeight ' 设置Y。
Print Msg ' 打印信息。
End Sub
Print Me.TextWidth("abcdefg") \ Screen.TwipsPerPixelX
Print Me.TextHeight("abcdefg") \ Screen.TwipsPerPixelY
End Sub
GetTextWidth = TextWidth(sText)
End Function
Private Function GetTextHeight(ByRef sText As String) As Long
GetTextHeight = TextHeight(sText)
End Function
Private Sub Command1_Click()
MsgBox GetTextWidth(Text1)
MsgBox GetTextHeight(Text1)
End Sub