Sign in to follow this  
Followers 0
the_lord_mephy

Label Problem

5 posts in this topic

#1 ·  Posted (edited)

Alright, my I create a label, make the text in it, but i figure it would look better with a border around it, so I try putting a border around it using $SS_ETCHEDFRAME but when i do that, my text disappears!

What's going on here?

Also:

There's no way to make a label automatically wide enough to fit all of the text in it is there?

Edited by the_lord_mephy

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Also:

There's no way to make a label automatically wide enough to fit all of the text in it is there?

It's possible with GetTextExtentPoint32 API function

Here is my function to set horizontal scrollbar range in listbox based on the width of widest item

$sirka_max is max. width in pixels

$List1 is control ID of my ListBox

$hwnd_list is handle of my ListBox

$hwnd_list = ControlGetHandle($Form1, "", $List1)oÝ÷ Ù«­¢+ÙÕ¹MÑ!½É¥éMɽ±± È ¤($ÀÌØíÍÑÉÕÐô±±MÑÉÕÑ
ÉÑ ÅÕ½Ðí¡ÉlÐÀäÙtÅÕ½Ðì¤($ÀÌØíÁÍÑÉÕÐô±±MÑÉÕÑÑAÑÈ ÀÌØíÍÑÉÕФ($ÀÌØíÁ½ÐôU%
ÑɱM¹5Í ÀÌØí1¥ÍÐÄ°ÀÌØí1   }Q
=U9P°À°À¤($($ÀÌØí¡ô11
±° ÅÕ½ÐíÕÍÈÌȹ±°ÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÅÕ½ÐíÑÅÕ½Ðì°ÅÕ½Ðí¡Ý¹ÅÕ½Ðì°ÀÌØí¡Ý¹}±¥ÍФ($ÀÌØí¡ôÀÌØí¡
lÁt($ÀÌØí¡½¹Ðô±±
±° ÅÕ½ÐíÕÍÈÌȹ±°ÅÕ½Ðì°ÅÕ½ÐíÁÑÈÅÕ½Ðì°ÅÕ½ÐíM¹5ÍÍÅÕ½Ðì°ÅÕ½Ðí¡Ý¹ÅÕ½Ðì°ÀÌØí¡Ý¹}±¥ÍаÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÀÌØí]5}Q=9P°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°À°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°À¤($ÀÌØí¡½¹ÐôÀÌØí¡½¹ÑlÁt($ÀÌØí¡=±ô±±
±° ÅÕ½Ðí¤Ìȹ±°ÅÕ½Ðì°ÅÕ½Ðí!ݹÅÕ½Ðì°ÅÕ½ÐíM±Ñ=©ÐÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÀÌØí¡°ÅÕ½ÐíÁÑÈÅÕ½Ðì°ÀÌØí¡½¹Ð¤($ÀÌØíÍÑÉÕÑ}Í¥éô±±MÑÉÕÑ
ÉÑ ÅÕ½Ðí¥¹Ðí¥¹ÐÅÕ½Ðì¤($ÀÌØíͥɭ}µàôÀ($(½ÈÀÌØí¤ôÀѼÀÌØíÁ½Ð´Ä($%U%
ÑɱM¹5Í ÀÌØí1¥ÍÐÄ°ÀÌØí1   }QQaP°ÀÌØí¤°ÀÌØíÁÍÑÉÕФ($$ÀÌØíɬô±±MÑÉÕÑÑÑ ÀÌØíÍÑÉÕаĤ($%±±
±° ÅÕ½Ðí¤Ìȹ±°ÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÅÕ½ÐíÑQáÑáѹÑA½¥¹ÐÌÈÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÀÌØí¡°ÅÕ½ÐíÍÑÈÅÕ½Ðì°ÀÌØíɬ°ÅÕ½Ðí±½¹ÅÕ½Ðì°MÑÉ¥¹1¸ ÀÌØíɬ¤°ÅÕ½ÐíÁÑÈÅÕ½Ðì°±±MÑÉÕÑÑAÑÈ ÀÌØíÍÑÉÕÑ}ͥ餤($$ÀÌØíͥɭô±±MÑÉÕÑÑÑ ÀÌØíÍÑÉÕÑ}Í¥é°Ä¤($%%ÀÌØíͥɭÐìÀÌØíͥɭ}µàQ¡¸ÀÌØíͥɭ}µàôÀÌØíͥɭ(%9áÐ($$($ÀÌØí¡=±ô±±
±° ÅÕ½Ðí¤Ìȹ±°ÅÕ½Ðì°ÅÕ½Ðí!ݹÅÕ½Ðì°ÅÕ½ÐíM±Ñ=©ÐÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÀÌØí¡°ÅÕ½ÐíÁÑÈÅÕ½Ðì°ÀÌØí¡=±¤(%11
±° ÅÕ½ÐíÕÍÈÌȹ±°ÅÕ½Ðì°ÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÅÕ½ÐíI±ÍÅÕ½Ðì°ÅÕ½Ðí¡Ý¹ÅÕ½Ðì°ÀÌØí¡Ý¹}±¥ÍаÅÕ½Ðí¥¹ÐÅÕ½Ðì°ÀÌØí¡¤($ÀÌØíÍÑÉÕÑ}Í¥éôÀ($ÀÌØíÍÑÉÕÐôÀ((%U%
ÑɱMÑ1¥µ¥Ð ÀÌØí1¥ÍÐÄ°ÀÌØíͥɭ}µà¤ìѼ±¥µ¥Ð¡½É¥é½¹Ñ°Íɽ±±¥¹)¹Õ¹

You can use this approach also on label control, look here for example with label

Edited by Zedna

Share this post


Link to post
Share on other sites

Yashied,

Here is my UDF for sizing a label to fit a text - it also lets you set a maximum width for the label if you require. There is an example commented out at the end of the UDF:

#include-once

#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <WinAPI.au3>

; #FUNCTION# =======================================================================================
;
; Name...........: _StringSize
; Description ...: Returns size of rectangle required to display string - width can be chosen
; Syntax ........: _StringSize($sText[, $iSize[, $iWeight[, $iAttrib[, $sName[, $iWidth]]]]])
; Parameters ....: $sText   - String to display
;                 $iSize   - Font size in points - default AutoIt GUI default
;                 $iWeight - Font weight (400 = normal) - default AutoIt GUI default
;                 $iAttrib - Font attribute (0-Normal, 2-Italic, 4-Underline, 8 Strike - default AutoIt
;                 $sName   - Font name - default AutoIt GUI default
;                 $iWidth  - [optional] Width of rectangle - default is unwrapped width of string
; Requirement(s) : v3.2.12.1 or higher
; Return values .: Success - Returns array with details of rectangle required for text:
;                 |$array[0] = String formatted with @CRLF at required wrap points
;                 |$array[1] = Height of single line in selected font
;                 |$array[2] = Width of rectangle required to hold formatted string
;                 |$array[3] = Height of rectangle required to hold formatted string
;                 Failure - Returns 0 and sets @error:
;                 |1 - Incorrect parameter type (@extended = parameter index)
;                 |2 - Failure to create GUI to test label size
;                 |3 - Failure of _WinAPI_SelectObject
;                 |4 - Font too large for chosen width - longest word will not fit
; Author ........: Melba23
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
;===================================================================================================


Func _StringSize($sText, $iSize = Default, $iWeight = Default, $iAttrib = Default, $sName = Default, $iWidth = 0)
    
    Local $hWnd, $hFont, $hDC, $oFont, $tSize, $hGUI, $hText_Label, $sTest_Line
    Local $iLine_Count, $iLine_Width, $iWrap_Count, $iLast_Word
    Local $asLines[1], $avSize_Info[4], $aiPos[4]
    
; Check parameters are correct type
    If Not IsString($sText) Then Return SetError(1, 1, 0)
    If Not IsNumber($iSize) And $iSize <> Default   Then Return SetError(1, 2, 0)
    If Not IsInt($iWeight)  And $iWeight <> Default Then Return SetError(1, 3, 0)
    If Not IsInt($iAttrib)  And $iAttrib <> Default Then Return SetError(1, 4, 0)
    If Not IsString($sName) And $sName <> Default   Then Return SetError(1, 5, 0)
    If Not IsNumber($iWidth) Then Return SetError(1, 6, 0)

; Create GUI to contain test labels, set to passed font parameters 
    $hGUI = GUICreate("", 1200, 500, 10, 10)
        If $hGUI = 0 Then Return SetError(2, 0, 0)
        GUISetFont($iSize, $iWeight, $iAttrib, $sName)
    
; Store unwrapped text
    $avSize_Info[0] = $sText
    
; Ensure EoL is @CRLF and break text into lines
    If StringInStr($sText, @CRLF) = 0 Then StringRegExpReplace($sText, "[x0a|x0d]", @CRLF)
    $asLines = StringSplit($sText, @CRLF, 1)
        
; Draw label with unwrapped lines to check on max width
    $hText_Label = GUICtrlCreateLabel($sText, 10, 10)
    $aiPos = ControlGetPos($hGUI, "", $hText_Label)
    GUICtrlDelete($hText_Label)
    
; Store line height for this font size after removing label padding (always 8)
    $avSize_Info[1] = ($aiPos[3] - 8)/ $asLines[0]
; Store width and height of this label
    $avSize_Info[2] = $aiPos[2]
    $avSize_Info[3] = $aiPos[3] - 4; Reduce margin
    
; Check if wrapping is required
    If $aiPos[2] > $iWidth And $iWidth > 0 Then
        
    ; Set returned text element to null
        $avSize_Info[0] = ""
        
    ; Set width element to max allowed
        $avSize_Info[2] = $iWidth
        
    ; Set line count to zero
        $iLine_Count = 0
        
    ; Take each line in turn
        For $j = 1 To $asLines[0]
            
        ; Size this line unwrapped
            $hText_Label = GUICtrlCreateLabel($asLines[$j], 10, 10)
            $aiPos = ControlGetPos($hGUI, "", $hText_Label)
            GUICtrlDelete($hText_Label)
            
        ; Check wrap status
            If $aiPos[2] < $iWidth Then
            ; No wrap needed so count line and store
                $iLine_Count += 1
                $avSize_Info[0] &= $asLines[$j] & @CRLF
            Else
            ; Wrap needed so need to count wrapped lines
                
            ; Create label to hold line as it grows
                $hText_Label = GUICtrlCreateLabel("", 0, 0)
            ; Initialise Point32 method
                $hWnd = ControlGetHandle($hGui, "", $hText_Label)
                $hFont = _SendMessage($hWnd, $WM_GETFONT)
                $hDC = _WinAPI_GetDC($hWnd)
                $oFont = _WinAPI_SelectObject($hDC, $hFont)
                If $oFont = 0 Then Return SetError(3, 0, 0)
                
            ; Zero counter
                $iWrap_Count = 0

                While 1
                    
                ; Set line width to 0
                    $iLine_Width = 0
                ; Initialise pointer for end of word
                    $iLast_Word = 0
                    
                    For $i = 1 To StringLen($asLines[$j])

                    ; Is this just past a word ending?
                        If StringMid($asLines[$j], $i, 1) = " " Then $iLast_Word = $i - 1
                    ; Increase line by one character
                        $sTest_Line = StringMid($asLines[$j], 1, $i)
                    ; Place line in label
                        GUICtrlSetData($hText_Label, $sTest_Line)
                    ; Get line length
                        $tSize = _WinAPI_GetTextExtentPoint32($hDC, $sTest_Line)
                        $iLine_Width = DllStructGetData($tSize, "X")
                        
                    ; If too long exit the loop
                        If $iLine_Width >= $iWidth - Int($iSize / 2) Then ExitLoop
                    Next
                        
                ; End of the line of text?
                    If $i > StringLen($asLines[$j]) Then
                    ; Yes, so add final line to count
                        $iWrap_Count += 1
                    ; Store line
                        $avSize_Info[0] &= $sTest_Line & @CRLF
                        ExitLoop
                    Else
                    ; No, but add line just completed to count
                        $iWrap_Count += 1
                    ; Check at least 1 word completed or return error
                        If $iLast_Word = 0 Then
                            GUIDelete($hGUI)
                            Return SetError(4, 0, 0)
                        EndIf
                    ; Store line up to end of last word
                        $avSize_Info[0] &= StringLeft($sTest_Line, $iLast_Word) & @CRLF
                    ; Strip string to point reached
                        $asLines[$j] = StringTrimLeft($asLines[$j], $iLast_Word)
                    ; Trim leading whitespace
                        $asLines[$j] = StringStripWS($asLines[$j], 1)
                    ; Repeat with remaining characters in line
                    EndIf
                    
                WEnd
                
            ; Add the number of wrapped lines to the count
                $iLine_Count += $iWrap_Count
                
            ; Clean up
                _WinAPI_ReleaseDC($hWnd, $hDC)
                GUICtrlDelete($hText_Label)
                
            EndIf
            
        Next
        
    ; Convert lines to pixels and add reduced margin
        $avSize_Info[3] = ($iLine_Count * $avSize_Info[1]) + 4
        
    EndIf

; Clean up
    GUIDelete($hGUI)
    
; Return array
    Return $avSize_Info

EndFunc; => _StringSize

#cs

; StringSize Test example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; This Test code includes the fully commented StringSize code
; Hence the need for these include files
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <WinAPI.au3>
; These include lines are contained in the code of the UDF itself

Global $aFont[4] = ["Arial", "Tahoma", "Courier New", "Comic Sans MS"]
Global $aSize[4] = [9, 12, 10, 15]
Global $aWeight[4] = [200, 400, 600, 800]
Global $aAttrib[4] = [0, 2, 4, 0]
Global $aMaxWidth[4] = [250, 300, 400, 500]
Global $aColour[4] = [0xFFFF88, 0xBBFF88, 0xBB88FF, 0xFF88FF]
Global $aButMsg[4] = ["Press for next example", "Click here", "Please push", "And the next one please..."]

$sLabelMsg  = "This is a message with very long lines which are unlikely to fit within the maximum width specified.  " & _
"This UDF will return the size of rectangle needed to display it in a selected font and size." & @CRLF & @CRLF & _
"The GUI can then be sized to fit and accurately placed on screen.  Other controls, such as the automatically " & _
"sized button under this label, can also be accurately positioned relative to the text."

GUICreate("String Sizing Test", 500, 500)
GUISetState()

While 1
    
    $sFont = $aFont[Random(0, 3, 1)]
    $iSize = $aSize[Random(0, 3, 1)]
    $iWeight = $aWeight[Random(0, 3, 1)]
    $iAttrib = $aAttrib[Random(0, 3, 1)]
    $iMaxWidth = Random(200, 850, 1)
    $iColour = $aColour[Random(0, 3, 1)]
    $sButMsg = $aButMsg[Random(0, 3, 1)]
    
    $aButReturn = _StringSize($sButMsg)
    $aMsgReturn = _StringSize($sLabelMsg, $iSize, $iWeight, $iAttrib, $sFont, $iMaxWidth)
    $iError = @error
    
    If IsArray($aMsgReturn) = 1 Then
        
        WinMove("String Sizing Test", "", (@DesktopWidth - ($aMsgReturn[2] + 25)) / 2, (@DesktopHeight - ($aMsgReturn[3] + 85)) / 2, $aMsgReturn[2] + 25, $aMsgReturn[3] + 85 )
        
        $hLabel = GUICtrlCreateLabel($aMsgReturn[0], 10, 10, $aMsgReturn[2], $aMsgReturn[3], 1)
            GUICtrlSetFont(-1, $iSize, $iWeight, $iAttrib, $sFont)
            GUICtrlSetBkColor(-1, $iColour)
        $hNext = GUICtrlCreateButton($sButMsg, ($aMsgReturn[2] - $aButReturn[2]) / 2, $aMsgReturn[3] + 20, $aButReturn[2] + 20, 30)
        
        While 1
    
        $iMsg = GUIGetMsg()
        
        If $iMsg = -3 Then Exit
        
        If $iMsg = $hNext Then ExitLoop
    
        WEnd

        GUICtrlDelete($hNext)
        GUICtrlDelete($hLabel)
    Else
        MsgBox(0, "Error", "Code = " & $iError)
    EndIf

WEnd

Exit

; End of example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#ce

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yashied,

Here is my UDF for sizing a label to fit a text - it also lets you set a maximum width for the label if you require. There is an example commented out at the end of the UDF:

...

Nice too, Melba23, thanks. Until that time I did not have to use it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0