找回密码
 加入
搜索
查看: 4945|回复: 4

[GUI管理] 如何取得 Label 中字符的行数?[已解决]

[复制链接]
发表于 2012-5-31 23:31:08 | 显示全部楼层 |阅读模式
本帖最后由 yhxhappy 于 2012-6-2 16:03 编辑

如下图,当Label中的字符过多时,会转下一行。


我能取得字符的全长(像素宽度),但用这个宽度除以Label的宽度显然行不通。
现我想知道Label占用了几行,大家帮看看!
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Local $Text = "Object-oriented GUI graphical storage management software, it can achieve the MegaRAID storage array of local, remote monitoring, configuration and management."

GUICreate("我的 GUI") ; 创建居中显示的 GUI 窗口
GUICtrlCreateLabel($Text, 10, 10, 200, 100)
GUICtrlSetBkColor(-1, 0x1c9fe3)
GUICtrlSetColor(-1, 0xffffff)

GUISetState(@SW_SHOW) ; 显示一个空白的窗口

    ; 运行 GUI, 直到 GUI 被关闭
While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then Exit
WEnd
感谢大家的帮助,在4楼写的脚本基本能实现我要的功能了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-6-1 08:43:11 | 显示全部楼层
可以那种不是由换行符组成的 否则你可以检查换行符的个数来确定·
发表于 2012-6-1 21:38:33 | 显示全部楼层
本帖最后由 netegg 于 2012-6-1 21:41 编辑

大概思路有,实现可能很麻烦,获取控件的大小,以控件创建场景,用_GDIPlus_GraphicsMeasureCharacterRanges获取包含全部字符的上下界的矩形,获取字体的高度,行间距,计算
 楼主| 发表于 2012-6-2 01:40:28 | 显示全部楼层
回复 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
发表于 2012-6-2 08:10:22 | 显示全部楼层
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-27 15:55 , Processed in 0.077807 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表