Jump to content

Change Color of text string


Recommended Posts

HI all,
        Just looking for a way that i can change the color of a string of text

e.g

$text "I need to be the color red"

simple im sure but i cant seem to find it.

 

the help file says

Local $iRedColor = _ColorGetRed(0x8080ff)

so i tried

$text =($iRedColor, "I need to be the color red")

any help would be great

 

Thanks

Shaggs

Link to comment
Share on other sites

Where do you need to change the color? In a GUI, a Word document, somewhere else?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Where do you need to change the color? In a GUI, a Word document, somewhere else?

will be used to display what the user has typed.

 

so user would type in text via a gui using GUICtrlCreateInput and gets passed into the  GUICtrlCreateEdit so that would be displayed in back

but when a user presses a button i wanted a pre-canned message displayed in red in the GUICtrlCreateEdit field

Link to comment
Share on other sites

  • Moderators

shaggy89,

If you  want only specific lines to be displayed in red then you will need to use a RichEdit control - GUICtrlSetColor will change all text in a simple Edit control.

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

 

 

shaggy89,

If you  want only specific lines to be displayed in red then you will need to use a RichEdit control - GUICtrlSetColor will changed all text in a simple Edit control.

M23

M23 you beat me to the puch. I looked at help file and @jguinch the help file says "

The control identifier (controlID) as returned by a GUICtrlCreate...() function, or -1 for the last created control."

as you can see mine is a pre made string

 

will look Into rich edit Shortly

 

cheers

Link to comment
Share on other sites

  • Moderators

shaggy98,

I use this function to write to RichEdit controls as it allows you to adjust the font size and attributes as well as the colour - here we leave the size and attributes as default and only adjust the colour:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiRichEdit.au3>

$hGui = GUICreate("RichEdit Test", 320, 350)

$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

GUISetState()

Sleep(1000)

; Write in black
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am in BLACK" & @CRLF, Default, Default, 0x000000)

Sleep(2000)

; Write in red
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am in RED" & @CRLF, Default, Default, 0xFF0000)

Sleep(2000)

; And back to black
_GUICtrlRichEdit_WriteLine($hRichEdit, "I am back in BLACK" & @CRLF, Default, Default, 0x000000)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            Exit
    EndSwitch



WEnd



Func _GUICtrlRichEdit_WriteLine($hWnd, $sText, $iIncrement = 0, $sAttrib = "", $iColor = -1)

    ; Count the @CRLFs
    StringReplace(_GUICtrlRichEdit_GetText($hWnd, True), @CRLF, "")
    Local $iLines = @extended
    ; Adjust the text char count to account for the @CRLFs
    Local $iEndPoint = _GUICtrlRichEdit_GetTextLength($hWnd, True, True) - $iLines

    ; Add new text
    _GUICtrlRichEdit_AppendText($hWnd, $sText & @CRLF)
    ; Select text between old and new end points
    _GuiCtrlRichEdit_SetSel($hWnd, $iEndPoint, -1)
    ; Convert colour from RGB to BGR
    $iColor = Hex($iColor, 6)
    $iColor = '0x' & StringMid($iColor, 5, 2) & StringMid($iColor, 3, 2) & StringMid($iColor, 1, 2)
    ; Set colour
    If $iColor <> -1 Then _GuiCtrlRichEdit_SetCharColor($hWnd, $iColor)
    ; Set size
    If $iIncrement <> 0 Then _GUICtrlRichEdit_ChangeFontSize($hWnd, $iIncrement)
    ; Set weight
    If $sAttrib <> "" Then _GUICtrlRichEdit_SetCharAttributes($hWnd, $sAttrib)
    ; Clear selection
    _GUICtrlRichEdit_Deselect($hWnd)

EndFunc

Please ask if you have any further questions.

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

shaggy89,

Another way to slice that banana...(1st time using rich edit so beware)

#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GuiRichEdit.au3>
#include <array.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $gui010 = GUICreate('RichEdit Example')
Local $aSize = WinGetClientSize($gui010)
Local $lbl010 = GUICtrlCreateLabel('Enter Your Data', 20, 20, 100, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
Local $inp010 = GUICtrlCreateInput('', 130, 20, 250, 20)
Local $edt010 = _GUICtrlRichEdit_Create($gui010, '', 20, 50, 360, 310, _
        BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY))
Local $btn010 = GUICtrlCreateButton('Button for the User to Press', 20, $aSize[1] - 30, $aSize[0] - 40, 20)
GUICtrlSetState($inp010, $GUI_FOCUS)
GUISetState()

