Jump to content
Sign in to follow this  
Skitty

RichEdit colored text complications.

Recommended Posts

I found this example by zedna, it worked perfectly (entering text into the control with specified color), that is until you add any form of line break (@CR, @LF or @CRLF).

If the line breaks aren't there, the text entered is colored correctly, but when they are, the colors gets off and I can't figure out why, anyone know what's the matter?

#include <GUIConstants.au3>
#include <GuiRichEdit.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 448, 269, 192, 124)
$Edit1 = _GUICtrlRichEdit_Create($Form1,"", 16, 24, 417, 225)
GUISetState(@SW_SHOW)
_GUICtrlRichEdit_AppendTextColor($Edit1, "Hello "& @CR, "0x00FF00")
_GUICtrlRichEdit_AppendTextColor($Edit1, "Autoit "& @CR, "0xFF0000")
_GUICtrlRichEdit_AppendTextColor($Edit1, "World", "0x000000")
;~ _GUICtrlRichEdit_AppendText($Edit1, "World")
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GUICtrlRichEdit_Destroy($Edit1)
            Exit
    EndSwitch
WEnd
Func _GUICtrlRichEdit_AppendTextColor($hWnd, $sText, $iColor)
    Local $iLength = Int(_GUICtrlRichEdit_GetTextLength($hWnd)/2) ; RichEdit stores text as 2 Byte Unicode chars
    _GUICtrlRichEdit_AppendText($hWnd, $sText)
    _GUICtrlRichEdit_SetSel($hWnd, $iLength, $iLength + StringLen($sText)*2) ; position in 2 Byte "Unicode"
    _GUICtrlRichEdit_SetCharColor($hWnd, $iColor)
    _GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

Func _GUICtrlRichEdit_AppendTextColor($hWnd, $sText, $iColor)
    Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd, True, True) ; RichEdit stores text as 2 Byte Unicode chars
    Local $iCp = _GUICtrlRichEdit_GetCharPosOfNextWord($hWnd, $iLength)
    _GUICtrlRichEdit_AppendText($hWnd, $sText)
    _GUICtrlRichEdit_SetSel($hWnd, $iCp-1, $iLength + StringLen($sText)) ; position in 2 Byte "Unicode"
    _GUICtrlRichEdit_SetCharColor($hWnd, $iColor)
    _GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

Share this post


Link to post
Share on other sites

Ok, so I just woke up, and I remember finding out what was the matter and as I was going to post, the forums went down.

Apparently one has to keep tabs of how many line break characters are entered and subtract them from the string size~

Func _GUICtrlRichEdit_AppendTextColor($hWnd, $sText, $iColor)
    Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd)/2 ; RichEdit stores text as 2 Byte Unicode chars
    _GUICtrlRichEdit_AppendText($hWnd, $sText)
If StringRight($sText,1) == @CR Then
  $CR_Tracker -= 1
EndIf
    _GUICtrlRichEdit_SetSel($hWnd, ($CR_Tracker+$iLength), ($CR_Tracker+$iLength) + StringLen($sText)*2) ; position in 2 Byte "Unicode"
    _GUICtrlRichEdit_SetCharColor($hWnd, $iColor)
    _GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

But apparantly rover has found a much better solution!

Func _GUICtrlRichEdit_AppendTextColor($hWnd, $sText, $iColor)
Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd, True, True) ; RichEdit stores text as 2 Byte Unicode chars
Local $iCp = _GUICtrlRichEdit_GetCharPosOfNextWord($hWnd, $iLength)
_GUICtrlRichEdit_AppendText($hWnd, $sText)
_GUICtrlRichEdit_SetSel($hWnd, $iCp-1, $iLength + StringLen($sText)) ; position in 2 Byte "Unicode"
_GUICtrlRichEdit_SetCharColor($hWnd, $iColor)
_GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

Thanks rover, this works perfectly.

Only problem now is resizing the rich edit control manually, to bad it cannot be done by just setting the resizing style.


Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

That's because RichEdit is not native AutoIt's control but UDFs one.

I see, what I did to overcome this is create a group control and position the richedit inside and resize it by using controlgetpos and _WinAPI_MoveWindow() through a callback from WM_SIZE.

But as I notice, this is very buggy and when the UI is doing something else, the control will not resize with the group box. I wish I would have foreseen this before making the changes I did to my application. Sure, colored text entries are nice but not when you have to do all this workaround stuff to get it to look, resize and work correctly.


Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

