VBGood网站全文搜索 Google

首页 - 经验之谈 - 测量文本长度
发表评论(0)作者:Nenad Cus Babic, 平台:VB6.0+Win98, 阅读:11220, 日期:2000-10-19
Measuring a text extent

It注释:s very simple to determine the extent of a string in VB. You can do so with WinAPI functions, but there注释:s an easier way: Use the AutoSize property of a Label component. First, insert a label on a form (labMeasure) and set its AutoSize property to True and Visible property to False. Then write this simple routine:

Private Function TextExtent(txt as String) as Integer
   labMeasure.Caption = txt
   TextExtent = labMeasure.Width
End Function When you want to find out the extent of some text, simply call this function with the string as a parameter.

In my case it turned out that the measure was too short. I just added some blanks to the string. For example:

Private Function TextExtent(txt As String) As Integer
   labMeasure.Caption = " " & txt
   TextExtent = labMeasure.Width
End Function