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.
class CMainFrame : public CFrameWnd
The next step is to create the rich text window over the client area of the frame window.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
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");
::SetWindowLong(m_RichEdit.GetSafeHwnd(), WL_EXSTYLE, WS_EX_CLIENTEDGE);
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
DragQueryFile acquires the name of the dropped file, and
ReadFile reads the contents of the file into the
void CMainFrame::OnDropFiles(HDROP hDropInfo)
::DragQueryFile(hDropInfo, 0, FileName.GetBufferSetLength
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.