Jump to content

Selecting texts on new line in __GUICtrlRichEdit_Create


Recommended Posts

I am working on a project that that requires me to identify "pass" and "fail" texts by setting character background in a _GUICtrlRichEdit_Create editbox.  I can do a count on each string and set the text selection with _GUICtrlRichEdit_SetSel but my output is usually about 50-70 lines of output.  Can someone suggest a better and more efficient method for me?  I have included a sample below to illustrate.

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


Global $Form1 = GUICreate("Form1", 615, 437, 192, 124)
;Global $Edit1 = GUICtrlCreateEdit("", 32, 32, 561, 377)
Global $Edit1 = _GUICtrlRichEdit_Create($Form1, "", 32, 32, 561, 377, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

_GUICtrlRichEdit_AppendText($Edit1, "WARNING - The test ran unsuccessfully!" & @CRLF)
_GUICtrlRichEdit_SetSel($Edit1, 0, 7)
_GUICtrlRichEdit_SetCharBkColor($Edit1, Dec('8888FF'))
_GUICtrlRichEdit_AppendText($Edit1, "PASS - The test ran successfully 2!" & @CRLF)
_GUICtrlRichEdit_SetSel($Edit1, 39, 44)
_GUICtrlRichEdit_SetCharBkColor($Edit1, Dec('00d65c'))
_GUICtrlRichEdit_AppendText($Edit1, "WARNING - The test ran unsuccessfully again!" & @CRLF)
_GUICtrlRichEdit_SetSel($Edit1, 0, 7)
_GUICtrlRichEdit_SetCharBkColor($Edit1, Dec('8888FF'))

GUISetState(@SW_SHOW)



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

    EndSwitch
WEnd

 

Link to comment
Share on other sites

Hmmm I use this when I'm using consoles in my applications

So I can append formatted text using html tags (or like html tags, because <color> is not really a html tag). I think I put color and bkcolor in their own tag because I don't change the fonts of my consoles, just color, bkcolor, and attributes. This was back when I was learning how to use regexp so it's (kind of?) inefficient but it does all the work for you lol

All you'd have to do is

_GUICtrlRichEdit_AppendHtmlText($Edit1, "<bkcolor=0x8888FF>WARNING</bkcolor> - The test ran unsuccessfully!" & @CRLF)
_GUICtrlRichEdit_AppendHtmlText($Edit1, "<bkcolor=0x00d65c>PASS</bkcolor> - The test ran successfully 2!" & @CRLF)
_GUICtrlRichEdit_AppendHtmlText($Edit1, "<bkcolor=0x8888FF>WARNING</bkcolor> - The test ran unsuccessfully again!" & @CRLF)

 

Link to comment
Share on other sites

Here is a basic regex-based lexer, very versatile
If the text in the richedit is very long this can be easily adapted to manage the last added line only instead of the whole text

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


Global $Form1 = GUICreate("Form1", 615, 437, 192, 124)
;Global $Edit1 = GUICtrlCreateEdit("", 32, 32, 561, 377)
Global $Edit1 = _GUICtrlRichEdit_Create($Form1, "", 32, 32, 561, 377, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

_GUICtrlRichEdit_AppendText($Edit1, "WARNING - The test ran unsuccessfully!" & @CRLF)
_GUICtrlRichEdit_AppendText($Edit1, "PASS - The test ran successfully 2!" & @CRLF)
_GUICtrlRichEdit_AppendText($Edit1, "WARNING - The test ran unsuccessfully again!" & @CRLF)

_colors($Edit1)

GUISetState(@SW_SHOW)


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


Func _colors($hRichEdit)
  Local $red = 0x8888FF, $green = 0x00d65c   ; 0xBBGGRR
  Local $matchstart, $matchlen, $offset = 1
  Local $txt = _GUICtrlRichEdit_GetText($hRichEdit), $len = StringLen($txt)
  _GUICtrlRichEdit_PauseRedraw($hRichEdit)

  While $offset < $len 
    $res = StringRegExp($txt, '(?|WARNING|PASS)', 1, $offset)
    $offset = @extended
    If not IsArray($res) Then Exitloop
    $match = $res[0]
    $matchlen = StringLen($match)
    $matchend = $offset-1 
    $matchstart = $matchend-$matchlen
    _GUICtrlRichEdit_SetSel($hRichEdit, $matchstart, $matchend)
    Switch $match
      Case "WARNING" 
         _GUICtrlRichEdit_SetCharBkColor($hRichEdit, $red)
      Case "PASS" 
         _GUICtrlRichEdit_SetCharBkColor($hRichEdit, $green)
    EndSwitch
  Wend
  _GUICtrlRichEdit_SetSel($hRichEdit, -1, -1)
  _GUICtrlRichEdit_ResumeRedraw($hRichEdit)
EndFunc

 

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

  • Recently Browsing   0 members

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