#include <GUIConstants.au3>
#include <GuiRichEdit.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

$Form1 = GUICreate("Form1", 448, 269, 192, 124, BitOr($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX))
$Edit1 = _GUICtrlRichEdit_Create($Form1,"", 10, 10, 428, 249)
GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUISetState(@SW_SHOW)

_GUICtrlRichEdit_AppendTextColor($Edit1, "Hello ", "0x00FF00")
_GUICtrlRichEdit_AppendTextColor($Edit1, "Autoit ", "0xFF0000")
_GUICtrlRichEdit_AppendTextColor($Edit1, "World", "0x000000")

_GUICtrlRichEdit_AppendText($Edit1, @CRLF & @CRLF)
_GUICtrlRichEdit_AppendTextColor($Edit1, "Hello" & @CRLF, "0x00FF00")
_GUICtrlRichEdit_AppendTextColor($Edit1, "Autoit" & @CRLF, "0xFF0000")
_GUICtrlRichEdit_AppendTextColor($Edit1, "World" & @CRLF, "0x000000")

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

Func _GUICtrlRichEdit_AppendTextColor($hWnd, $sText, $iColor)
Local $iLength = _GUICtrlRichEdit_GetTextLength($hWnd, True, True)
Local $iCp = _GUICtrlRichEdit_GetCharPosOfNextWord($hWnd, $iLength)
_GUICtrlRichEdit_AppendText($hWnd, $sText)
_GUICtrlRichEdit_SetSel($hWnd, $iCp-1, $iLength + StringLen($sText))
_GUICtrlRichEdit_SetCharColor($hWnd, $iColor)
_GuiCtrlRichEdit_Deselect($hWnd)
EndFunc

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
$NewW = _WinAPI_LoWord($lParam)
$NewH = _WinAPI_HiWord($lParam)
_WinAPI_SetWindowPos($Edit1, 0, 10, 10, $NewW - 20, $NewH - 20, BitOR($SWP_NOACTIVATE, $SWP_NOZORDER))
Return $GUI_RUNDEFMSG
EndFunc

Edited by Zedna

Share this post


Link to post
Share on other sites

Wow, thanks zedna, the replacement of a simple dull edit control with the extreme likes of a colorful RichEdit control worked out flawlessly as far as I can tell, thank you all for the help.

The UI that has been implemented in the app I'm making doesn't even seem like it was made with autoit, working with the WM messages sure makes a huge difference in how responsive the UI of your script will be.

