Edit Text color for specific lines

This is a discussion on Edit Text color for specific lines within the Windows Programming forums, part of the Platform Specific Boards category; After some searching I was able to figure out how to change the text color in a read-only edit box, ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Edit Text color for specific lines

    After some searching I was able to figure out how to change the text color in a read-only edit box, but it also turns out that is not exactly what I want.

    I have a simple bot that connects to battle.net so I add text every time something else is said. I want to be able to change the color depending on whether it is normal chat text, a whisper, an emote, etc. But I don't want it to affect the entire edit control--just a line or two (or whatever).

    Is there a simple way to do this? I looked at the TextOut() function, and that might work, but it seems like it would be a lot of work if I understand how it works correctly. That is, it looks like you have to specify the coordinates for the text and it wouldn't work too well with scrolling and constantly adding more text.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're going to have to look into the richedit control. That'll allow you to specify different colours for each part of the text. With a standard edit control, you can only specify one colour for everything.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thanks. I'm looking into it now, and this sounds good:
    Rich edit controls support almost all of the messages and notification messages used with multiline edit controls. Thus, applications that already use edit controls can be easily changed to use rich edit controls.
    I was worried that I would have to make drastic changes, but maybe I won't after all!
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Took a bit more work than I thought it would but I've the colors working correctly now, thanks
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    could you please tell me how you did it?
    i'm trying to do the exact same thing as you

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well first you have to create the Rich Edit, of course, so you call LoadLibrary with the proper dll as the argument (see here).

    Then you use the EM_SETCHARFORMAT message when you want to change the color. I first filled the CHARFORMAT structure and then selected the end of the edit with the EM_SETSEL message and replaced that selection with the EM_REPLACESEL message like this:
    Code:
    int len=(GetWindowTextLength(GetDlgItem(hWnd, IDC_CHAT_TEXT))+1); //get edit text length
    static CHARFORMAT chr = { sizeof(CHARFORMAT), CFM_COLOR, 0, 0, 0, RGB(0,0,0), 0, 0 }; //initialize the structure, only specifying color 
    const char* parsedmessage = parseBNMessage(tempBuffer); //this gets the text to add to the edit  
    switch (CurrentColor) //this is global, set by parseBNMessage()
    {//pretty self-explanatory here
        case CHAT_COLOR:
            chr.crTextColor = RGB(0,0,255);
        break;  
        case EMOTE_COLOR:
            chr.crTextColor = RGB(252,158,18);
        break;
        case ERROR_COLOR:
             chr.crTextColor = RGB(255,0,0);
        break;
        case WHISPER_COLOR:
             chr.crTextColor = RGB(120,120,120);
        break;
        case CHANNEL_COLOR:
             chr.crTextColor = RGB(0,255,0);        
        break;
        case INFO_COLOR:
           chr.crTextColor = RGB(85,255,85);
        break;  
        case DEFAULT_COLOR:		        
           chr.crTextColor = RGB(0,0,0);
        break;
            
    }
    static CHARFORMAT* chrformat = &chr;  //SendMessage needs a pointer  
    SendMessage(hChat, EM_SETSEL, len, len); //select the end of the edit
    SendMessage(hChat, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)(chrformat));  //color and/or format text 
    SendMessage(hChat, EM_REPLACESEL, 0, (LPARAM)(parsedmessage)); //add the text
    the parseBNMessage and switch statement are specific to my application, but I thought I'd leave it in anyways because it kind of makes it clearer what I'm doing (I think).

    Hope that helps!
    Last edited by JaWiB; 07-08-2004 at 01:25 PM. Reason: Added comments
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    what a great answer, thanks man.
    that's really helped me out.
    I appreciate it

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    just tried the code and it works, i'm so happy.
    been trying to do that for ages!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  2. need to edit text from within my application.
    By spiroth10 in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2006, 02:15 PM
  3. adding a line of text to a readonly edit control?
    By Kibble in forum Windows Programming
    Replies: 2
    Last Post: 11-25-2002, 08:04 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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