65.9K
CodeProject is changing. Read more.
Home

Text Editor in C++ using CRichEditCtrl

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.29/5 (22 votes)

Feb 20, 2005

2 min read

viewsIcon

166486

downloadIcon

11472

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.