Now I just need to work out a few bugs regarding the rich edit disappearing randomly and a problem where bits of the rich edit seem to fail to be painted when data is entered into it until I resize the UI.


Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

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
Sign in to follow this  

  • Similar Content

    • By user4157124
      ConsoleWrite('>Message here.' & @CRLF) outputs colored text (per + > - ! characters).
      ConsoleWrite('warning' & @TAB & '38' & @TAB & 'more text ...' & @CRLF) enables jump to line 38 on doubleclick.

      Using "jump to line" format, only red and pink text coloring seems possible (simply prefixing color directives to output-text disables jump to line functionality). Is it possible to combine the two (define custom color while keeping "jump to line" functionality)?
    • By Miliardsto
      I have the following code. You can see there are $color_normal and $color_trans
      In my case normal is too dark . And color trans is to much violet.
      If you run it you will see how its looks. I want gui with listView have the same color like taskbar in windows 10.
      How to achieve that?
       
       
      #include <Date.au3> #include <MsgBoxConstants.au3> #include <GuiListBox.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <GuiListView.au3> HotKeySet("{ESC}", "Terminate") $color_normal = "0x" & StringLeft(StringRight(RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Accent", "AccentPalette"), 24), 6) $color_trans = "0x" & StringLeft(StringRight(RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Accent", "AccentPalette"), 16), 6) $trans = RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", "EnableTransparency") ConsoleWrite("Transpart = " & $trans & @CRLF) ConsoleWrite("Color normal = " & $color_normal & @CRLF) ConsoleWrite("Color trans = " & $color_trans & @CRLF) ConsoleWrite(_Get_taskbar_color()); It return AARRGGBB $taskbarColor = _Get_taskbar_color() Global $gui = GUICreate("Test", 150, 58,@DesktopWidth-300,@DesktopHeight-58,$WS_POPUP,BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)) Global $idListview = GUICtrlCreateListView("", 0, 0, 150, 58,BitOR($LBS_NOTIFY,$LBS_SORT), 0) ;_GuiCtrlMakeTrans(-1,100) ; Add column _GUICtrlListView_AddColumn($idListview, "Msgs", 100) GUICtrlSetFont(-1, 7, 400, 0, "Segoe UI") GUICtrlSetColor(-1, 0x470C4F) GUICtrlSetBkColor($idListview, 0x310638) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func Terminate() Exit EndFunc Func _Get_taskbar_color() If RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", "ColorPrevalence") Then If RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", "EnableTransparency") Then Return "0xD9" & StringLeft(StringRight(RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Accent", "AccentPalette"), 16), 6) Else Return "0xFF" & StringLeft(StringRight(RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Accent", "AccentPalette"), 24), 6) EndIf Else If RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize", "EnableTransparency") Then Return "0xD9000000" Else Return "0xFF000000" EndIf EndIf EndFunc  
    • By dejhost
      Happy New Year everyone!
      I would like to extract the color values of certain pixels in an image.  This is how I picture the workflow: 
      User opens an image of his choice. Image is shown on the screen. User draws a line into the image. This happens by marking the startpixel and the endpixel of the line The line is drawn, so the user can check visually if he is happy with the line.  The following pixel based properties belonging to the line are stored in Excel: X-Coordinate Y-Coordinate Color Value Additional operations: Extracting for max- and min. color values; Statistical operations.   Browsing through the helpfile of AutoIt I find plenty of functions for treating images (e.g. GDIPlus), but I am completely unsure if Autoit will get me there. Should I read the entire image into an array? Should I rather attempt to script an external image software (e.g. IrfanView, Gimp)?
      I would be very thankful if someone could give some recommendations and maybe list a couple of the most important commands to use.
      Thank you very much.
      Dejhost
       
       
    • By Skeletor
      Hi All,
      While creating a few excel spreadsheets using AutoIt, I came across something which to my limiting time to research the forums I don't anyone has mentioned. 
      The color pallettes are reversed. 
      Huge shock to me.
       
      I wanted to produce a red row but kept on getting blue. 
      Seems like 0xFF0000 was red on the charts but when running the script, I got blue. 
      I then played around with the colors, and after a few tries, I finally got Red. 
      Reversed the FF0000 and the result is 0000FF.
       
      So for Excel compared to Html
      0000FF (Red) - Excel
      0000FF (Blue) - Html

      FFFF00 (Cyan) - Excel
      FFFF00(Yellow) - Html
       
    • By xtcislove
      Hello,
      i stuck again,
      Im using this function to create a treeview from root dir.
       
      ;https://autoit.de/index.php?thread/86082-treeview-root-verbergen/&postID=691139#post691139 #include <File.au3> #include <WindowsConstants.au3> Global $sPath = @ScriptDir Global $hGui = GUICreate('TreeView-Example', 400, 600) Global $idTreeView = GUICtrlCreateTreeView(10, 10, 380, 580, Default, $WS_EX_CLIENTEDGE) GUISetState() _CreatePath($sPath, $idTreeView) Do Until GUIGetMsg() = -3 Func _CreatePath($sPath, $idParent) Local $aFolder, $aFiles, $idItem If StringRight($sPath, 1) <> '\' Then $sPath &= '\' $aFolder = _FileListToArray($sPath, '*', $FLTA_FOLDERS) If Not @error Then For $i = 1 To $aFolder[0] $idItem = GUICtrlCreateTreeViewItem($aFolder[$i], $idParent) _CreatePath($sPath & $aFolder[$i], $idItem) Next EndIf $aFiles = _FileListToArray($sPath, '*', $FLTA_FILES) If @error Then Return For $i = 1 To $aFiles[0] $idItem = GUICtrlCreateTreeViewItem($aFiles[$i], $idParent) Next EndFunc Folder Structure:

      Folder1
      Folder2
      Folder3
      If a file exists in multiple folders, i like to color it red, if not green. 

      I know how to do this for files, but nut for the folders.

      Because if there is only 1 file in Folder2 that is also in Folder1 that it should only color this single file red, inlcuding its whole tree.

      The Folder1 and Folder2 should be red in this case, too. Other files and trees should stay green.
       
      Edit:
      Basically i like to color a file and its belonging tree red if the file exists more than 1 time.
×
×
  • Create New...