Sign in to follow this  
Followers 0
JeyJ0

changing color,etc. in Edit field

13 posts in this topic

Hey community, I've got the following problem:

I want to highlight keywords in an Edit-field in my GUI by using a changing color or make it italic/bold.

But I want to read the words to highlight out of a file like this:

Filename: bold.txt (or bold.ini)

Text:

while

for

in

so I want that the keywords "while", "for" and "in" will be bold if they are typed in the Edit-field. The same I want to do with colors etc. Could anyone please post how to, because I dont have any idea...

Thanks!

JeyJ0

 

-----------------------------------------------------------

Sorry for bad english, I'm from Germany...


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

There are people here with more experience in this, but I suggest you look at using a Rich Edit control and look in example scripts for similar projects.

Edited by czardas

Share this post


Link to post
Share on other sites

How do you create a "Rich Edit control"??? :wacko:

I looked for example projects before but didn't find what I wanted to... But thanks for the fast answer!! :thumbsup:


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites

Hi,

Take a look at the _GUICtrlRichEdit_SetCharAttributes function in the helpfile.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Example

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


Main()

Func Main()
    Local $hGui, $iMsg
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "RichEdit - Test | Phoenix XL", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    _GUICtrlRichEdit_SetEventMask($hRichEdit, $ENM_CHANGE)

    Local $lblMsg = GUICtrlCreateLabel("While For Next are automatically bolded upon typing.", 10, 235, 300, 60)

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    GUISetState()

    Do
        Sleep(10)
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
    GUIDelete() ; is OK too

EndFunc   ;==>Main

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    If _WinAPI_HiWord($wParam) = $EN_CHANGE Then CheckCurrentWord($lParam)

    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_COMMAND

Func CheckCurrentWord($hRichEdit, $s_Pattern = "(?i)^(while|for|next)$")

    Static $i_CheckBK ;a check for if backspace was pressed

    $s_Word = _GetCurrentWord($hRichEdit)
    $i_WordPos = @extended

    If _GUICtrlRichEdit_GetTextLength($hRichEdit) < $i_CheckBK Then
        $i_CheckBK = _GUICtrlRichEdit_GetTextLength($hRichEdit)
        ;backspace is pressed or chars deleted.
        Return
    EndIf


    ;if matched then
    If StringRegExp($s_Word, $s_Pattern) Then

        _GUICtrlRichEdit_SetSel($hRichEdit, $i_WordPos, $i_WordPos + StringLen($s_Word))
        _GUICtrlRichEdit_SetCharAttributes($hRichEdit, "+bo", True)


        _GUICtrlRichEdit_InsertText($hRichEdit, " ")
        _GUICtrlRichEdit_SetCharAttributes($hRichEdit, "-bo", False)


    EndIf

    $i_CheckBK = _GUICtrlRichEdit_GetTextLength($hRichEdit)

EndFunc   ;==>CheckCurrentWord

; #FUNCTION# ====================================================================================================================
; Name...........: _GetCaretOffset
; Description ...: Get the Offset of the Caret
; Syntax.........: _GetCaretOffset($Edit_ID)
; Parameters ....: $Edit_ID         - The Control ID of the Edit Control
;
; Return values .: Success      - Returns the Offset & Sets @extended to
;                       | 1 - A Selection is Present
;                       | 0 - No Selection is Present
;                  Failure      - Returns -1 & Sets @error to 1
;
; Author ........: Phoenix XL
; Modified.......:
; Remarks .......: Internally Used. from RichEditPredictText library
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/145398-predict-text-for-a-richedit-control-richeditpredicttextau3-udf/
; Example .......:
; ===============================================================================================================================
Func _GetCaretOffset($nRichEdit)
    Local $_LineIndex = _GUICtrlRichEdit_GetSel($nRichEdit)
    If Not IsArray($_LineIndex) Then Return SetError(1, @error, -1)
    Switch $_LineIndex[0]
        Case $_LineIndex[1]
            Return SetExtended(0, $_LineIndex[1])
        Case Else
            $_LineIndex = _GUICtrlRichEdit_GetSelAA($nRichEdit)
            ;Its a Bug in the GUIRichEdit UDF [Switch the Anchor Point]
            _SendMessage($nRichEdit, $EM_SETSEL, $_LineIndex[0], $_LineIndex[1])
            Return SetExtended(1, $_LineIndex[1])
    EndSwitch
EndFunc   ;==>_GetCaretOffset

; #FUNCTION# ====================================================================================================================
; Name...........: _GetCurrentWord
; Description ...: Get the Current Word the Caret is Present at.
; Syntax.........: _GetCurrentWord($hRichEdit[, $cPos=-10[,$cEnter=0]])
; Parameters ....: $hRichEdit       - The Control ID of the Edit Control
;                  $cPos            - For flexibility, used for getting the word at any other offset
;                  $cEnter          - If Enter was Pressed then the Previous Line is Treated as the Primary Line
;                       | 1 - Check the Previous Line
;                       | 0 - Check the Present Line
; Return values .: Success      - Returns the Word & Sets @extended to the character offset of the first alphabet of the word.
;                  Failure      - Returns -1 & Sets @error to 1
;
; Author ........: Phoenix XL
; Modified.......:
; Remarks .......: Internally Used. from RichEditPredictText library
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/145398-predict-text-for-a-richedit-control-richeditpredicttextau3-udf/
; Example .......:
; ===============================================================================================================================
Func _GetCurrentWord($hRichEdit, $cPos = -10, $cEnter = 0)
    If $cPos = -10 Then $cPos = _GetCaretOffset($hRichEdit)
    If $cPos < 0 Then Return SetError(1, $cPos, -1)
    Local $nLine = _GUICtrlRichEdit_GetLineNumberFromCharPos($hRichEdit, $cPos)
    $nLine -= $cEnter
    Local $nStart = _GUICtrlRichEdit_GetFirstCharPosOnLine($hRichEdit, -1)
    Local $nText = _GUICtrlRichEdit_GetTextInLine($hRichEdit, $nLine)
    Local $nWords = StringSplit($nText, ' ', 1)
    If @error Then Return SetError(2, 0, $nText)
    If $cEnter Then Return $nWords[$nWords[0]]
    For $n = 1 To $nWords[0]
        Switch $cPos
            Case $nStart To $nStart + StringLen($nWords[$n])
                Return SetExtended($nStart, $nWords[$n])
        EndSwitch
        $nStart += StringLen($nWords[$n]) + 1
    Next
    Return SetError(1, 0, -1)
