测量文本长度

发表于:2007-06-21来源:作者:点击数: 标签:
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

   


  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 

原文转自:http://www.ltesting.net