Click here to Skip to main content
Click here to Skip to main content

Text Editor in C++ using CRichEditCtrl

, 20 Feb 2005
Rate this:
Please Sign up or sign in to vote.
Simple text editor featuring drag-and-drop and printing.

Sample Image - TextEdit.gif


This program is a text editor, somewhat similar to Notepad. It includes the following features:

  • Uses streams to load and save files.
  • Supports drag-and-drop operations.
  • Supports printing.


Recently, I decided to revisit one of my earliest attempts to write a C++ program using MFC. The goal of the original program was to build a simple text editor which support drag and drop operations to load the file.

Originally, I tried to build this application using CRichEditView. There was a problem however. If the CRichEditView was set to read only, the drag and drop editing behaved as expected, but when the read only was removed, I lost programmatic control of the drag-and-drop. Without read only set, the default behavior of CRichEditView's drag-and drop took over.

I've never found a solution to this particular problem so I've selected to create the text window using CRichEditCtrl instead. If anyone reading this knows how to get CRichEditView to look after the drag-and-drop, let me know.


Since I wasn't using an MFC view, as I elected to dispense with the Document View architecture entirely, I created the RichEdit window directly over the client area of the frame window. The first step is to declare a CRichEditCtrl variable as a member of CMainFrame as follows.

//inside Mainfrm.h
class CMainFrame : public CFrameWnd
    CRichEditCtrl m_RichEdit;

The next step is to create the rich text window over the client area of the frame window.

//inside Mainfrm.cpp
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    // create a RichEdit window to occupy the client area of the frame

    if (!m_RichEdit.Create(WS_CHILD | WS_VISIBLE | SCROLL |
         WS_VSCROLL, CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST))
        TRACE0("Failed to create RichEdit window\n");
        return -1;

    // Set extended style: 3D look - ie. sunken edge 
    ::SetWindowLong(m_RichEdit.GetSafeHwnd(), WL_EXSTYLE, WS_EX_CLIENTEDGE);

    //Support Drag and Drop

DragAcceptFiles allows us to process files which are dragged from an application like Windows Explorer and dropped on to our rich text window. The WM_DROPFILES windows message is generated in response to the dropped file, and this message is then handled by the OnDropFiles function. Inside the OnDroppedFiles function, DragQueryFile acquires the name of the dropped file, and ReadFile reads the contents of the file into the Richedit control.

void CMainFrame::OnDropFiles(HDROP hDropInfo)
    CString FileName;

    ::DragQueryFile(hDropInfo, 0, FileName.GetBufferSetLength
      (_MAX_PATH), _MAX_PATH);

    m_strPathname = FileName;


Mastering MFC Development Using Microsoft Visual C++ 6.0.


This program shows one approach to using MFC to create a simple text editor. Hopefully it will be of interest to programmers new to MFC. There is no copyright on this code, so feel free to use it in programs of your own.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Nash

Australia Australia
David worked in the computer industry for 20 years as a hardware and network specialist. He taught himself to program C++ in 2002, and continues to dabble in C++ as a hobbie.

Comments and Discussions

Questioncreating text editor using c/c++ WITHOUT using visual studio PinmemberRahul Kumar13-Nov-13 6:58 
GeneralMy vote of 4 Pinmemberilyas VaxSoft18-Dec-12 2:28 
Generalget the full code of your text editor PinmemberNayana Uday21-Nov-10 23:26 
Generalfull code for project development Pinmemberpartha_sumu10-Sep-09 5:40 
GeneralRe: full code for project development PinmemberDavid Nash10-Sep-09 18:04 
GeneralI can't get a pop-up menu on the window using OnRButtonClick PinmemberMichael B Pliam14-Jul-09 12:19 
QuestionHas anyone figured out how to do this using SDI Document View architecture? PinmemberMichael B Pliam24-Jun-09 21:27 
GeneralMy vote of 2 Pinmembermicron_rt10-May-09 23:34 
How about formating? This code doesn't have too much RichText-specific functionality. It uses only EDITSTREAM and a few other control's features. In other words - I expexted something more from a demo which would use a CRichEditCtrl.
GeneralNice Pinmemberbehzadsh2-Jan-09 8:45 
Generalprint - GetTextLength Pinmembert-error29-Aug-08 3:43 
GeneralThanks for your code PinmemberAntonio292923-Oct-07 0:20 
GeneralVery good PinmemberRafael Melo24-Apr-07 8:34 
GeneralRegarding Printing PinmemberDavid Nash24-Apr-07 20:42 
Questionhow to start Pinmemberhodasj4-Feb-07 7:43 
AnswerRe: how to start PinmemberDavid Nash5-Feb-07 18:13 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 20 Feb 2005
Article Copyright 2005 by David Nash
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid