编成函数,借用一个 PictureBox 'Object: FORM1、Picture1、Command1 Option Explicit Public Function GetTextWidth(TextX As String, FontX As stdole.StdFont, PictureX As _ VB.PictureBox, Optional FromScale As VBRUN.ScaleModeConstants = vbHimetric, Optional _ ToScale As VBRUN.ScaleModeConstants = vbPixels) As Double '保留原值,Clone 该字体为今后恢复,也可没必要 Dim Temp As New stdole.StdFont Temp.Bold = FontX.Bold Temp.Charset = FontX.Italic Temp.Name = FontX.Name Temp.Size = FontX.Size Temp.Strikethrough = FontX.Strikethrough Temp.Underline = FontX.Underline Temp.Weight = FontX.Weight Set PictureX.Font = FontX GetTextWidth = PictureX.ScaleX(PictureX.TextWidth(TextX), FromScale, ToScale) '恢复 Set PictureX.Font = Temp End Function '测试 Private Sub Command1_Click() 'VBA.MsgBox Me.ScaleX(Me.TextWidth("aa"), vbHimetric, vbPixels) 'VBA.MsgBox GetTextWidthPixels("aa", Me.Font, Picture1) Dim x As New stdole.StdFont x.Size = 12 VBA.MsgBox GetTextWidth("a阿", x, Picture1) End Sub
'One Test Program 'Add Two Label On the Form (刚写的基本方法就是YHeng(我来也!)说的那样) Private Sub Form_Load() Dim i As Integer Dim h As Integer, w As Integer Dim n, s
Label2.Caption = "" n = Me.FontName s = Me.FontSize Me.FontName = Label1.FontName Me.FontSize = Label1.FontSize For i = 1 To Len(Label1.Caption) c = Mid(Label1.Caption, i, 1) h = Me.TextHeight(c) w = Me.TextWidth(c) Label2.Caption = Label2.Caption & "字符" & c & _ " 的宽和高为:" & Format(w) & "、" & Format(h) & _ Chr(10) Next i Me.FontName = n Me.FontSize = s End Sub
然后用上面的。
'Object: FORM1、Picture1、Command1
Option Explicit
Public Function GetTextWidth(TextX As String, FontX As stdole.StdFont, PictureX As _
VB.PictureBox, Optional FromScale As VBRUN.ScaleModeConstants = vbHimetric, Optional _
ToScale As VBRUN.ScaleModeConstants = vbPixels) As Double
'保留原值,Clone 该字体为今后恢复,也可没必要
Dim Temp As New stdole.StdFont
Temp.Bold = FontX.Bold
Temp.Charset = FontX.Italic
Temp.Name = FontX.Name
Temp.Size = FontX.Size
Temp.Strikethrough = FontX.Strikethrough
Temp.Underline = FontX.Underline
Temp.Weight = FontX.Weight
Set PictureX.Font = FontX
GetTextWidth = PictureX.ScaleX(PictureX.TextWidth(TextX), FromScale, ToScale)
'恢复
Set PictureX.Font = Temp
End Function
'测试
Private Sub Command1_Click()
'VBA.MsgBox Me.ScaleX(Me.TextWidth("aa"), vbHimetric, vbPixels)
'VBA.MsgBox GetTextWidthPixels("aa", Me.Font, Picture1)
Dim x As New stdole.StdFont
x.Size = 12
VBA.MsgBox GetTextWidth("a阿", x, Picture1)
End Sub
'Add Two Label On the Form
(刚写的基本方法就是YHeng(我来也!)说的那样)
Private Sub Form_Load()
Dim i As Integer
Dim h As Integer, w As Integer
Dim n, s
Label2.Caption = ""
n = Me.FontName
s = Me.FontSize
Me.FontName = Label1.FontName
Me.FontSize = Label1.FontSize
For i = 1 To Len(Label1.Caption)
c = Mid(Label1.Caption, i, 1)
h = Me.TextHeight(c)
w = Me.TextWidth(c)
Label2.Caption = Label2.Caption & "字符" & c & _
" 的宽和高为:" & Format(w) & "、" & Format(h) & _
Chr(10)
Next i
Me.FontName = n
Me.FontSize = s
End Sub
Dim c as String
TextWidth 方法
用以返回按 Form,PictureBox 或 Printer 的当前字体被打印的文本字符串的宽度。不支持命名参数。语法object.TextWidth(string)TextWidth 方法的语法包含下列部分:部分 描述
object 可选的。一个对象表达式,其值为“应用于”列表中的一个对象。如果省略 object,则带有焦点的 Form 对象缺省为 object。
String 必需的。一个字符串表达式,它用以计算确定其宽度的字符串。必须用括号包括该字符串表达式。
说明字符串宽度是以对 object 有效的 ScaleMode 属性设置或通过 Scale 方法的坐标系统来表示的。使用 TextWidth 可以确定文本显示需要的水平空间宽度。如果 string 含有嵌入的回车返回符,TextWidth 将返回最长行的宽度。