Jump to content

_GUICtrlRichEdit_AppendTextEx - Easily write colored text to RichEdit


corgano
 Share

Recommended Posts

This is a simple func I made to write colored text to a rich edit, and I thought someone else might find it useful. What it does is format the text into RTF syntax so you don't need to mess with selecting and setting the color and such with multiple commands.

; _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $underline=0, $strike=0)
;   This function was created to make it simpler to use RichEdit controls.
;
;   Note: to set the line spacing to a size bigger than the text, 
;   you need to call this function once to write the text, and then call
;   it again and write a space with a larger size, and that will give you
;   spacing between the lines.
;
;Peramiters
;   $RichEdit = handle of RichEdit control
;   $text = the string to write. You need to add @CRLF for a newline
;   $font = the font family to use, default = "Arial"
;   $color = the rrggbb hex color code to use, default = "000000" (black)
;   $size = the font size to use in points, will be rounded to the nearest 0.5 points before use, default = 12
;   $bold = flag to make the text bold, default = 0 (not bold)
;   $italic = flag to make the text italic, default = 0 (not italic)
;   $strike = flag to make the text strikethrough, default = 0
;   $underline = int, what kind of underlining to use. default = 0
;       1 = Underline
;       2 = Double Underline
;       3 = Thick Underline
;       4 = Underline words only
;       5 = Wave Underline
;       6 = Dotted Underline
;       7 = Dash Underline
;       8 = Dot Dash Underline
;   
;Return value
;   On success: Returns the value from _GUICtrlRichEdit_AppendText()
;   On failure: Sets @error to non-0
;       1 = Error with color
;
Func _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $strike=0, $underline=0)
  Local $command = "{\rtf1\ansi"
  Local $r, $g, $b, $ul[9] = ["8", '\ul', '\uldb', '\ulth', '\ulw', '\ulwave', '\uld', '\uldash', '\uldashd']

  If $font <> "" Then $command &= "{\fonttbl\f0\f"&$font&";}"
  If $color <> "" Then
    If StringLen($color) <> 6 And StringLen($color) = 8 Then Return SetError(1)
    $b = dec(StringRight($color,2))
    if @error Then seterror(1, 1)
    $color = StringTrimRight($color,2)
    $g = dec(StringRight($color,2))
    if @error Then seterror(1, 2)
    $color = StringTrimRight($color,2)
    $r = dec(StringRight($color,2))
    if @error Then seterror(1, 3)
    If $r+$b+$g > 0 Then
      $command &= "{\colortbl;\red"&$r&"\green"&$g&"\blue"&$b&";}\cf1"
    EndIf
  EndIf
 
  If $size Then $command &= "\fs"&round($size*2)&" "
  If $strike Then $command &= "\strike "
  If $italic Then $command &= "\i "
  If $bold Then $command &= "\b "
  If $underline > 0 and $underline < 9 Then $command &= $ul[$underline]&" "
;~   ConsoleWrite($command&$text&"}"&@CRLF) ; Debugging line
  Return _GUICtrlRichEdit_AppendText($RichEdit, $command&StringReplace($text,@CRLF,"\line")&"}" )
EndFunc

An example of it's use:

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

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 331, 303, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 240, 313, 21)
$ButtonBob = GUICtrlCreateButton("Bob", 8, 264, 75, 25)
$ButtonMar = GUICtrlCreateButton("Mary", 88, 264, 75, 25)
$ButtonSus = GUICtrlCreateButton("Susan", 168, 264, 75, 25)
$ButtonJoe = GUICtrlCreateButton("Joe", 248, 264, 75, 25)
$RichEdit = _GUICtrlRichEdit_Create($Form1, "", 8, 8, 313, 225, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $lastuser = ""
demo()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    Case $ButtonBob
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Bob", "993333", GUICtrlRead($Input1))
    Case $ButtonJoe
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Joe", "339933", GUICtrlRead($Input1))
    Case $ButtonSus
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Susan", "333399", GUICtrlRead($Input1))
    Case $ButtonMar
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Mary", "339999", GUICtrlRead($Input1))

    EndSwitch
WEnd



Func demo()
  _Chat_SendMessage("Bob", "993333", "Testin 123")
  _Chat_SendMessage("Bob", "993333", "Example script here")
  _Chat_SendMessage("Susan", "333399", "Enter something in the input and press a button")
  _Chat_SendMessage("Susan", "333399", "Try experimenting with it a bit")
  _Chat_SendMessage("Mary", "339999", "Messages between the same user are less spaced out than between other people")
EndFunc


;use a function for wrapping messages with multiple parts of different formattings
Func _Chat_SendMessage($user, $color, $text)
  Local $temp = ControlGetFocus($Form1)
  _GUICtrlRichEdit_AppendTextEx($RichEdit, $user&":", "Arial", $color, 10, 1)
  If $lastuser <> $user and $lastuser <> "" Then
    ; Add line spacing between users by using a taller font size for the space
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 15)
  Else
    ; Same width as 15, but only as tall as 10 :P
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 10)
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 5)
  EndIf
  _GUICtrlRichEdit_AppendTextEx($RichEdit, $text&@CRLF, "Arial", "000000", 10)
  $lastuser = $user
  ControlFocus($Form1, "", $temp)
