I am developing a network chat application, I am using Winapi, my main window is a dialog (created from a .rc file).

I am looking into a way of doing an text box where my outuput text can be like this:

Welcome to ##### chat server!
Person1 says: Hi folks, what's up?
Person2 says: Hey... that's Person1!
*Person2 pokes Person1.*
Person1 says: Hey!

I'm using a simple EDIT TEXT control ATM. I've been trying to implement a RICH EDIT CONTROL but it seems to be very hard to do...

Anyone know of a good way to obtain the result in the quote.. should i use some kind of control or window class that I'm missing?

Any information is welcome, a simple demonstration code is more than welcome :)

Oops I guess my first post was unclear, I am looking into a way to outuput colored text in the controls in my interface.

I know it is possible in RichEdit Control, but I am not being successful into moving the caret to the start of each line to color the diferent parts. :P

I got this working a few days ago, gonna post my solution so anyone having the same problem can learn with it :)

HWND richtextHandler //must be global

int chatlog_ADD(const char* msg, int mode=0)
{
    CHARRANGE selection;
    CHARFORMAT fontFormat;
    
    memset(&fontFormat, 0, sizeof fontFormat);
    fontFormat.cbSize = sizeof fontFormat;
    
    switch(mode)
    {
        case 0:  //DEFAULT BLUE TEXT
            fontFormat.dwMask = CFM_COLOR;
            fontFormat.crTextColor = RGB(0,0,255);         
            break;
        case 1:  //BOLD RED TEXT
            fontFormat.dwEffects = CFE_BOLD;
            fontFormat.dwMask = CFM_COLOR|CFM_BOLD;
            fontFormat.crTextColor = RGB(255,0,0);          
            break;

        case 3:  //DARK GREEN TEXT
            fontFormat.dwMask = CFM_COLOR;
            fontFormat.crTextColor = RGB(0,128,0);  
            break;                              
    }

    //Move caret to the end of the text
    selection.cpMin = -1;
    selection.cpMax = -1;
    SendMessage(richtextHandler, EM_EXSETSEL, 0, (LPARAM)&selection); 
    
    //Retrieves the curent position
    SendMessage(richtextHandler, EM_EXGETSEL, 0, (LPARAM)&selection);
    
    //ADDs the text
    SendMessage(richtextHandler, EM_REPLACESEL, 0, (LPARAM)msg); 
    
    //Move caret back to the start of the line (position retrieved earlier)
    SendMessage(richtextHandler, EM_EXSETSEL, 0, (LPARAM)&selection); 
    
    //Set endindg position to the end of the line
    selection.cpMax = -1;
    
    //Selects the line you added
    SendMessage(richtextHandler, EM_EXSETSEL, 0, (LPARAM)&selection);
    
    //Applies the format
    SendMessage(richtextHandler , EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &fontFormat); 
    
    //Set the caret to the end again
    selection.cpMin = -1;
    selection.cpMax = -1;
    SendMessage(richtextHandler, EM_SETSEL, 0,(LPARAM)&selection );
    
    //Scrolls down the scroll bar :)
    SendMessage(richtextHandler, WM_VSCROLL, SB_BOTTOM, (LPARAM)NULL);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.