回复 1# cashiba
回复 16# fuldho
;这是要获取宽度的文字
Global $text="W"
Global $user32Dll=DllOpen("user32.dll")
GetTextWidthAndHeight($text)
Func GetTextWidthAndHeight($_text="")
Local $_resultArray[2]=[-1,-1]
If $_text="" Then Return $_resultArray
Local $hdc = DllCall($user32Dll, "handle", "GetDC", "hwnd", 0)
If @error Then Return $_resultArray
$hdc = $hdc[0]
;构建一个Rect的结构体
Local $tagRECT = "struct;long Left;long Top;long Right;long Bottom;endstruct"
Local $tRect = DllStructCreate($tagRect)
;初始化结构体变量初始值
DllStructSetData($tRect, "Left", 1)
DllStructSetData($tRect, "Top", 1)
DllStructSetData($tRect, "Right", 1)
DllStructSetData($tRect, "Bottom", 1)
;获取结构体变量指针
Local $pRect = DllStructGetPtr($tRect)
;定义字体格式
Local $format = BitOR(1024, 32)
;输出文字到DC
DllCall($user32Dll,"INT","DrawTextA","Int", $hdc,"Str",$text,"int",-1,"ptr",$pRect,"int",$format)
If @error Then Return $_resultArray
;获取返回的Rect信息
Local $right= DllStructGetData($tRect, "Right")
Local $left = DllStructGetData($tRect, "Left")
Local $top= DllStructGetData($tRect, "Top")
Local $bottom = DllStructGetData($tRect, "Bottom")
ConsoleWrite($right&@TAB&$left&@TAB&$top&@TAB&$bottom&@CRLF)
;计算宽度
$_resultArray[0]=$right-$left
;计算高度
$_resultArray[1]=$bottom-$top
Return $_resultArray
EndFunc
|