Click here to Skip to main content
14,740,854 members
Articles » Languages » C / C++ Language » Utilities
Posted 20 Feb 2005


44 bookmarked

Text Editor in C++ using CRichEditCtrl

Rate me:
Please Sign up or sign in to vote.
4.29/5 (24 votes)
20 Feb 2005
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 Pin
Rahul Kumar13-Nov-13 6:58
MemberRahul Kumar13-Nov-13 6:58 
GeneralMy vote of 4 Pin
ilyas VaxSoft18-Dec-12 2:28
Memberilyas VaxSoft18-Dec-12 2:28 
Generalget the full code of your text editor Pin
Nayana Uday21-Nov-10 23:26
MemberNayana Uday21-Nov-10 23:26 
Generalfull code for project development Pin
partha_sumu10-Sep-09 5:40
Memberpartha_sumu10-Sep-09 5:40 
GeneralRe: full code for project development Pin
David Nash10-Sep-09 18:04
MemberDavid Nash10-Sep-09 18:04 
GeneralI can't get a pop-up menu on the window using OnRButtonClick Pin
Michael B Pliam14-Jul-09 12:19
MemberMichael B Pliam14-Jul-09 12:19 
QuestionHas anyone figured out how to do this using SDI Document View architecture? Pin
Michael B Pliam24-Jun-09 21:27
MemberMichael B Pliam24-Jun-09 21:27 
GeneralMy vote of 2 Pin
micron_rt10-May-09 23:34
Membermicron_rt10-May-09 23:34 
GeneralNice Pin
behzadsh2-Jan-09 8:45
Memberbehzadsh2-Jan-09 8:45 
Generalprint - GetTextLength Pin
t-error29-Aug-08 3:43
Membert-error29-Aug-08 3:43 
GeneralThanks for your code Pin
Antonio292923-Oct-07 0:20
MemberAntonio292923-Oct-07 0:20 
GeneralVery good Pin
Rafael Melo24-Apr-07 8:34
MemberRafael Melo24-Apr-07 8:34 
GeneralRegarding Printing Pin
David Nash24-Apr-07 20:42
MemberDavid Nash24-Apr-07 20:42 
Questionhow to start Pin
hodasj4-Feb-07 7:43
Memberhodasj4-Feb-07 7:43 
AnswerRe: how to start Pin
David Nash5-Feb-07 18:13
MemberDavid Nash5-Feb-07 18:13 

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.