Jump to content

Center Text w/ Wrap for a Label


ViciousXUSMC
 Share

Recommended Posts

I am working on a random question script, it was looking good until I started to put my real questions in place of my temporary ones.

I was using SS_Center + $SS_CenterImage to get the labels that make up the answer to look nice.  But if the text is too long it goes off screen.

I tried to manually add a @LF or @CRLF via RegEX but it does not respond to those it stays on one line.

So I took out SS_CenterImage and it wraps fine but I am not happy with how it looks.

Anybody have a simple solution to the problem?  

Code Below:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <File.au3>

#Region ### START Koda GUI section ### Form=c:\users\it022565\desktop\random question\form1.kxf
$Form1_1 = GUICreate("Question", 609, 470, 308, 317)
GUISetBkColor(0x000000)
$Label1 = GUICtrlCreateLabel("Random Question Of The Day", 184, 0, 243, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label2 = GUICtrlCreateLabel("Question Body", 8, 32, 593, 148)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label3Border = GUICtrlCreateLabel("", 5-2, 192-2, 600+4, 60+4)
GUICtrlSetBkColor(-1, 0x800000)
$Label3 = GUICtrlCreateLabel("Label3", 5, 192, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4Border = GUICtrlCreateLabel("", 5-2, 258-2, 600+4, 60+4)
GUICtrlSetBkColor(-1, 0x800000)
$Label4 = GUICtrlCreateLabel("Label4", 5, 258, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5Border = GUICtrlCreateLabel("", 5-2, 324-2, 600+4, 60+4)
GUICtrlSetBkColor(-1, 0x800000)
$Label5 = GUICtrlCreateLabel("Label5", 5, 324, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label6Border = GUICtrlCreateLabel("", 5-2, 390-2, 600+4, 60+4)
GUICtrlSetBkColor(-1, 0x800000)
$Label6 = GUICtrlCreateLabel("Label6", 5, 390, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$aFiles = _FileListToArray(@Scriptdir, "Question*.txt", $FLTA_FILES, True)
$iSelect = Random(1, $aFiles[0], 1)

$sContext = FileRead($aFiles[$iSelect])

$aQuestion = StringRegExp($sContext, '&([^&]+)&', 3)
GUICtrlSetData($Label2, $aQuestion[0])

$aAnswer1 = StringRegExp($sContext, "!([^!\r\n]+)", 3)
GUICtrlSetData($Label3, $aAnswer1[0])

$aAnswer2 = StringRegExp($sContext, "@([^@\r\n]+)", 3)
GUICtrlSetData($Label4, $aAnswer2[0])

$aAnswer3 = StringRegExp($sContext, "#([^#\r\n]+)", 3)
GUICtrlSetData($Label5, $aAnswer3[0])

$aAnswer4 = StringRegExp($sContext, "\$([^$\r\n]+)", 3)
GUICtrlSetData($Label6, $aAnswer4[0])


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Label3Border
            $vAnswer = 1
            ExitLoop
        Case $Label4Border
            $vAnswer = 2
            ExitLoop
        Case $Label5Border
            $vAnswer = 3
            ExitLoop
        Case $Label6Border
            $vAnswer = 4
            ExitLoop
    EndSwitch
WEnd

$Form1_2 = GUICreate("Answer", 609, 470, 308, 317)
GUISetBkColor(0x000000)
GUISwitch($Form1_2)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("Question Body", 8, 32, 593, 148,  BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetColor(-1, 0xFF0000)
GUiCtrlSetData(-1, ReadRadioButton())
GUICtrlSetFont(-1, 75, 800, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("OK", 250, 300, 140, 70)

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

Func ReadRadioButton()
        If $vAnswer = 1 Then Return $aAnswer1[1]
        If $vAnswer = 2 Then Return $aAnswer2[1]
        If $vAnswer = 3 Then Return $aAnswer3[1]
        If $vAnswer = 4 Then Return $aAnswer4[1]
EndFunc

Edit: Attached a question so you can have runnable code.

Question1.txt

Edited by ViciousXUSMC
Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

Can we please have an example of the text blocks you wish to use - otherwise we risk having the same problem as you.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Its attached, the script will read the file and set the label data.

In this particular case its the first answer that is long enough to wrap.

The problem can be created by changing the style to this in the code posted in the first post.

$Label3 = GUICtrlCreateLabel("Label3", 5, 192, 600, 60, BitOR($SS_CENTER,$SS_CENTERIMAGE))

I am trying to keep the "buttons" in the same size/orientation for presentation purposes.  I started with buttons but ended up with labels in the end for that text w/ border look.

Ultimate goal here is a simple and easy to update quiz type app that will ask one question when you log in every day.

Can just use a network location to easily update question.txt files as needed. 

Edited by ViciousXUSMC
Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

Using my StringSize UDF (as I suggested) allows you to add spacer lines to get the text as near as possible to the centre of the label.

I rewrote the questions file into INI format to make the whole thing easier:

[Total]
Total=1
[1]
Q=Regarding the Tiburon MobileCom application, wich of the following is correct:
1=Users should log off and sign on at the start of each shift, listing all PCFR members assigned to that apparatus
2=Only the primary user should log off and sign on
3=As long as the system is up and running, users should not log off and sign back on
4=None of the above
A=1

Total is the total number of questions - each of which is divided into the question, 4 answers, and the index of the correct answer.

Then you can do this:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <File.au3>

#include "StringSize.au3"
#include "String.au3"

$sIniFile = "Question.txt"
$iGUI_Width = 470
$iQuestion_Height = 148
$iAnswer_Height = 60+4

#Region ### START Koda GUI section ### Form=c:\users\it022565\desktop\random question\form1.kxf

$Form1_1 = GUICreate("Question", 609, $iGUI_Width, 308, 317)
GUISetBkColor(0x000000)
$Label1 = GUICtrlCreateLabel("Random Question Of The Day", 184, 0, 243, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label2 = GUICtrlCreateLabel("Question Body", 8, 32, 593, $iQuestion_Height, $SS_CENTER)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label3Border = GUICtrlCreateLabel("", 5-2, 192-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label3 = GUICtrlCreateLabel("Label3", 5, 192, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4Border = GUICtrlCreateLabel("", 5-2, 258-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label4 = GUICtrlCreateLabel("Label4", 5, 258, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5Border = GUICtrlCreateLabel("", 5-2, 324-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label5 = GUICtrlCreateLabel("Label5", 5, 324, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label6Border = GUICtrlCreateLabel("", 5-2, 390-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label6 = GUICtrlCreateLabel("Label6", 5, 390, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Select random question
$iSelect = Random(1, IniRead($sIniFile, "Total", "Total", "Error"), 1)

; Read in each element of the question/answer, size it and set it in the label
;                                 Question text                               Label width & height
GUICtrlSetData($Label2, _SizeText(IniRead($sIniFile, $iSelect, "Q", "Error"), $iGUI_Width, $iQuestion_Height))
GUICtrlSetData($Label3, _SizeText(IniRead($sIniFile, $iSelect, "1", "Error"), $iGUI_Width, $iAnswer_Height))
GUICtrlSetData($Label4, _SizeText(IniRead($sIniFile, $iSelect, "2", "Error"), $iGUI_Width, $iAnswer_Height))
GUICtrlSetData($Label5, _SizeText(IniRead($sIniFile, $iSelect, "3", "Error"), $iGUI_Width, $iAnswer_Height))
GUICtrlSetData($Label6, _SizeText(IniRead($sIniFile, $iSelect, "4", "Error"), $iGUI_Width, $iAnswer_Height))

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case $Label3Border
            $vAnswer = 1
            ExitLoop

        Case $Label4Border
            $vAnswer = 2
            ExitLoop

        Case $Label5Border
            $vAnswer = 3
            ExitLoop

        Case $Label6Border
            $vAnswer = 4
            ExitLoop

    EndSwitch

WEnd



; Check the result against the correct answer
$sResult = ( ($vAnswer = IniRead($sIniFile, $iSelect, "A", "Error")) ? ("Correct") : ("Wrong") )

$Form1_2 = GUICreate("Answer", 609, 470, 308, 317)
GUISetBkColor(0x000000)
GUISwitch($Form1_2)
GUISetState(@SW_SHOW)
GUICtrlCreateLabel("Question Body", 8, 32, 593, 148,  BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetFont(-1, 75, 800, 0, "MS Sans Serif")
GUICtrlSetData(-1, $sResult)
$Button1 = GUICtrlCreateButton("OK", 250, 300, 140, 70)

While 1
        $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Exit
    EndSwitch

WEnd



Func _SizeText($sText, $iWidth, $iHeight)
    ; Size the text wrapping into the label
    $aSize = _StringSize($sText, 10, 800, 0, "MS Sans Serif", $iWidth - 20)
    ; Now add spacing lines if any are needed ahead of the text to get it near the centre of the label
    Return _StringRepeat(@CRLF, Int(($iHeight - $aSize[3]) / $aSize[1] / 2)) & $aSize[0]
    
    ; Internal working:
    ; - $iHeight - $aSize[3] is the space below the text
    ; - divide by line height to get number of blank lines below text
    ; - divide by 2 to get required space ABOVE text
    ; - Int makes sure space ABOVE text smaller than below
    ; - add required @CRLFs before text to "centre" it

EndFunc

I realise that is it is quite a rewrite, but it has the advantage of only needing the single file to hold all the questions, so I hope you feel it was worth it. I leave the errorchecking to you!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Ha INI that is smart and not sure why I did not think about it.

I was thinking Excel at first, but wanted files to be small and not require any extra software.  

I think because I am learning RegEx its on the brain too much and forgot the basics.

 

I'll test now so I can see how this works.

 

 

Link to comment
Share on other sites

  • Moderators

ViciousXUSMC,

I think because I am learning RegEx its on the brain too much and forgot the basics

One of the first lessons to learn is when NOT to use RegExs!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Ended up with this, tweaked the size values a bit to make the text wider and so that it would add a line break on single line entries as they were by default still on the first line.

Your _SizeText() is really cool, I'll have to keep that handy.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <StringSize.au3>
#include <String.au3>

$sIniFile = "Question.txt"
$iGUI_Width = 470
$iQuestion_Height = 148
$iAnswer_Height = 60+4

#Region ### START Koda GUI section ### Form=c:\users\it022565\desktop\random question\form1.kxf

$Form1_1 = GUICreate("Question", 609, $iGUI_Width, 308, 317)
GUISetBkColor(0x000000)
$Label1 = GUICtrlCreateLabel("Random Question Of The Day", 184, 0, 243, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label2 = GUICtrlCreateLabel("Question Body", 8, 32, 593, $iQuestion_Height, $SS_CENTER)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label3Border = GUICtrlCreateLabel("", 5-2, 192-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label3 = GUICtrlCreateLabel("Label3", 5, 192, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label4Border = GUICtrlCreateLabel("", 5-2, 258-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label4 = GUICtrlCreateLabel("Label4", 5, 258, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label5Border = GUICtrlCreateLabel("", 5-2, 324-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label5 = GUICtrlCreateLabel("Label5", 5, 324, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label6Border = GUICtrlCreateLabel("", 5-2, 390-2, 600+4, $iAnswer_Height)
GUICtrlSetBkColor(-1, 0x800000)
$Label6 = GUICtrlCreateLabel("Label6", 5, 390, 600, 60, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Select random question
$iSelect = Random(1, IniRead($sIniFile, "Total", "Total", "Error"), 1)

; Read in each element of the question/answer, size it and set it in the label
;                                 Question text                               Label width & height
GUICtrlSetData($Label2, _SizeText(IniRead($sIniFile, $iSelect, "Q", "Error"), $iGUI_Width+150, $iQuestion_Height))
GUICtrlSetData($Label3, _SizeText(IniRead($sIniFile, $iSelect, "1", "Error"), $iGUI_Width+150, $iAnswer_Height +12))
GUICtrlSetData($Label4, _SizeText(IniRead($sIniFile, $iSelect, "2", "Error"), $iGUI_Width+150, $iAnswer_Height +12))
GUICtrlSetData($Label5, _SizeText(IniRead($sIniFile, $iSelect, "3", "Error"), $iGUI_Width+150, $iAnswer_Height +12))
GUICtrlSetData($Label6, _SizeText(IniRead($sIniFile, $iSelect, "4", "Error"), $iGUI_Width+150, $iAnswer_Height +12))

$Form1_2 = GUICreate("Answer", 609, 470, 308, 317)
GUISetBkColor(0x000000)
$sAnswerBody = GUICtrlCreateLabel("Question Body", 8, 32, 593, 148,  BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetFont(-1, 74.5, 800, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("OK", 245, 300, 140, 70)

Question()

Func Question()
    GUISwitch($Form1_1)
    GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Label3Border
            $vAnswer = 1
            ExitLoop
        Case $Label4Border
            $vAnswer = 2
            ExitLoop
        Case $Label5Border
            $vAnswer = 3
            ExitLoop
        Case $Label6Border
            $vAnswer = 4
            ExitLoop
    EndSwitch
WEnd
GUISetState(@SW_HIDE)
    ; Check the result against the correct answer
$sResult = ( ($vAnswer = IniRead($sIniFile, $iSelect, "A", "Error")) ? ("Correct") : ("Wrong") )
Answer($sResult)
EndFunc

Func Answer($sResult)
    GUISwitch($Form1_2)
    GUISetState(@SW_SHOW)
    GUICtrlSetData($sAnswerBody, $sResult)
While 1
        $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ExitLoop
    EndSwitch
WEnd
GUISetState(@SW_HIDE)
If $sResult="Correct" Then Exit
Question()
EndFunc

Func _SizeText($sText, $iWidth, $iHeight)
    ; Size the text wrapping into the label
    $aSize = _StringSize($sText, 10, 800, 0, "MS Sans Serif", $iWidth - 20)
    ; Now add spacing lines if any are needed ahead of the text to get it near the centre of the label
    Return _StringRepeat(@CRLF, Int(($iHeight - $aSize[3]) / $aSize[1] / 2)) & $aSize[0]

    ; Internal working:
    ; - $iHeight - $aSize[3] is the space below the text
    ; - divide by line height to get number of blank lines below text
    ; - divide by 2 to get required space ABOVE text
    ; - Int makes sure space ABOVE text smaller than below
    ; - add required @CRLFs before text to "centre" it

EndFunc

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...