1,105,534 Community Members

Help with chat interface (WINAPI)

Member Avatar
darkbreaker
Newbie Poster
21 posts since Nov 2010
Reputation Points: 33 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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 :)

Member Avatar
pseudorandom21
Practically a Posting Shark
888 posts since Jan 2011
Reputation Points: 166 [?]
Q&As Helped to Solve: 115 [?]
Skill Endorsements: 0 [?]
 
0
 

MSDN says: http://msdn.microsoft.com/en-us/library/ms645489(v=vs.85).aspx

See under the "Initializing a Text Buffer" section here:
http://msdn.microsoft.com/en-us/library/bb775456(v=vs.85).aspx#text_buffer

Member Avatar
darkbreaker
Newbie Poster
21 posts since Nov 2010
Reputation Points: 33 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
darkbreaker
Newbie Poster
21 posts since Nov 2010
Reputation Points: 33 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
}
Question Answered as of 3 Years Ago by pseudorandom21
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: