Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

Text Editor in C++ using CRichEditCtrl

Rate me:
Please Sign up or sign in to vote.
4.29/5 (24 votes)
20 Feb 20052 min read 163.8K   11.4K   45   15
Simple text editor featuring drag-and-drop and printing.

Sample Image - TextEdit.gif

Introduction

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.

Background

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.

Implementation

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
{
public:
    CMainFrame();
    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 |
         ES_AUTOHSCROLL | ES_AUTOHSCROLL | ES_MULTILINE | 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();

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);
    
    FileName.ReleaseBuffer();
    ::DragFinish(hDropInfo);

    m_strPathname = FileName;
    SetWindowTitle();
    ReadFile();
}

Acknowledgments

Mastering MFC Development Using Microsoft Visual C++ 6.0.

Conclusion

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.

License

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


Written By
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 5:58
Rahul Kumar13-Nov-13 5:58 
GeneralMy vote of 4 Pin
ilyas VaxSoft18-Dec-12 1:28
ilyas VaxSoft18-Dec-12 1:28 
Generalget the full code of your text editor Pin
Nayana Uday21-Nov-10 22:26
Nayana Uday21-Nov-10 22:26 
Generalfull code for project development Pin
partha_sumu10-Sep-09 4:40
partha_sumu10-Sep-09 4:40 
GeneralRe: full code for project development Pin
David Nash10-Sep-09 17:04
David Nash10-Sep-09 17:04 
GeneralI can't get a pop-up menu on the window using OnRButtonClick Pin
Michael B Pliam14-Jul-09 11:19
Michael B Pliam14-Jul-09 11:19 
QuestionHas anyone figured out how to do this using SDI Document View architecture? Pin
Michael B Pliam24-Jun-09 20:27
Michael B Pliam24-Jun-09 20:27 
GeneralMy vote of 2 Pin
micron_rt10-May-09 22:34
micron_rt10-May-09 22:34 
GeneralNice Pin
behzadsh2-Jan-09 7:45
behzadsh2-Jan-09 7:45 
Generalprint - GetTextLength Pin
t-error29-Aug-08 2:43
t-error29-Aug-08 2:43 
GeneralThanks for your code Pin
Antonio292922-Oct-07 23:20
Antonio292922-Oct-07 23:20 
GeneralVery good Pin
Rafael Melo24-Apr-07 7:34
Rafael Melo24-Apr-07 7:34 
GeneralRegarding Printing Pin
David Nash24-Apr-07 19:42
David Nash24-Apr-07 19:42 
Questionhow to start Pin
hodasj4-Feb-07 6:43
hodasj4-Feb-07 6:43 
AnswerRe: how to start Pin
David Nash5-Feb-07 17:13
David Nash5-Feb-07 17: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.