Jump to content
kisstom

[Solved] GUICtrlCreateLabel vertical scrollbar, text justification

Recommended Posts

kisstom

Hi, i would like to make a simple gui with a picture on the top and some text in the lower part of the gui.

I wrote this program, its ok but i would like a vertical scrollbar if the text too long (for the text only if possible)

EDIT: i added a line : $Text = StringRegExpReplace($Text, '(.{70,}?)\h', '$1 ' & @LF); break long lines

#include <UnRAR.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include<guiconstants.au3>

Global $bckcolor = 0xF2F2F2
$Text = FileRead("c:\TestText.txt")
$Text = StringRegExpReplace($Text, '(.{70,}?)\h', '$1 ' & @LF); break long lines
$Pic = ("c:\TestPic.jpg")

GUICreate("TEST", 450, 750,Default,Default, $WS_SIZEBOX + $WS_SYSMENU)
GUISetBkColor($bckcolor)
GUICtrlCreatePic($Pic, 10, 10, 430, 550)
GUICtrlCreateLabel($Text, 10, 570, 430, 130)

GUISetState(@SW_SHOW)

While 1
  Sleep(10)
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                Exit

       EndSwitch
Wend

 

Edited by kisstom

Share this post


Link to post
Share on other sites
Melba23

kisstom,

If you use a couple of my UDFs then it is very easy:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "GUIScrollbars_Ex.au3"
#include "StringSize.au3"

; Read file
$sText = FileRead(@ScriptFullPath)
; Get width and height of file text wrapped to GUI width
$aSize = _StringSize($sText, Default, Default, Default, "Arial", 430)

; Create main GUI
$hGUI = GUICreate("TEST", 450, 750, Default, Default, BitOR($WS_SIZEBOX, $WS_SYSMENU))
; Simulate pic control
GUICtrlCreateLabel($aSize[0], 10, 10, 430, 550)
GUICtrlSetBkColor(-1, 0xC4C4C4)

GUISetState(@SW_SHOW)

; Creat child GUI to hold scrollable text
$hGUI_Scroll = GUICreate("", 430, 130, 10, 570, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)

; Create label to hold text
$cLabel = GUICtrlCreateLabel($aSize[0], 0, 0, 430, $aSize[3])
GUICtrlSetFont($cLabel, Default, Default, Default, "Arial")

GUISetState()

; Generate vertical scrollbar of correct size
_GUIScrollbars_Generate($hGUI_Scroll, 0, $aSize[3])

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

You can find the UDFs in my sig below.  Please ask if you have any questions.

M23

 

Edited by Melba23
Added more comments
  • Like 1

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
kisstom

Thx Melba23, seems ok but how can i set the bckground color of the text? GUICtrlSetBkColor(-1, 0xC4C4C4) seems does nothing. if use GUISetBkColor(0xF2F2F2) it makes gray the gui background as i want but the text has brown background color

EDIT: and can i change the size of the font?

 

Edited by kisstom

Share this post


Link to post
Share on other sites
Melba23

kisstom,

seems ok

Nice piece of understatement there, are you English too?

You can set the background colour and change the font size of the text by using the standard GUICtrlSetFont/BkColor commands on the label containing the text - here the font size is 14 and you have a brown(ish) background:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "GUIScrollbars_Ex.au3"
#include "StringSize.au3"

$iFontSize = 14
$iFontBkColor = 0xDF7000

; Read file
$sText = FileRead(@ScriptFullPath)
; Get width and height of file text
$aSize = _StringSize($sText, $iFontSize, Default, Default, "Arial", 430)

; Create main GUI
$hGUI = GUICreate("TEST", 450, 750, Default, Default, BitOR($WS_SIZEBOX, $WS_SYSMENU))
; Simulate pic control
GUICtrlCreateLabel("", 10, 10, 430, 550)
GUICtrlSetBkColor(-1, 0xC4C4C4)

GUISetState(@SW_SHOW)

; Creat child GUI to hold scrollable text
$hGUI_Scroll = GUICreate("", 430, 130, 10, 570, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)

; Create label to hold text
$cLabel = GUICtrlCreateLabel($aSize[0], 0, 0, 430, $aSize[3])
GUICtrlSetFont($cLabel, $iFontSize, Default, Default, "Arial")
GUICtrlSetBkColor($cLabel, $iFontBkColor)

GUISetState()

