Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC
Article

A chat control based on the Rich Edit control

Rate me:
Please Sign up or sign in to vote.
4.67/5 (19 votes)
6 Dec 2003CPOL6 min read 151.6K   7.2K   44   22
Using the Rich Edit control as a chat window.

Introduction

Every so often someone will post a request to the message boards requesting help in using the Rich Edit control in a chat client. Since I've done this before it seemed like a good topic for an article.

Background

The class I present here is from an IRC chat client I wrote in 1999/2000. It was a specialised client oriented toward trivia games and not of general interest, though it could certainly function as a general purpose chat client. If you're really curious you can download it from my website MindProbes and have a look. The direct link is Prober (disclaimer: I'm no longer involved in any way with the site).

The Rich Edit control

I'm guessing here but it seems to me that the Rich Edit control was designed to be used as an edit control replacement providing extra functionality. It assumes the user is typing directly into it. This contrasts with what a typical chat client display window would do. The typical chat client window is read only (text is added from an incoming stream from an IRC server) and automatically scrolls text so that the most recent entry is always visible.

Chat clients also typically allow for varied formatting of the contents based on the message type. Was this a whisper? Was it an action? Or just normal 'said' text. (If you have no idea what I'm talking about you haven't experienced a chat room). The need to differentiate different message types makes the Rich Edit control seem a 'natural' fit for the display. But it's not as easy as that!

More detail

So let's look at the class and pick it apart.
C++
class CChatRichEd : public CRichEditCtrl
{
    DECLARE_DYNAMIC(CChatRichEd);
// Construction
public:
                    CChatRichEd();
    virtual         ~CChatRichEd();

// Attributes
public:
    CHARFORMAT&     CharFormat()    { return m_cfDefault; }

// Operations
public:
    BOOL            Create(DWORD dwStyle,  const RECT& rcRect,
                        CWnd* pParentWnd, UINT nID);
    void            AppendText(LPCTSTR szText);
    BOOL            SaveToFile(CFile *pFile);
    void            Freeze();
    void            Thaw();
    void            Clear();

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CRichEd)
    //}}AFX_VIRTUAL

    // Generated message map functions
protected:
    void            InternalAppendText(LPCTSTR szText);
    static DWORD CALLBACK   StreamCallback(DWORD dwCookie, LPBYTE pbBuff,
                                LONG cb, LONG *pcb);

    int             m_iLineCount,
                    m_iLastLineCount;
    CStringList     m_cslDeferredText;
    BOOL            m_bFrozen;
    CHARFORMAT      m_cfDefault;

    //{{AFX_MSG(CRichEd)
    afx_msg void    OnSize(UINT nType, int cx, int cy);
    //}}AFX_MSG
    afx_msg void    OnLink(NMHDR *in_pNotifyHeader, LRESULT *out_pResult);
    DECLARE_MESSAGE_MAP()
};
The constructor does little of interest save for initialising a CHARFORMAT structure. This structure is used by the Rich Edit control to set formatting for inserted text. The formatting you can set includes font settings and text colour.

The Create() function ensures that we've done the appropriate initialisation of the Rich Edit control DLL before we try and create it.

C++
BOOL CChatRichEd::Create(DWORD dwStyle,  const RECT& rcRect,
        CWnd* pParentWnd, UINT nID)
{
    if (!::AfxInitRichEditEx())
        return FALSE ;
    
    CWnd* l_pWnd = this;

    return l_pWnd->Create(_T("RichEdit20A"), NULL, dwStyle, rcRect,
                pParentWnd, nID);
}
Hmmm, we've got a function there that's not part of the windows API. AfxInitRichEditEx() is defined here (in the RichEd.cpp file).
C++
_AFX_RICHEDITEX_STATE::_AFX_RICHEDITEX_STATE()
{
    m_hInstRichEdit20 = NULL ;
}

_AFX_RICHEDITEX_STATE::~_AFX_RICHEDITEX_STATE()
{
    if (m_hInstRichEdit20 != NULL)
        ::FreeLibrary(m_hInstRichEdit20) ;
}

_AFX_RICHEDITEX_STATE _afxRichEditStateEx;

BOOL PASCAL AfxInitRichEditEx()
{
    _AFX_RICHEDITEX_STATE *l_pState = &_afxRichEditStateEx;
    
    if (l_pState->m_hInstRichEdit20 == NULL)
        l_pState->m_hInstRichEdit20 = LoadLibraryA(_T("RICHED20.DLL"));
    
    return l_pState->m_hInstRichEdit20 != NULL ;
}
Well that's pretty simple. The RichEd.cpp file defines a global variable of type _AFX_RICHEDITEX_STATE. Because it's a global the compiler dutifully runs the constructor before our program's WinMain is called. The constructor loads the RICHED20.DLL library, ensuring it's in our process space when we come to run the CChatRichEd constructor. Just to be sure, the CChatRichEd::Create() function checks that the library has been loaded. (this code, _AFX_RICHEDITEX_STATE, was written by Andrew Forget and the article I found it on is at CodeGuru).

