回复 3# netegg
多谢 netegg 的提醒,参照_GDIPlus_GraphicsMeasureCharacterRanges的示例,可以取得行数了,但发现识别时如果有某一行的字符太靠近label边缘,则识别结果会多一行。#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <StaticConstants.au3>
Local $sString = "Object-oriented GUI graphical xxxxx storage management software, it can achieve the MegaRAID storage array of local, remote monitoring, configuration and management.>>"
Local $LabelW = 200;, $LabelH = 300
$hGUI = GUICreate("我的 GUI") ; 创建居中显示的 GUI 窗口
$LabelLine = _GetLabelLine($hGUI, $sString, $LabelW, 9, 0, "Arial")
GUICtrlCreateLabel($sString, 10, 10, $LabelW, $LabelLine*15)
GUICtrlSetBkColor(-1, 0x1c9fe3)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 9, 400, 0, "Arial")
GUICtrlCreateLabel("字符串占用的行数: " & $LabelLine, $LabelW+30, 20, 200, 50)
GUISetState(@SW_SHOW) ; 显示一个空白的窗口
; 运行 GUI, 直到 GUI 被关闭
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
Func _GetLabelLine($hWnd, $sString, $LabelW, $FSize, $FStyle, $FName)
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate($FName)
$hFont = _GDIPlus_FontCreate($hFamily, $FSize, $FStyle)
$tLayout = _GDIPlus_RectFCreate(0, 0, $LabelW, 0)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
If IsArray($aInfo) Then
Return $aInfo[2]
Else
Return 0
EndIf
EndFunc
|