Jump to content

Recommended Posts

Posted (edited)

Hi,

A long time ago i was looking for a way to set color/font for seperate parts inside label control. I even posted a Feature request about this issue, but back then i quickly realized (well, actualy it's Valik ho told me :D ) that this would be almost impossible to do (for my anyway).

But today i found a tricky way to do that - I managed to build a function, that is kind of html-tag parser. It creates a label control for each text data that is wrapped with <font> tag (or not, but then the control is not formatted).

Function Header:

; #FUNCTION# ====================================================================================================
; Name...........:  _GUICtrlTFLabel_Create
; Description....:  Creates Text Formatted Label control(s).
; Syntax.........:  _GUICtrlTFLabel_Create($sData, $iLeft, $iTop [, $iWidth = -1 [, $iHeight = -1 [, $nStyle = -1 [, $nExStyle = -1 ]]]])
;
; Parameters.....:  $sData    - Formatted data. To set formatted data use <font></font> tag for data string.
;                              * This tag supports the following parameters (when used, they *can* be wrapped with quotes):
;                                   Color   - Text *Color* of data between the tags.
;                                   Size    - Text *Size*.
;                                   Weight  - Text *Weight* (the same values as used in GUICtrlSetFont()).
;                                   Attrib  - Text *Attributes* - The same values as used in GUICtrlSetFont(),
;                                                or supported strings combined together: i(talic), u(nderlined), s(trike).
;                                   Name    - Text font *Name* (the same values as used in GUICtrlSetFont()).
;                                   Style   - Label control’s Style (applies for partial data between the tags).
;                                   ExStyle - Label control’s ExStyle (applies for partial data between the tags).
;                                   Cursor  - Label control’s Cursor (can be cursor IDs, or strings, see description for MouseGetCursor).
;                                   Top     - Top position correction (relative to the global $iTop parameter).
;                                             This is designed to avoid text corruption when using different font names/text's size.
;
;                   $iLeft    - Left position (starting point in case when <font> tags are used) of label controls.
;                   $iTop     - Top position of label controls.
;                   $iWidth   - [Optional] Width of label control - Not used when <font> tags found in the data.
;                   $iHeight  - [Optional] Height of label control - Not used when <font> tags found in the data.
;                   $nStyle   - [Optional] (Forced) Style for entire label controls. Can be overridden by local Style parameter.
;                   $nExStyle - [Optional] (Forced) ExStyle for entire label controls. Can be overridden by local ExStyle parameter.
;                   
; Return values..:  Success   - Returns array of identifiers (Control IDs) of new created label controls.
;                   Failure   - Returns 0.
; Author.........:  G.Sandler (a.k.a MrCreatoR)
; Modified.......:  
; Remarks........:  
; Related........:  
; Link...........:  http://www.autoitscript.com/forum/index.php?showtopic=96986
; Example........:  Yes.
; ===============================================================================================================

Example - Formatted Labels Editor:

#include <GUIConstantsEx.au3>
#include <GUIEdit.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>

#include "GUITFLabel.au3"

Global $iEdit_Changed = 0, $aLabel_Ctrls

$hGUI = GUICreate("Formatted Labels Editor", 650, 400)

#Region Formate text panel

GUICtrlCreateLabel("Size:", 10, 8, -1, 15)
$nSize_Combo = GUICtrlCreateCombo("", 40, 5, 55, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|8|8.5|9|10|11|12|14|16|18|20|22|24|26|28|36|48|72", "None")

GUICtrlCreateLabel("Weight:", 100, 8, -1, 15)
$nWeight_Combo = GUICtrlCreateCombo("", 140, 5, 55, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|200|400|600|800|1000", "None")

GUICtrlCreateLabel("Attrib:", 10, 33, -1, 15)
$nAttrib_Combo = GUICtrlCreateCombo("", 40, 30, 155, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|italic|underlined|strike|italic+underlined+strike|italic+underlined|italic+strike|underlined+strike", "None")

GUICtrlCreateLabel("Name:", 230, 15, 50, 15)
$nName_Combo = GUICtrlCreateCombo("", 230, 30, 160, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|Arial|Comic Sans Ms|Tahoma|Times|Georgia|Lucida Sans Unicode|Verdana|Times New Roman|Courier New", "None")

GUICtrlCreateLabel("Color:", 400, 15, 50, 15)
$nColor_Combo = GUICtrlCreateCombo("", 400, 30, 60, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|Red|Green|Blue|Yellow|Orange|Gray|Brown|White", "None")

GUICtrlCreateLabel("Bk Color:", 470, 15, 50, 15)
$nBkColor_Combo = GUICtrlCreateCombo("", 470, 30, 60, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|Red|Green|Blue|Yellow|Orange|Gray|Brown|White", "None")

GUICtrlCreateLabel("Cursor:", 540, 15, 50, 15)
$nCursor_Combo = GUICtrlCreateCombo("", 540, 30, 100, 20, BitOr($GUI_SS_DEFAULT_COMBO, $CBS_DROPDOWNLIST))
GUICtrlSetData(-1, "None|POINTING|APPSTARTING|ARROW|CROSS|HELP|IBEAM|ICON|NO|SIZE|SIZEALL|SIZENESW|SIZENS|SIZENWSE|SIZEWE|UPARROW|WAIT|HAND", "None")

#EndRegion Formate text panel

GUICtrlCreateGroup("Select text and then select the formatting parameters from the above panel:", 10, 60, 630, 150)

$nSrcText_Edit = GUICtrlCreateEdit("", 20, 80, 610, 120, $ES_NOHIDESEL)

;GUICtrlCreateLabel("Original text:", 20, 75, -1, 15)
;$nSrcText_Edit = GUICtrlCreateEdit("", 20, 90, 610, 40, $ES_NOHIDESEL)

;GUICtrlCreateLabel("Formatted text:", 20, 140, -1, 15)
;$nFormattedText_Edit = GUICtrlCreateEdit("", 20, 155, 610, 40, BitOR($ES_NOHIDESEL, $ES_READONLY))

GUICtrlCreateGroup("Preview:", 10, 230, 630, 130)
GUICtrlSetFont(-1, 10, 800)

$nClose_Button = GUICtrlCreateButton("Close", 10, 370, 60, 20)
$nCopy_Button = GUICtrlCreateButton("Copy", 90, 370, 60, 20)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState(@SW_SHOW, $hGUI)

While 1
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $nClose_Button
            Exit
        Case $nSize_Combo, $nWeight_Combo, $nAttrib_Combo, $nName_Combo, $nColor_Combo, $nBkColor_Combo, $nCursor_Combo
            Local $sParam = StringLower(StringRegExpReplace(GUICtrlRead($nMsg - 1), 'h+|:', ''))
            Local $sValue = GUICtrlRead($nMsg)
            
            _SetFormattedText_Proc($sParam, $sValue)
        Case $nCopy_Button
            Local $sText = GUICtrlRead($nSrcText_Edit)
            
            If $sText <> "" Then
                ClipPut($sText)
            EndIf
    EndSwitch
    
    If $iEdit_Changed Then
        $iEdit_Changed = 0
        
        For $i = 1 To UBound($aLabel_Ctrls)-1
            GUICtrlDelete($aLabel_Ctrls[$i])
        Next
        
        $aLabel_Ctrls = _GUICtrlCreateTFLabel(GUICtrlRead($nSrcText_Edit), 20, 245, 610, 110)
    EndIf
WEnd

Func _SetFormattedText_Proc($sParam, $sValue)
    If $sParam = "attrib" And Not StringRegExp($sValue, '(?i)A(None)?z') Then
        $aSplit = StringSplit($sValue, "+")
        $sValue = ""
        
        For $i = 1 To $aSplit[0]
            $sValue &= StringLeft($aSplit[$i], 1)
            
            If $i < $aSplit[0] Then
                $sValue &= "+"
            EndIf
        Next
    EndIf
    
    Local $sSelectionData = ControlCommand($hGUI, "", $nSrcText_Edit, "GetSelected")
    Local $sAddParamValue = ' ' & $sParam & '="' & $sValue & '"'
    
    If $sSelectionData = '' Then
        Return
    EndIf
    
    If StringRegExp($sSelectionData, '(?i)<font.*>.*</font>') Then
        $sSelectionData = StringRegExpReplace($sSelectionData, '(?i)(<font.*)( ' & $sParam & '=".*?")(.*>.*</font>)', '13')
        
        If Not StringRegExp($sValue, '(?i)A(None)?z') Then
            $sSelectionData = StringRegExpReplace($sSelectionData, '(?i)(<font.*)(>.*</font>)', '1' & $sAddParamValue & '2')
        EndIf
        
        If StringRegExp($sSelectionData, '(?i)<fonth*>.*</font>') Then
            $sSelectionData = StringRegExpReplace($sSelectionData, '(?i)<font.*>(.*)</font>', '1')
        EndIf
    ElseIf $sAddParamValue <> '' Then
        $sSelectionData = '<font' & $sAddParamValue & '>' & $sSelectionData & '</font>'
    EndIf
    
    _GUICtrlEdit_ReplaceSel($nSrcText_Edit, $sSelectionData)
    Local $iStart = StringInStr(GUICtrlRead($nSrcText_Edit), $sSelectionData)-1
    Local $iEnd = $iStart + StringLen($sSelectionData)
    GUICtrlSendMsg($nSrcText_Edit, $EM_SETSEL, $iStart, $iEnd)
EndFunc

Func WM_COMMAND($hWnd, $nMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0xFFFF)
    Local $hCtrl = $lParam
    
    Switch $nID
        Case $nSrcText_Edit
            Switch $nNotifyCode
                Case $EN_CHANGE, $EN_UPDATE
                    $iEdit_Changed = 1
            EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc

Screenshots:

  Reveal hidden contents
Posted Image Posted Image

Posted Image

Posted Image

History version:

  Reveal hidden contents

Attachments:

v1.5 GUITFLabel_UDF.zip

v1.4 _GUICtrlCreateTFLabel_UDF.zip

v1.3 _GUICtrlCreateTFLabel_UDF.zip

v1.2 _GUICtrlCreateTFLabel_UDF.zip(Edited version - Previous downloads: 3 :D)

v1.1 _GUICtrlCreateTFLabel_UDF.zip

v1.0 _GUICtrlCreateTFLabel_UDF.zip

Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

you could probably change this so that instead of html tags, it can be done in arrays. like

$labeltext1[2][8] = [["text", "color", "size", "weight", "attrib", "fontname", "style", "exstyle"], _
["text", "color", "size", "weight", "attrib", "fontname", "style", "exstyle"]]

[font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap

Posted

Thanks to all for the feedbacks!

  billthecreator said:

you could probably change this so that instead of html tags, it can be done in arrays.

This exactly idea i used in the first version, but i thought that there is should be more convinient usage method, so the user will not have to create bunch of complicated arrays and pass them to the function.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

  MrCreatoR said:

Thanks to all for the feedbacks!

This exactly idea i used in the first version, but i thought that there is should be more convinient usage method, so the user will not have to create bunch of complicated arrays and pass them to the function.

Nice one CreatoR

I agree, This concept is easier than the Dim process required to use they array. And the visual use of the "&" makes it clearer too!

$sLabel2_Data = _
    '<font top="1">and</font> ' & _
    '<font color="0x0000FF" size="9" weight="800">Few</font> ' & _
    '<font color="0xFF8000" size="9" weight="800" style="' & BitOr(...etc) & '">&more&</font> ' & _
    '<font color="0x000080" size="12" weight="800" top="-2.5" name="Georgia">strings</font> data.'

8)

NEWHeader1.png

Posted

  nikink said:

Nice! Can you make it work for Buttons too? :D

Only like this perhaps:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUICtrlCreateTFLabel.au3>
;

$hGUI = GUICreate("_GUICtrlCreateTFLabel Example", 300, 200)

$a3DfaceColor = DllCall("User32.dll", "int", "GetSysColor", "int", 15) ;$COLOR_3DFACE
$n3DfaceColor = BitAND(BitShift(String(Binary($a3DfaceColor[0])), 8), 0xFFFFFF) ;RGB2BGR

$sLabel_Data = _
    '<font color="0x0000FF" bkcolor="' & $n3DfaceColor & '" size="9" weight="800">My </font>' & _
    '<font color="0xFF0000" bkcolor="' & $n3DfaceColor & '" size="9" weight="800">Button</font>'

$aLabel_Ctrls = _GUICtrlCreateTFLabel($sLabel_Data, 20, 50)

$nButton = GUICtrlCreateButton("", 12, 45, 70, 25, $WS_CLIPSIBLINGS)
GUICtrlSetBkColor(-1, $n3DfaceColor)

GUISetState(@SW_SHOW, $hGUI)

While 1
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aLabel_Ctrls[1] To $aLabel_Ctrls[$aLabel_Ctrls[0]]
            $aCurInfo = GUIGetCursorInfo($hGUI)
            
            While $aCurInfo[2] = 1
                Sleep(10)
                $aCurInfo = GUIGetCursorInfo($hGUI)
            WEnd
            
            If $aCurInfo[4] = $nMsg Then ControlClick($hGUI, "", $nButton)
        Case $nButton
            MsgBox(64, 'Title', 'Button pressed')
    EndSwitch
WEnd

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

UDF Updated...

History version:

  Quote

v1.1

+ Added bkcolor parameter to <font> tag.

+ Added state parameter to <font> tag.

+ Added "string color" recognition to bk/color parameter for <font> tag. Supporting 140 known color strings.

+ Added example with formatted labels on button control.

* Now the parameters in <font> tag do not have to be wrapped with quotes.

v1.0

* First release.

Please see the first post.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

UDF Updated!

History Version:

  Quote

v1.2

+ Added "string attribute" recognition to "attrib" parameter for <font> tag.

Supported: i(talic) (2), u(nderlined) (4), s(trike) (8).

+ Added support for html-like colors format (#FF0000) in "color" parameter.

+ The "color" parameter in <font> tag can accept now RGB shortcut color, not just RrGgBb.

+ The "color" parameter in <font> tag can also accept now color with no prefix (# or 0x).

v1.1

+ Added bkcolor parameter to <font> tag.

+ Added state parameter to <font> tag.

+ Added "string color" recognition to bk/color parameter for <font> tag. Supporting 140 known color strings.

+ Added example with formatted labels on button control.

* Now the parameters in <font> tag do not have to be wrapped with quotes.

v1.0

* First release.

Please see the first post.

Edited by MrCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

How can i read data or change data of these labels? Even how to delete it?

Edit:

I Read and Delete use this:

$label = _GUICtrlCreateTFLabel($sLabel1_Data, 20, 20)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                For $i = 1 To UBound($label) - 1
                    $msg = $msg & "" & GUICtrlRead($label[$i])
                Next
                    MsgBox(0,"",$msg)
                    ;$label = _GUICtrlCreateTFLabel($sLabel2_Data, 20, 20)
                For $i = 1 To UBound($label) - 1
                    GUICtrlDelete($label[$i])
                Next
wend

Other problem: I cannot get label transparent with this.

Edited by nguyenbason
UnderWorldVN- Just play the way you like it
Posted
  Quote

I cannot get label transparent with this.

It's should be transparent by default... and you can also use GUICtrlSetBkColor($aLabel[$i], $GUI_BKCOLOR_TRANSPARENT).

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • 11 months later...
Posted

Update!

  Quote

v1.3

+ Added "cursor" parameter in <font> tag, wich supports numeric IDs and string values (POINTING, ARROW, CROSS, etc.), example:

<font attrib="underlined" cursor="POINTING">Hyperlink</font>

+ Added "Formatted Labels Editor", allows to use the library much easier - now the formatted labels can be generated visualy (thanks to SmOke_N for the idea).

* Fixed wrong charset usage, was causing adding of extra lenght to the formatted labels when using several fonts.

* Now the unformatted label data created with the font that currently used in the GUI (by GUISetFont).

* Cosmetic changes in the code.

Please check the first post.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • Moderators
Posted (edited)

  On 6/13/2010 at 12:56 PM, 'MrCreatoR said:

Update!

Please check the first post.

I like the edit GUI very much. Good job.

Edit:

Let me offer a suggestion.

Make two edits. One that has the character output, and one that has the normal text that you type. That way you don't have to dig through all the <start><end> stuff while editing.

So:

First edit control would have the text I typed. All options I highlight here and change show up in the second edit control.

Second edit control would have the output for me to copy.

And your label would remain the same showing the text in it's display state.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted

  On 6/13/2010 at 9:38 PM, 'SmOke_N said:

I like the edit GUI very much. Good job.

Thanks.

  On 6/13/2010 at 9:38 PM, 'SmOke_N said:

Make two edits. One that has the character output, and one that has the normal text that you type. That way you don't have to dig through all the <start><end> stuff while editing.

So:

First edit control would have the text I typed. All options I highlight here and change show up in the second edit control.

Second edit control would have the output for me to copy.

And your label would remain the same showing the text in it's display state.

To be honest i don't understand the purpose of this, what for we need this?

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

P.S

And i am not sure how to realize this...

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
  • Recently Browsing   0 members

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