EndFunc   ;==>_GetCurrentWord

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@PhoenixXL

On my WinXP / AutoIt 3.3.7.23 doesn't work.

1. when keyword is typed at start of line text of first line in RichEdit is scrambled

2. when keyword is typed inside of other text nothing happens

EDIT: Though nice example ;-)

Edited by Zedna

Share this post


Link to post
Share on other sites

Thanks for all those posts!!!

I need to look on the example later, but to say that here: It is supposed to work like in an IDE... (like SciTE)


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites

Everything Works fine, but I don't know how to change the Text Color? _GUICtrlRichEdit_SetTextColor doesn't work behause i can only do it the while Rich edit...


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Everything Works fine, but I don't know how to change the Text Color? _GUICtrlRichEdit_SetTextColor doesn't work behause i can only do it the while Rich edit...

.

JeyJO, the first thing to learn is to use the helpfile. the holy helpfile is your ständiger Begleiter, your primary source, your friend, your teacher, your saviour, your everything. from the helpfile:

_GUICtrlRichEdit_SetCharColor

.

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

Global $lblMsg, $hRichEdit

Main()

Func Main()
    Local $hGui, $iMsg, $btnNext, $iStep = 0
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    $lblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60)
    $btnNext = GUICtrlCreateButton("Next", 270, 310, 40, 30)
    GUISetState()

    _GUICtrlRichEdit_SetText($hRichEdit, "Paragraph 1")
    While True
        $iMsg = GUIGetMsg()
        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
;~              GUIDelete()     ; is OK too
                Exit
            Case $iMsg = $btnNext
                $iStep += 1
                Switch $iStep
                    Case 1
                        Report("1. Initial setting")
                    Case 2
                        _GUICtrlRichEdit_SetCharColor($hRichEdit, "304050")
                        Report("2. Setting is now")
                    Case 3
                        _GUICtrlRichEdit_SetSel($hRichEdit, 1, 5)
                        _GUICtrlRichEdit_SetCharColor($hRichEdit)
                        Report("3. Background of a few characters changed")
                    Case 4
                        _GUICtrlRichEdit_SetSel($hRichEdit, 6, -1)
                        ; Stream all text to the Desktop so you can look at settings in Word
                        _GUICtrlRichEdit_Deselect($hRichEdit)
                        _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")
                        Report("4. Saved to File")
                        GUICtrlSetState($btnNext, $GUI_DISABLE)
                EndSwitch
        EndSelect
    WEnd
EndFunc   ;==>Main

Func Report($sMsg)
    Local $iColor = _GUICtrlRichEdit_GetCharColor($hRichEdit)
    Local $sMixed = _Iif(@extended, "+", "~")
    Local $aRet = _ColorGetRGB($iColor)
    $sMsg = $sMsg & @CR & @CR & $aRet[0] & ";" & $aRet[1] & ";" & $aRet[2] & " Color=0x" & Hex($iColor) & ":" & $sMixed
    GUICtrlSetData($lblMsg, $sMsg)
    ControlFocus($hRichEdit, "", "")
EndFunc   ;==>Report

.

_GUICtrlRichEdit_SetTextColor does not exist, unless you might use some beta or udf version

cheers E

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Share this post


Link to post
Share on other sites

Sry, I just wrote the wrong thing (in my post)... I meant _GUICtrlRichEdit_SetCharColor :ermm:

I unserstood it the wrong way, cause my English isn't the best...

But thank for giving me the answer!^^


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites

Hey, I have a problem with that function... Why doesn't my code work? (It's not everything, it's only the necessary part)

_GUICtrlRichEdit_SetSel($Edit, $pos - 1, $pos - 1 + $wordLength)
_GUICtrlRichEdit_SetCharAttributes($Edit, "+bo", True)
_GUICtrlRichEdit_SetCharAttributes($Edit, "+it", True)
_GUICtrlRichEdit_SetCharColor($Edit, "0000ff")

The text gets bold and italic, but it isn't blue :wacko:

Can anyone help?


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

Share this post


Link to post
Share on other sites

You're setting the color with a string, not even a hex string, just a string. BTW, it uses a COLORREF color code, not an RGB color code. See the help file for _ColorSetCorref, or just reverse the RGB to a BGR and add 2 zeros in front of it. RGB = 0x0000FF, COLORREF = 0x00FF0000


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

thx, will try it tomorrow!


[font="'courier new', courier, monospace;"]--------------------------------------------------------[/font]

[font="'courier new', courier, monospace;"]Sorry for bad english, I'm from Germany...[/font]

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  
Followers 0