; Generate vertical scrollbar of correct size
_GUIScrollbars_Generate($hGUI_Scroll, 0, $aSize[3])

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Note you have to use the same font for the _StringSize call and the label - otherwise the size calculations will not be accurate.

M23

  • Like 1

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
kisstom

Works now! Thank you. One last question. Can i - not sure how to say in English - justify (?) the text?

I mean, this text:

xxx xx xxxxx xx

xxx xx xx

I want to see like this:

xxx xx xxxxx xx

xxx     xx      xx

Now you can see i'm not an English :)

EDIT: i just google it, i want justified text istead of left aligned

EDIT 2: i want some "intelligent" justified text, means if needs more than 4-5 consecutive spaces let the line remain left aligned

Edited by kisstom

Share this post


Link to post
Share on other sites
Melba23

kisstom,

You can set the label to have the $SS_CENTER style:

#include <StaticConstants.au3>
;
;
;
$cLabel = GUICtrlCreateLabel($aSize[0], 0, 0, 430, $aSize[3], $SS_CENTER)

Whether that is "intelligent" enough only you can say.  If it is not, then you will probably have to do some clever stuff with my StringSize UDF to position the lines - I would recommend sticking with the style.

M23


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
kisstom

You can set the label to have the $SS_CENTER style:

Thx but this just center the text not justify. I solved the justification with your StringSize UDF. I choose the easy way so it works with monospaced fonts only.

Here is the code if someone interested:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <guiconstants.au3>
#include "StringSize.au3"

Global $iFontSize = 10 ; default :8.5
Global $MaxLineWidth = 0
Global $MaxSpace = 100 ; maximum spaces in a line
Global $TextJustified = ""
Global $MaxConsSpaces = 2; maximum concesutive spaces
Global $spaces = ""

For $o = 1 to $MaxConsSpaces
    $spaces = $spaces & " "
Next

$Text = "YOUNG SHERLOCK HOLMES 1" & @LF
$Text = $Text & "The year is 1868, and Sherlock Holmes is fourteen. His life is that of a perfectly ordinary army officer’s son: boarding school, good manners, a classical education – the backbone of the British Empire. But all that is about to change. With his father suddenly posted to India, and his mother mysteriously ‘unwell’, Sherlock is sent to stay with his eccentric uncle and aunt in their vast house in Hampshire. So begins a summer that leads Sherlock to uncover his first murder, a kidnap, corruption and a brilliantly sinister villain of exquisitely malign intent."
$Text = StringRegExpReplace($Text, '(.{70,}?)\h', '$1 ' & @LF) ; break long line
$aTextLines = StringSplit($Text,@LF,1) ;split text to lines, $aTextLines[0] gives the number of lines

For $i = 1 to ($aTextLines[0])
    $aSize = _StringSize($aTextLines[$i], $iFontSize, Default, Default, "Courier New") ; Courier New is monospaced font
    if $aSize[2] > $MaxLineWidth Then
        $MaxLineWidth = $aSize[2]
    EndIf
Next

For $p = 1 to ($aTextLines[0])

For $i = 1 to $MaxSpace
    $aTextLines [$p] = StringReplace ($aTextLines [$p], " ", "  ", 1)
    if @extended = 0 Then
        $aTextLines [$p] = StringReplace ($aTextLines [$p], "**", " *")
    EndIf

    $aTextLines [$p] = StringReplace ($aTextLines [$p], "  ", "**")
    $aSize = _StringSize($aTextLines[$p], $iFontSize, Default, Default, "Courier New")
    if $aSize[2] >= $MaxLineWidth + 1 Then
        ExitLoop
    EndIf

Next
$aTextLines [$p] = StringReplace ($aTextLines [$p], "*", " ")

For $z = 1 to $MaxSpace
$aTextLines [$p] = StringReplace ($aTextLines [$p], $spaces & " ", $spaces) ; max constucive spaces
Next

Next

For $i = 1 to ($aTextLines[0])                                  ;-
    $TextJustified = $TextJustified & $aTextLines[$i] & @LF     ;-- Lines to text
Next                                                            ;-


$hGUI = GUICreate("Test", 650, 500)
GUISetFont($iFontSize, Default, Default, "Courier New") ; Courier New is monospaced font
GUICtrlCreateLabel($TextJustified, 10, 10)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Edited by kisstom

Share this post


Link to post
Share on other sites
Melba23

kisstom,

