Jump to content

How to send formatted "RTF text" to richedit directly?


 Share

Recommended Posts

Trying to enter colored text into a rich edit is a pain in the ass and complicated, and after searching for a few hours I always end up in the help file staring at this text:

 

_GUICtrlRichEdit_SetText ( $hWnd, $sText )


Parameters
$hWnd Handle to the control 
$sText Plain or RTF text to put into the control

What is this RTF text it speaks of? What I am THINKING of is how if you copy bold text from a page, and paste it into wordpad, it is bold. But if you paste that text into notepad and then copy it and paste it into wordpad it will have no formatting. There is some kind of extra formatting that can be on the text in clipboard

In the same way if you _GUICtrlRichEdit_SetText with plain text it sends it in the richedit as plain text. How do I send pre-formatted "RTF text" to a richedit using _GUICtrlRichEdit_SetText?

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

RichTextFormat save the format options with the text. When you copy the text from wordpad and then insert into your RichEditControl the inserted text is also formated. You must use pre-formated Text with _GUICtrlRichEdit_SetText. Have a look on https://en.wikipedia.org/wiki/Rich_Text_Format#Code_example or open a RTF-File with notepad.

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

Example()

Func Example()
    Local $hGui, $iMsg, $hRichEdit
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_SetText($hRichEdit, "{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard This is some {\b bold} text.\par}This is some {\b bold} text.\par}")
    While True
        $iMsg = GUIGetMsg()
        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                ; GUIDelete()   ; is OK too
                Exit
        EndSelect
    WEnd
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Oh. Thanks, at least I now know what to google.

Using this info and this tutorial here, I was able to come up with this:

Func _GUICtrlRichEdit_AppendTextEx($RichEdit, $text, $font="Arial", $color="000000", $size=12, $spacing=-1, $bold=0, $italic=0, $underline=0, $strike=0)
  Local $command = "{\rtf1\ansi"
  Local $r, $g, $b, $ul[9] = ["8", '\ul', '\uldb', '\ulth', '\ulw', '\ulwave', '\uld', '\uldash', '\uldashd']
  If $spacing = -1 Then $spacing = $size

  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 $spacing <> $size Then $command &= "\sl"&round($spacing*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)
   _GUICtrlRichEdit_AppendText($RichEdit, $command&StringReplace($text,@CRLF,"\line")&"}" )
EndFunc

And it works for pretty much everything you could need from a rich edit, ecpt I cannot figure out how to get line spacing working. According to the first page I linked, \sl should give me the line spacing, but nothing I try works and I cannot find an example for it.

Could someone who knows RTF provide an example for using \sl? I want to be able to set the spacing between lines.

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Update:

All attempts to try and set the distance between lines as line spacing (Like in the paragraph menu of word) has failed to give me the desired effect, since they REQUIRE you create a new paragraph and end it to apply the spacing. If you wanted to have 

Some regular text with some bold text

and then a line under it with 1.5x spacing between

You would need to have some kind of patterns or \flag things to specify when to have the bold text / other formatting.

I worked around this by keeping things simple. Instead of some kind of phraser, I just use the function multiple times, which I guess is lazy but it works really well. For instance

The quick brown fox
jumped over

the lazy dog.

It would look like

Func QuickBrownFox()
    _GUICtrlRichEdit_AppendTextEx($RichEdit, "The quick ", "Arial", "000000", 12)
    ;Color code for brown
    _GUICtrlRichEdit_AppendTextEx($RichEdit, "brown", "Arial", "A57954", 12)
    ;CRLF's turn into \line's in the func anyways
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " fox"&@CRLF, "Arial", "000000", 12)
    ;the 1 denotes bold, all line styles in the func
    _GUICtrlRichEdit_AppendTextEx($RichEdit, "jumped over", "Arial", "000000", 12, 1)
    ;add a space at the end of the line with a bigger size. Works well enough to set spacing in most cases.
    _GUICtrlRichEdit_AppendTextEx($RichEdit, " "&@CRLF, "Arial", "000000", 18)
    _GUICtrlRichEdit_AppendTextEx($RichEdit, "the lazy dog", "Arial", "000000", 12, 1)
endfunc

Definitely not optimal for all cases, where the text is very dynamic, but it works. I think I'm going to patch this up a bit more and post in example scripts for those who want it.

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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