EndFunc


; _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $underline=0, $strike=0)
;   This function was created to make it simpler to use RichEdit controls.
;
;   Note: to set the line spacing to a size bigger than the text,
;   you need to call this function once to write the text, and then call
;   it again and write a space with a larger size, and that will give you
;   spacing between the lines.
;
;Peramiters
;   $RichEdit = handle of RichEdit control
;   $text = the string to write. You need to add @CRLF for a newline
;   $font = the font family to use, default = "Arial"
;   $color = the rrggbb hex color code to use, default = "000000" (black)
;   $size = the font size to use in points, will be rounded to the nearest 0.5 points before use, default = 12
;   $bold = flag to make the text bold, default = 0 (not bold)
;   $italic = flag to make the text italic, default = 0 (not italic)
;   $strike = flag to make the text strikethrough, default = 0
;   $underline = int, what kind of underlining to use. default = 0
;       1 = Underline
;       2 = Double Underline
;       3 = Thick Underline
;       4 = Underline words only
;       5 = Wave Underline
;       6 = Dotted Underline
;       7 = Dash Underline
;       8 = Dot Dash Underline
;
;Return value
;   On success: Returns the value from _GUICtrlRichEdit_AppendText()
;   On failure: Sets @error to non-0
;       1 = Error with color
;
Func _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $strike=0, $underline=0)
  Local $command = "{\rtf1\ansi"
  Local $r, $g, $b, $ul[9] = ["8", '\ul', '\uldb', '\ulth', '\ulw', '\ulwave', '\uld', '\uldash', '\uldashd']

  If $font <> "" Then $command &= "{\fonttbl\f0\f"&$font&";}"
  If $color <> "" Then
    If StringLen($color) <> 6 And StringLen($color) = 8 Then Return SetError(1)
    $b = dec(StringRight($color,2))
    if @error Then seterror(1, 1)
    $color = StringTrimRight($color,2)
    $g = dec(StringRight($color,2))
    if @error Then seterror(1, 2)
    $color = StringTrimRight($color,2)
    $r = dec(StringRight($color,2))
    if @error Then seterror(1, 3)
    If $r+$b+$g > 0 Then
      $command &= "{\colortbl;\red"&$r&"\green"&$g&"\blue"&$b&";}\cf1"
    EndIf
  EndIf

  If $size Then $command &= "\fs"&round($size*2)&" "
  If $strike Then $command &= "\strike "
  If $italic Then $command &= "\i "
  If $bold Then $command &= "\b "
  If $underline > 0 and $underline < 9 Then $command &= $ul[$underline]&" "
;~   ConsoleWrite($command&$text&"}"&@CRLF) ; Debugging line
  Return _GUICtrlRichEdit_AppendText($RichEdit, $command&StringReplace($text,@CRLF,"\line")&"}" )
EndFunc

If this was useful to you, or if you have any ideas / suggestions to make it better, let me know.

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

  • 11 months later...

I hate to revive an old post, but I was using your UDF, but I can't seem to change the font.

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

$hGUI = GUICreate("Example", 400, 100)
$GUI_Debug = _GUICtrlRichEdit_Create($hGUI, "", 0, 0, 400, 100, BitOR($WS_HSCROLL, $WS_VSCROLL, $ES_READONLY, $ES_MULTILINE))

GUISetState(@SW_SHOW)