Local $message = '>>>>> My Pre-Canned Message <<<<<' & @CRLF

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            _GUICtrlRichEdit_Destroy($edt010)
            Exit
        Case $btn010
            _GUICtrlRichEdit_SetCharColor($edt010, 0x000000ff)      ;   set color red (BBGGRR format)
            _GUICtrlRichEdit_SetFont($edt010, 10)                   ;   set size
            _GUICtrlRichEdit_SetCharAttributes($edt010, '+bo')      ;   set bold
            _GUICtrlRichEdit_AppendText($edt010, $message)          ;   write line
            GUICtrlSetState($inp010, $GUI_FOCUS)
        Case $inp010
            _GUICtrlRichEdit_AppendText($edt010, GUICtrlRead($inp010) & @CRLF)
            GUICtrlSetState($inp010, $GUI_FOCUS)
            GUICtrlSetData($inp010, '')
    EndSwitch

WEnd

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

   hi all,   

Ive been trying to get RichText working but am having an issue.

If I use

Func Print($txtMsg)
     _GUICtrlRichEdit_AppendText($hEdit, @CRLF&$txtMsg)
EndFunc ;==> Print

nothing works but if I remove the

@CRLF

the message is displayed but not on a new line. what am i missing ?

whole script

 

#cs ----------------------------------------------------------------------------

 DELmE's LanChat

#ce ----------------------------------------------------------------------------

;include files
#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>
#include <GuiRichEdit.au3>
;define variables
Global $hGui, $hEdit, $hInp, $hBtn, $username, $rsock, $ssock, $recv
Global $port = 7892
Global $Broadcast = StringLeft(@IPAddress1, StringInStr(@IPAddress1, ".", 0, 3)) & "255"

;start main function
_main()
Func _main()
    ;get username
    $username = @UserName
    ;create gui
    $hGui = GUICreate("LanChat and Assist",585,503)
    $hEdit = _GUICtrlRichEdit_Create($hGui,"Welcome to LanChat and Assist",0,0,500,480,BitOR($ES_READONLY,$ES_WANTRETURN,$WS_VSCROLL,$ES_AUTOVSCROLL))
    _GUICtrlEdit_SetLimitText($hEdit,99999999999999)
    $hInp = GUICtrlCreateInput("",0,480,470,20)
    $hBtn = GUICtrlCreateButton("Send",470,480,30,20,$BS_DEFPUSHBUTTON)
    $AFK = GUICtrlCreateButton("AFK", 505, 50, 75, 25,$BS_DEFPUSHBUTTON)
    $bak = GUICtrlCreateButton("BAK", 505, 100, 75, 25,$BS_DEFPUSHBUTTON)
    $exit= GUICtrlCreateButton("Close", 505, 470, 75, 25,$BS_DEFPUSHBUTTON)
    ;display gui
    GUISetState()
    ;start winsock for udp
    UDPStartup()
    ;bind a socket to the port for listening
    $rsock = UDPBind(@IpAddress1,$port)
    If @error <> 0 Then Exit
    ;send message informing of online status
    SendMsg("-"&$username&" has come online.")
    ;start gui loop
    Do
        $recv = UDPRecv($rsock,1024)
        If $recv <> "" Then
            Print($recv)
        EndIf
        $msg = GUIGetMsg()
        If $msg = $hBtn Then
            $buffer = GUICtrlRead($hInp)
            If $buffer <> "" Then
                GUICtrlSetData($hInp,"")
                SendMsg("<"&$username&">: "&$buffer)
            EndIf
         EndIf
         If $msg = $AFK Then
            $buffer = "AFK " ; make red
            If $buffer <> "" Then
                GUICtrlSetData($hInp,"")
                SendMsg("<"&$username&">: "&$buffer)
             EndIf
          EndIf
          If $msg = $bak Then
            $buffer = "BAK " ; make green
            If $buffer <> "" Then
                GUICtrlSetData($hInp,"")
                SendMsg("<"&$username&">: "&$buffer)
             EndIf
          EndIf
          EndIf
          If $msg = $exit Then
             $msg = $GUI_EVENT_CLOSE
             EndIf
    Until $msg = $GUI_EVENT_CLOSE
    Exit
EndFunc ;==> _main

Func SendMsg($txtMsg)
        $ssock = UDPOpen($Broadcast,$port)
        If @error = 0 Then
            UDPSend($ssock,$txtMsg)
        EndIf
        UDPCloseSocket($ssock)
EndFunc ;==> SendMsg

Func Print($txtMsg)
     _GUICtrlRichEdit_AppendText($hEdit, @CRLF&$txtMsg)
EndFunc ;==> Print

Func OnAutoItExit()
    ;send message informing of offline status
    SendMsg("-"&$username&" has gone offline.")
    UDPShutdown()
EndFunc ;==> OnAutoItExit

 

Edited by shaggy89
Now on a PC
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

×
×
  • Create New...