Changing the color of CERTAIN words in an edit control

This is a discussion on Changing the color of CERTAIN words in an edit control within the Windows Programming forums, part of the Platform Specific Boards category; I'm programming this program that when a specific work is typed into the edit control, it turns blue. But, just ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Changing the color of CERTAIN words in an edit control

    I'm programming this program that when a specific work is typed into the edit control, it turns blue. But, just that one word. How would I go about doing this? Thanks!
    1978 Silver Anniversary Corvette

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Maybe a seperate thread running EM_FINDTEXT mesasages to a rich edit...............on a return > 0.....EM_SETCHARFORMAT to change text colour

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What do you mean?
    1978 Silver Anniversary Corvette

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Assuming rich edit.... (you would need that for the text colour)

    Create a worker thread to constantly scan your edit for certain key words....this can be done by sending (with SendMessage) EM_FINDTEXT messages......now if one is found the return is more than 0 (the first char of the found word)....use that and the length of the word to send an EM_SETSEL message and select the word....Now prepare a CHARFORMAT structure (for the colour) and send it with the wParam set to SCF_WORD | SCF_SELECTION........

    I havent tried any of this, but I assume it should work.....

    I would code an example to try it, but its 2AM and I have to be up in the morning.....

    Hope it works

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I haven't quite gotten into threads yet (I've been slacking in the WinAPI department ) but I'm going to go read about them and work in what you said. I'll come back with results/problems. Thanks, Fordy!
    1978 Silver Anniversary Corvette

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Another, brute-force, method using rich edit controls is to take the contents, parse for keywords and add syntax formatting for rtf - colours, fonts etc. Use the EditStreamCallback fn to fill the control using the EM_STREAMIN or EM_STREAMOUT messages.

  7. #7
    Unregistered
    Guest
    There is an interesting tutorial (in assembly) about this subject on Iczelion's web site, the number 35.
    http://win32asm.cjb.net
    But it is not an easy task.
    If you want to use such a control for an editor give a look at http://www.winmain.com/ (at this moment this site is down).

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Fordy, do you really think I need to use threads? After all, the program is only an IDE and nothing will be needed to go on besides checking the rich edit for those certain words. So, do you think that using threads are necessary?
    1978 Silver Anniversary Corvette

  9. #9
    Unregistered
    Guest
    (this is not the previous Unregistered)

    Are you then attempting to make a syntax-highlighting edit control?

    If so, I can only say from my experience with using (end-user so far, but its my Summer project to make my own) slow implementations (presumably brute-force) of this that it is worth your time to make sure that you can refresh quickly enough.

    Consider later that you may be doing additional processing to add word completion, funcion sig. messages , or further consider what happens when someone tries to load a 2000-line source document.

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Unregistered
    (this is not the previous Unregistered)

    Are you then attempting to make a syntax-highlighting edit control?

    If so, I can only say from my experience with using (end-user so far, but its my Summer project to make my own) slow implementations (presumably brute-force) of this that it is worth your time to make sure that you can refresh quickly enough.

    Consider later that you may be doing additional processing to add word completion, funcion sig. messages , or further consider what happens when someone tries to load a 2000-line source document.
    So what are you saying I do? Use a thread?
    1978 Silver Anniversary Corvette

  11. #11
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Instead of using a thread you could subclass the rich edit control. Everytime a key is pressed you could run through a function to find and highlight the text.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from a Rich Edit control
    By JustMax in forum Windows Programming
    Replies: 10
    Last Post: 02-14-2009, 07:12 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 10:52 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Controlling an edit control
    By master5001 in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 04:08 PM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 03:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21