shaggy89

Change Color of text string

15 posts in this topic

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 change all text in a simple Edit control.

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

 

 

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

Share this post


Link to post
Share on other sites

M23 could you give an example how this would work im my situation

Share this post


Link to post
Share on other sites

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


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

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

Share this post


Link to post
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

So change all GuiEdit to GuiRtfEdit and then set colors?

Just would like to say a big thanks to all for help.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

   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

Share this post


Link to post
Share on other sites

shaggy89,

Please post the problem code or a reproducer.

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

Share this post


Link to post
Share on other sites

Bump

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

  • Similar Content

    • IAMK
      By IAMK
      Hello,
       
      I have a script which uses a GUI with two buttons. It gets shown and hidden., and pressing the buttons (including their hotkeys) highlights the button, making you able to press enter the next time.
      However, I don't want that.
       
      I've tried using WinActivate() to make enter do things on another program, but the enter is still tied to the AutoIT GUI (which still has the button highlighted).
       
      Here's the important part of my code:
      $MY_GUI = GUICreate("Check.exe", 400, 300, ((@DesktopWidth / 2) - 200), -500) WinSetOnTop($MY_GUI, "", 1) ;Keeps the window on the top. $Button1 = GUICtrlCreateButton("Pass (Ctrl+P)", 40, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $Button2 = GUICtrlCreateButton("Fail (Ctrl+F)", 240, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $GUIText = GUICtrlCreateLabel("", 20, 10, 360, 230) GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif") GUICtrlSetData($GUIText, Execute($stepMessage[$step])) $showGUI = "True" GUISetState(@SW_SHOW) While($showGUI = "True") $buttonPressed = GUIGetMsg() Switch $buttonPressed Case $Button1 ;Pass $pass = "True" $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) Case $Button2 ;Fail $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) While($message = "") $message = InputBox("Check.exe", "Please enter the reason the step failed." & @CRLF, "", "", 300, 200, ((@DesktopWidth / 2) - 150), -400) WEnd Return $message EndSwitch WEnd Local $pass = "False" ;Whether "Pass" or "Fail" was pressed. Local $failLog = xxxfunc($pass) ;Returns a message to log if the step failed. ##THIS CALLS THE CODE SNIPPET ABOVE## FileWrite($file, _Now() & ": Step " & $step & getStatus($pass) & "and took " & Ceiling(TimerDiff($timer) / 1000) & " seconds." & @CRLF) If($failLog <> "") Then FileWrite($file, "Logged comment: " & $failLog & @CRLF) EndIf WinActivate("[TITLE:ProgramName]") ;Hotkeys: HotKeySet("^{p}", "pressPass") HotKeySet("^{f}", "pressFail") Func pressPass() $buttonPressed = $Button1 EndFunc Func pressFail() $buttonPressed = $Button2 EndFunc  
      The GUI's button/hotkey pressing works correctly.
      The same WinActivate code works in another script of mine.
      Together, WinActivate is not take "Active" away from the GUI's last-pressed button. How do I fix this?
       
      Thank you in advance.
    • fatpig
      By fatpig
      Dear AutoIT Community.
      I am currently working on a program that will display lots of images in a scroll down GUI.
      I create the GUI based on the number of images, which works fine.
      But all images I place using GuiCtrlCreatePic above a threshold of around 32000 pixels (cant say exactly) are placed at negative locations and finally will reach 0 again
      and will then populate on top of the images placed there before.
      For $i = 1 To $Images[0] ; Get picture dimensions $hImage = _GDIPlus_ImageLoadFromFile($ImgRep & $Images[$i]) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_ImageDispose($hImage) ; Resize & place images in GUI $Pic[$i] = GUICtrlCreatePic($ImgRep & $Images[$i], $Spacer, Round($yPos, 0), $MaxImgWidth, $MaxImgWidth * ($iY / $iX)) $yPos = $yPos + $MaxImgWidth * ($iY / $iX) + $Spacer $log = $log & "|" & $yPos $currentlut = $Images[1] Next I have found a limit at 32767 pixels for some GUI elements when googling around.
      Can someone tell me how to circumvent that?
      thanks!
      cheers
      Felix
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      in need the path/text present int above shown input box to be copied or retrieved into a variable .. how can i do that?
       
      Thanks