_GUICtrlRichEdit_AppendTextEx($GUI_Debug, "The Quick brown fox jumped over the lazy dog." & @CRLF, "Arial", "000000", 12)
_GUICtrlRichEdit_AppendTextEx($GUI_Debug, "The Quick brown fox jumped over the lazy dog." & @CRLF, "Courier", "FF0000", 12)


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

When run, I get two lines, one black, one red, but both are the same font. 

Debug output looks like:

{\rtf1\ansi{\fonttbl\f0\fArial;}\fs24 The Quick brown fox jumped over the lazy dog.
}
{\rtf1\ansi{\fonttbl\f0\fCourier;}{\colortbl;\red255\green0\blue0;}\cf1\fs24 The Quick brown fox jumped over the lazy dog.
}

Any thoughts?

Thanks in advance.

Edited by dmlarsen30
added debug output
Link to comment
Share on other sites

  • 4 weeks later...

Hi, I was really happy, finding this function! thx!

However, when I use it, whatever I do, everything is written on the same line

_GUICtrlRichEdit_AppendTextEx($LogBox,$sDatas&@CRLF,"",$color)

If I copy the code of your example, it works fine.

What is wrong ?

 

 

FIXED :

There was missing

$ES_MULTILINE

inside the

_GUICtrlRichEdit_Create

 

Edited by Lokidor
Problem fixed
Link to comment
Share on other sites

  • 6 years later...

I am searching for ideas to extend the GuiRichEdit.au3

and so i tested your example. ;the function works good, for normal text. For Filelinks as text can't be solved in the moment from me for this func.

Please test this modified example;

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

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 331, 303, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 240, 313, 21)
$ButtonBob = GUICtrlCreateButton("Bob", 8, 264, 75, 25)
$ButtonMar = GUICtrlCreateButton("Mary", 88, 264, 75, 25)
$ButtonSus = GUICtrlCreateButton("Susan", 168, 264, 75, 25)
$ButtonJoe = GUICtrlCreateButton("Joe", 248, 264, 75, 25)
$RichEdit = _GUICtrlRichEdit_Create($Form1, "", 8, 8, 313, 225, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_READONLY))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $lastuser = ""
demo()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    Case $ButtonBob
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Bob", "993333", GUICtrlRead($Input1))
    Case $ButtonJoe
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Joe", "339933", GUICtrlRead($Input1))
    Case $ButtonSus
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Susan", "333399", GUICtrlRead($Input1))
    Case $ButtonMar
      If GUICtrlRead($Input1) <> "" Then _Chat_SendMessage("Mary", "339999", GUICtrlRead($Input1))

    EndSwitch
WEnd



Func demo()
  _Chat_SendMessage("Bob", "993333", "Testin 123")
  _Chat_SendMessage("Bob", "993333", "Example script here: C:\Users\autoBert\AutoIt\Downloaded\Test\_GUICtrlRichEdit_AppendTextEx.au3")
  _Chat_SendMessage("Susan", "333399", "Enter something in the input and press a button")
  _Chat_SendMessage("Susan", "333399", "Try experimenting with it a bit")
  _Chat_SendMessage("Mary", "339999", "Messages between the same user are less spaced out than between other people")
EndFunc


;use a function for wrapping messages with multiple parts of different formattings
Func _Chat_SendMessage($user, $color, $text)
  Local $temp = ControlGetFocus($Form1)
  _GUICtrlRichEdit_AppendTextEx($RichEdit, $user&":", "Arial", $color, 10, 1)
  If $lastuser <> $user and $lastuser <> "" Then
    ; Add line spacing between users by using a taller font size for the space
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 15)
  Else
    ; Same width as 15, but only as tall as 10 :P
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 10)
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " ", "Arial", "000000", 5)
  EndIf
  _GUICtrlRichEdit_AppendTextEx($RichEdit, $text&@CRLF, "Arial", "000000", 10)
  $lastuser = $user
  ControlFocus($Form1, "", $temp)
EndFunc