I would do it like this - it works for both monospaced and proportional fonts:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <String.au3>
#include <Array.au3>

#include "StringSize.au3"

Global $iMaxWidth = 630 ; max width of a line

$sText = "YOUNG SHERLOCK HOLMES 1" & @CRLF & _
        "The year is 1868, and Sherlock Holmes is fourteen.  His life is that of a perfectly ordinary army officer’s son: " & _
        "boarding school, good manners, a classical education – the backbone of the British Empire.  " & _
        "But all that is about to change. With his father suddenly posted to India, and his mother mysteriously ‘unwell’, " & _
        "Sherlock is sent to stay with his eccentric uncle and aunt in their vast house in Hampshire.  " & _
        "So begins a summer that leads Sherlock to uncover his first murder, a kidnap, corruption and a brilliantly sinister villain of exquisitely malign intent."

; Create a GUI
$hGUI = GUICreate("Test", $iMaxWidth + 20, 500)

$cLabel = GUICtrlCreateLabel("", 10, 10, $iMaxWidth, 400)

$cSet = GUICtrlCreateButton("Set", 200, 460, 80, 30)
$cExit = GUICtrlCreateButton("Exit", 370, 460, 80, 30)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $cExit

            Exit
        Case $cSet

            $iFontSize = Number(InputBox("Font Size", "Insert Font Size"))
            $sFont = InputBox("Font Name", "Insert Font Name")
            $iMaxConsSpaces = Number(InputBox("Max Spaces", "Input Max Spaces"))
            $sNewText = _Justify_Text($sText, $iFontSize, $sFont, $iMaxWidth, $iMaxConsSpaces)
            GUICtrlSetFont($cLabel, $iFontSize, Default, Default, $sFont)
            GUICtrlSetData($cLabel, $sNewText)
    EndSwitch

WEnd



Func _Justify_Text($sText, $iFontSize, $sFont, $iMaxWidth, $iMaxConsSpaces)

    ; Convert text to wrapped
    $aSize = _StringSize($sText, $iFontSize, Default, Default, $sFont, $iMaxWidth)
    
    ; Split into lines
    $aLines = StringSplit($aSize[0], @CRLF, $STR_ENTIRESPLIT)

    ; For each line
    For $i = 1 To $aLines[0]
        $sLine = $aLines[$i]
        ; Force all spaces to single
        $sLine = StringRegExpReplace($sLine, "(?<!\s)(\s+)(?!\s)", " ")
        ; And determine margin to fill to full width
        $aSize = _StringSize($sLine, $iFontSize, Default, Default, $sFont)
        $iMargin = $iMaxWidth - $aSize[2]

        ; Split line into words
        $aWords = StringSplit($sLine, " ")
        ; And count spaces
        $iCount = $aWords[0] - 1
        Local $aAdditions[$iCount] ; Array to hold final space size

        ; how mant spaces can we add to fill line to maximum width
        $iAdditions = 0
        $sFiller = ""
        ; We only look to fill if we do not need to expand spaces beyond set limit
        For $j = 1 To $iCount * ($iMaxConsSpaces - 1)
            $sFiller &= " "
            $aSize = _StringSize($sFiller, $iFontSize, Default, Default, $sFont)
            If $aSize[2] > $iMargin Then
                ; We now know how many spaces we can add
                $iAdditions = $j - 1
                ExitLoop

            EndIf

        Next

        ; So now we determine how many spaces we need between each word
        $iIndex = 0
        For $j = 1 To $iAdditions

            ; Increase count by 1
            $aAdditions[$iIndex] += 1
            $iIndex += 1
            ; Reset to beginning of line
            If $iIndex = $iCount Then $iIndex = 0
        Next

        ; Now recreate line with the determined number of spaces between each word
        $sLine = ""
        For $j = 1 To $aWords[0] - 1
            $sLine &= $aWords[$j]
            $sSpaces = _StringRepeat(" ", $aAdditions[$j - 1] + 1)
            $sLine &= $sSpaces

        Next
        $sLine &= $aWords[$j]

        ; And store the result
        $aLines[$i] = $sLine



    Next

    ; Convert the lines back into a single text
    Return _ArrayToString($aLines, @CRLF, 1)

EndFunc   ;==>_Justify_Text

Please ask if you have any questions.


M23


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
kisstom

Thank you M23, this is professional code, i just watch and learn :)

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

×