主题:请教API GetCharWidth的用法
我想获得任意字符串的宽度(不是长度),而找到了API GetCharWidth(调查字体中一个或多个字符的宽度) 却不知道怎么用,因为在网上没有找到资料。试来试去,这样可以达到目的
Private Declare Function GetCharWidth Lib "gdi32" Alias "GetCharWidthA" (ByVal hdc As Long, ByVal wFirstChar As Long, ByVal wLastChar As Long, lpBuffer As Long) As Long
Private Function GetLineStrWidth(ByVal strs As String) As Long
Dim a As Long, fo As Long
GetCharWidth hdc, 32, 32, a
GetLineStrWidth = Len(strs) * a
For fo = 1 To Len(strs)
If Asc(Mid(strs, fo, 1)) < 0 Then GetLineStrWidth = GetLineStrWidth + a
Next
End Function
Function GetStrWidth(ByVal strs As String) As Long
Dim a() As String, fo, lng As Long
If Len(strs) = 0 Then GetStrWidth = 0: Exit Function
strs = Replace(Replace(strs, vbLf, vbCr), vbCr & vbCr, vbCr)
a = Split(strs, vbCr)
For Each fo In a
lng = GetLineStrWidth((fo))
GetStrWidth = IIf(GetStrWidth < lng, lng, GetStrWidth)
Next
End Function
Private Sub Form_Load()
Dim strs As String
Show
strs = "asdfasdf" & vbCrLf & "wefwjjcvlkjsdfsdf"
MsgBox "文本:" & vbCrLf & strs & "长" & GetStrWidth(strs) * 15
Dim a As Long, b As Long
a = GetStrWidth(strs) * 15
Print strs
Line (a, 0)-(a, 1000)
End Sub
但 我还是想知道getcharwidth的详细用法,请高手指点
Private Declare Function GetCharWidth Lib "gdi32" Alias "GetCharWidthA" (ByVal hdc As Long, ByVal wFirstChar As Long, ByVal wLastChar As Long, lpBuffer As Long) As Long
Private Function GetLineStrWidth(ByVal strs As String) As Long
Dim a As Long, fo As Long
GetCharWidth hdc, 32, 32, a
GetLineStrWidth = Len(strs) * a
For fo = 1 To Len(strs)
If Asc(Mid(strs, fo, 1)) < 0 Then GetLineStrWidth = GetLineStrWidth + a
Next
End Function
Function GetStrWidth(ByVal strs As String) As Long
Dim a() As String, fo, lng As Long
If Len(strs) = 0 Then GetStrWidth = 0: Exit Function
strs = Replace(Replace(strs, vbLf, vbCr), vbCr & vbCr, vbCr)
a = Split(strs, vbCr)
For Each fo In a
lng = GetLineStrWidth((fo))
GetStrWidth = IIf(GetStrWidth < lng, lng, GetStrWidth)
Next
End Function
Private Sub Form_Load()
Dim strs As String
Show
strs = "asdfasdf" & vbCrLf & "wefwjjcvlkjsdfsdf"
MsgBox "文本:" & vbCrLf & strs & "长" & GetStrWidth(strs) * 15
Dim a As Long, b As Long
a = GetStrWidth(strs) * 15
Print strs
Line (a, 0)-(a, 1000)
End Sub
但 我还是想知道getcharwidth的详细用法,请高手指点