Skitty Posted June 17, 2012 Posted June 17, 2012 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
Zedna Posted June 17, 2012 Posted June 17, 2012 Just for the reference here is original post I tried this workaround but result is the same (bad) _GUICtrlRichEdit_AppendTextColor($Edit1, "Hello ", "0x00FF00") _GUICtrlRichEdit_AppendText($Edit1, @CRLF) I don't know yet how to fix this... Resources UDF ResourcesEx UDF AutoIt Forum Search
rover Posted June 17, 2012 Posted June 17, 2012 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 KristinaA and Skitty 2 I see fascists...
Skitty Posted June 17, 2012 Author Posted June 17, 2012 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.
Zedna Posted June 17, 2012 Posted June 17, 2012 Only problem now is resizing the rich edit control manually, to bad it cannot be done by just setting the resizing style.That's because RichEdit is not native AutoIt's control but UDFs one. Resources UDF ResourcesEx UDF AutoIt Forum Search
Skitty Posted June 17, 2012 Author Posted June 17, 2012 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.
Zedna Posted June 17, 2012 Posted June 17, 2012 (edited) expandcollapse popup#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 June 17, 2012 by Zedna SupaNewb and Skitty 2 Resources UDF ResourcesEx UDF AutoIt Forum Search
Skitty Posted June 18, 2012 Author Posted June 18, 2012 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now