So now we've created our control. As I said before, typically it's a readonly control, so the only way text can get to it is by calling the AppendText() function. So let's have a look at that.

C++
//  This is the public interface for appending text to the control.  It
//  either appends it directly to the control or adds it to a string 
//  list if the control is frozen.  When the control is thawed the
//  strings are taken off the list and added to the control.
void CChatRichEd::AppendText(LPCTSTR szText)
{
    if (m_bFrozen)
        m_cslDeferredText.AddHead(szText);
    else
        InternalAppendText(szText);
}
Hmmmmm so now we have to know if the control is thawed or frozen? Yes we do. Picture the use of this control in a chat client. You have maybe a dozen people connected to the chat server from half a dozen different countries. Depending on what's being said one or more users may want to scroll back and see previous history. That's very difficult to do without telling the chat server to shut up. Since the IRC protocol doesn't support such a message we need to do it ourselves. We do this by 'freezing' the control (under user control). When the control is 'frozen' it doesn't display new messages. But we don't want to lose new messages, just defer them. When we 'thaw' the control we want any messages recieved since we 'froze' it to be displayed. That's the purpose of the m_cslDeferredText string array.

The control doesn't 'freeze' or 'thaw' itself. That's the responsibility of your application.

Most of the time the m_cslDeferredText array is empty and messages are added directly to the chat control.

So our control isn't frozen. What does InternalAppendText() do?

C++
void CChatRichEd::InternalAppendText(LPCTSTR szText)
{
    int len;

    ASSERT(szText);
    ASSERT(AfxIsValidString(szText));

    int  iTotalTextLength = GetWindowTextLength();
    CWnd *focusWnd = GetFocus();

    //  Hide any selection and select the end of text marker.
    HideSelection(TRUE, TRUE);
    SetSel(iTotalTextLength, iTotalTextLength);

    //  Now set the character format
    SetSelectionCharFormat(m_cfDefault);
    //  And put the text into the selection
    ReplaceSel(szText);
    len = GetWindowTextLength();
    //  Now select the end of text marker again
    SetSel(len, len);

    if (iTotalTextLength > 125000)
    {
        //  The control's starting to get full so trim off the first 
        //  50,000 bytes....
        SetSel(0, 50000);
        ReplaceSel(_T(""));
        SetSel(iTotalTextLength, iTotalTextLength);
    }

    HideSelection(FALSE, TRUE);
    SendMessage(EM_SCROLLCARET, 0, 0);

    if (focusWnd != (CWnd *) NULL)
        focusWnd->SetFocus();
}
This is also pretty simple. We need to hide the current selection (else the control does amazing things visually), select the end of the text, set the character formatting and insert the new text. When we've inserted the text we scroll the cursor to the end of the control.

This code also sets a hard limit of 125000 bytes on the text length. If the control contains more than 125000 bytes it will delete the first 50000 bytes. Hmmmm this is Win32 and we have gigabytes of virtual memory space available right? Right. But empirical experience (which is admittedly two years old) tells me that trying to save more than 125000 bytes will cause some users of your chat client major problems. Tune those numbers for yourself :)

The OnLink() function

If your chat client is using Rich Edit version 2.0 or later (I think all versions of Windows later than Windows 95 original install have Rich Edit 2.0 or later) the Rich Edit control understands URL's. If the text includes something that looks like a URL or an email address (for example http://www.codeproject.com or mailto:ultramaroon@mindprobes.net) it will show the text matching the pattern as a link. Clicking on the link generates an EN_LINK message which the class intercepts and turns into a ShellExecute "open" action. ShellExecute in turn does the 'right' thing with the URL.

Saving the chat history

I'm not responsible for your divorce :) Those who use your chat client may want to save the history. You create a CFile using whatever method is appropriate and pass a pointer to it to the SaveToFile() function. That function in turn sets up the Rich Edit control stream conditions needed and saves the contents as an RTF file.

Using this in your own projects

I've simplified a couple of things. The most important simplification is in the way text is formatted. In my chat client the Rich Edit control doesn't have a default character format. The code that reads the incoming chat stream creates an object that specifies the formatting based on the incoming message type, ie, normal chat, whispers, server messages etc. Likewise the deferred text object isn't a string list because it doesn't contain strings per se.

I was that close to including my entire object heirarchy for text objects. I decided not to because it has too many dependencies on other parts of my chat client. How are colours specified? How are fonts specified? Suddenly a simple article becomes a treatise on my way of doing things. This is not my intention :)

So really, how do I use this in my project?