#cs
Found: https://www.autoitscript.com/forum/topic/173521-_guictrlrichedit_appendtextex-easily-write-colored-text-to-richedit/
Autor: Corgano  ;https://www.autoitscript.com/forum/profile/43194-corgano/
; _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $underline=0, $strike=0)
;   This function was created to make it simpler to use RichEdit controls.
;
;   Note: to set the line spacing to a size bigger than the text,
;   you need to call this function once to write the text, and then call
;   it again and write a space with a larger size, and that will give you
;   spacing between the lines.
;
;Peramiters
;   $RichEdit = handle of RichEdit control
;   $text = the string to write. You need to add @CRLF for a newline
;   $font = the font family to use, default = "Arial"
;   $color = the rrggbb hex color code to use, default = "000000" (black)
;   $size = the font size to use in points, will be rounded to the nearest 0.5 points before use, default = 12
;   $bold = flag to make the text bold, default = 0 (not bold)
;   $italic = flag to make the text italic, default = 0 (not italic)
;   $strike = flag to make the text strikethrough, default = 0
;   $underline = int, what kind of underlining to use. default = 0
;       1 = Underline
;       2 = Double Underline
;       3 = Thick Underline
;       4 = Underline words only
;       5 = Wave Underline
;       6 = Dotted Underline
;       7 = Dash Underline
;       8 = Dot Dash Underline
;
;Return value
;   On success: Returns the value from _GUICtrlRichEdit_AppendText()
;   On failure: Sets @error to non-0
;       1 = Error with color
;
#ce
Func _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $strike=0, $underline=0)
  Local $command = "{\rtf1\ansi"
  Local $r, $g, $b, $ul[9] = ["8", '\ul', '\uldb', '\ulth', '\ulw', '\ulwave', '\uld', '\uldash', '\uldashd']

  If $font <> "" Then $command &= "{\fonttbl\f0\f"&$font&";}"
  If $color <> "" Then
    If StringLen($color) <> 6 And StringLen($color) = 8 Then Return SetError(1)
    $b = dec(StringRight($color,2))
    if @error Then seterror(1, 1)
    $color = StringTrimRight($color,2)
    $g = dec(StringRight($color,2))
    if @error Then seterror(1, 2)
    $color = StringTrimRight($color,2)
    $r = dec(StringRight($color,2))
    if @error Then seterror(1, 3)
    If $r+$b+$g > 0 Then
      $command &= "{\colortbl;\red"&$r&"\green"&$g&"\blue"&$b&";}\cf1"
    EndIf
  EndIf

  If $size Then $command &= "\fs"&round($size*2)&" "
  If $strike Then $command &= "\strike "
  If $italic Then $command &= "\i "
  If $bold Then $command &= "\b "
  If $underline > 0 and $underline < 9 Then $command &= $ul[$underline]&" "
;~   ConsoleWrite($command&$text&"}"&@CRLF) ; Debugging line
  Return _GUICtrlRichEdit_AppendText($RichEdit, $command&StringReplace($text,@CRLF,"\line")&"}" )
EndFunc

I have only changed Text in the func demo. Your idea is good, so i have it integrated in the GuiRichEditPlus.au3.

Link to comment
Share on other sites

I found a solution (just escaping the text):

Func _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $bold=0, $italic=0, $strike=0, $underline=0)
  Local $command = "{\rtf1\ansi"
  Local $r, $g, $b, $ul[9] = ["8", '\ul', '\uldb', '\ulth', '\ulw', '\ulwave', '\uld', '\uldash', '\uldashd']
  $text = StringReplace($text, '\', '\\')
  If $font <> "" Then $command &= "{\fonttbl\f0\f"&$font&";}"
  If $color <> "" Then
    If StringLen($color) <> 6 And StringLen($color) = 8 Then Return SetError(1)
    $b = dec(StringRight($color,2))
    if @error Then seterror(1, 1)
    $color = StringTrimRight($color,2)
    $g = dec(StringRight($color,2))
    if @error Then seterror(1, 2)
    $color = StringTrimRight($color,2)
    $r = dec(StringRight($color,2))
    if @error Then seterror(1, 3)
    If $r+$b+$g > 0 Then
      $command &= "{\colortbl;\red"&$r&"\green"&$g&"\blue"&$b&";}\cf1"
    EndIf
  EndIf

  If $size Then $command &= "\fs"&round($size*2)&" "
  If $strike Then $command &= "\strike "
  If $italic Then $command &= "\i "
  If $bold Then $command &= "\b "
  If $underline > 0 and $underline < 9 Then $command &= $ul[$underline]&" "
;~   ConsoleWrite($command&$text&"}"&@CRLF) ; Debugging line
  Return _GUICtrlRichEdit_AppendText($RichEdit, $command&StringReplace($text,@CRLF,"\line")&"}" )
EndFunc

Please test.

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...