Add the ChatRichEd.cpp/h files to your project. When you want to use the chat control in a dialog (or in a view) add a custom control whose class is RichEdit20A. Set the control styles as required. The sample uses 0x50a10844 which translates into WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPED | WS_BORDER | WS_VSCROLL | WS_MAXIMIZEBOX | ES_READONLY | ES_AUTOVSCROLL | ES_MULTILINE.

You then need to create an instance of CChatRichEd and subclass the Rich Edit control to that instance. In the sample project I did this by adding

C++
CChatRichEd m_richEdit;
to the dialog header and
C++
DDX_Control(pDX, IDC_RICHEDIT, m_richEdit);
to the DoDataExchange() function.

History

First version - December 7 2003

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been programming for 35 years - started in machine language on the National Semiconductor SC/MP chip, moved via the 8080 to the Z80 - graduated through HP Rocky Mountain Basic and HPL - then to C and C++ and now C#.

I used (30 or so years ago when I worked for Hewlett Packard) to repair HP Oscilloscopes and Spectrum Analysers - for a while there I was the one repairing DC to daylight SpecAns in the Asia Pacific area.

Afterward I was the fourth team member added to the Australia Post EPOS project at Unisys Australia. We grew to become an A$400 million project. I wrote a few device drivers for the project under Microsoft OS/2 v 1.3 - did hardware qualification and was part of the rollout team dealing directly with the customer.

Born and bred in Melbourne Australia, now living in Scottsdale Arizona USA, became a US Citizen on September 29th, 2006.

I work for a medical insurance broker, learning how to create ASP.NET websites in VB.Net and C#. It's all good.

Oh, I'm also a Kentucky Colonel. http://www.kycolonels.org

Comments and Discussions

 
GeneralQuick Question Pin
picazo22-Feb-06 18:12
picazo22-Feb-06 18:12 
GeneralRe: Quick Question Pin
renato tome1-Oct-06 20:47
renato tome1-Oct-06 20:47 
GeneralRe: Quick Question Pin
Jhony george1-Jun-07 0:56
Jhony george1-Jun-07 0:56 
GeneralHelp Pin
anup_zade22-Aug-05 1:54
anup_zade22-Aug-05 1:54 
GeneralRe: Help Pin
Jhony george1-Jun-07 1:01
Jhony george1-Jun-07 1:01 
QuestionHow to Map a NM_CLICK or a EN_SETFOCUS Event to a Rich Edit Control Pin
Ranjus5-Jul-05 0:47
Ranjus5-Jul-05 0:47 
GeneralUnicode Pin
Nguyen Van Binh12-Jun-05 21:07
Nguyen Van Binh12-Jun-05 21:07 
Questionhow to change the line spacing of a RichEdit control Pin
xiaogi1-Apr-04 18:20
xiaogi1-Apr-04 18:20 
GeneralCreating a control Pin
pdsphil26-Feb-04 6:29
pdsphil26-Feb-04 6:29 
GeneralRe: Creating a control Pin
AbuseByUnkindPeople18-Jun-06 16:12
AbuseByUnkindPeople18-Jun-06 16:12 
QuestionHow to use this in a View Pin
Member 86765014-Feb-04 14:55
Member 86765014-Feb-04 14:55 
AnswerRe: How to use this in a View Pin
Rob Manderson14-Feb-04 15:29
protectorRob Manderson14-Feb-04 15:29 
GeneralRe: How to use this in a View Pin
Member 86765014-Feb-04 16:14
Member 86765014-Feb-04 16:14 
GeneralRe: How to use this in a View Pin
Member 86765014-Feb-04 17:12
Member 86765014-Feb-04 17:12 
GeneralMinor UNICODE problem Pin
Abraxas234-Feb-04 23:14
Abraxas234-Feb-04 23:14 
GeneralRe: Minor UNICODE problem Pin
Rob Manderson4-Feb-04 23:35
protectorRob Manderson4-Feb-04 23:35 
Questionwho can tell me how to set image as richedit background? Pin
okboy26-Dec-03 4:39
okboy26-Dec-03 4:39 
GeneralFreeze while backscrolling Pin
AAntix8-Dec-03 8:53
AAntix8-Dec-03 8:53 
GeneralRe: Freeze while backscrolling Pin
Rob Manderson8-Dec-03 22:51
protectorRob Manderson8-Dec-03 22:51 
QuestionProber??? Pin
Vadim Tabakman7-Dec-03 18:04
Vadim Tabakman7-Dec-03 18:04 
AnswerRe: Prober??? Pin
Rob Manderson7-Dec-03 18:52
protectorRob Manderson7-Dec-03 18:52 
GeneralRe: Prober??? Pin
AntMan7-Dec-03 23:20
AntMan7-Dec-03 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.