How To Make A Very Simple Text Editor Using MFC in Visual C++






2.64/5 (28 votes)
Nov 18, 2005
5 min read

92426

3847
An article good for beginners giving step by step instructions.
Introduction
Want to start programming in Visual C++? how about making a simple "text editor" even simpler than notepad for a start?
Here is a step by step guide which will guide you through every step of the way, in my humble opinion, this is one of the best guides
on making a simple text editor with MFC.
In this article you will learn the basics of Visual C++ and how to use Button, Edit box, and Dialog box controls and their various options.
Enjoy...
Creating the Application
TODO: Place dialog controls here.
static text. (by clicking on it once and pressing the delete key on the keyboard)

Button1
text for the "Caption" and type &Clear
.
1
on IDC_BUTTON1
and type _CLEAR
, renaming it to IDC_BUTTON_CLEAR
, for the ID, then click [x].
&Open...
and Save &As...
using the appropriate ID.
ab|
icon then draw an "Edit Box" from the top left of the dialog to all the way to the right of the dialog then draw it downwards towards the buttom
but don't draw past the buttons.
It works, you can type stuff like any text editor, but you can't save or open files just yet, that bit you have to program which I explain bellow.
Using the code
To add opening and saving files functionality to the text editor, you'll need some functions that open files in reading and writing mode, here are some functions which I copied and pasted from my program, najitool GUI which you can use for this text editor program as well. Double click on an empty space on the dialog and paste the following code under the#ifdef _DEBUG
to #endif
stuff.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
So paste the following code right under the above code...
#include <stdio.h>
#include <stdlib.h>
FILE *naji_input;
FILE *naji_output;
CString naji_buffer;
void najin(const char *namein)
{
naji_input = fopen(namein, "rb");
if (naji_input == NULL)
{
naji_buffer.Format("Error, cannot open input file: %s", namein);
MessageBox(0, naji_buffer, "Error", 0);
exit(2);
}
}
void najout(const char *nameout)
{
naji_output = fopen(nameout, "wb");
if (naji_output == NULL)
{
naji_buffer.Format("Error, cannot open output file: %s", nameout);
MessageBox(0, naji_buffer, "Error", 0);
exit(4);
}
}
Now close the window with all the code, to go back to the resource thing with the dialog and stuff.
IDC_COMMONDIALOG1
, and click "Add Variable".
m_dialogbox
and click OK.
IDC_EDIT1
, and click "Add Variable".
m_buffer
and click OK.
OnChangeEdit1
.
TODO: Add your control notification handler code here
.
UpdateData(TRUE);
You need to do this so the member variable m_buffer
for the Edit box gets updated everytime the user types something in the Edit box.
TODO: Add your control notification handler code here
.
m_buffer="";
UpdateData(FALSE);
Now the "Clear" button is fully functional, for the user to clear all the text that they typed, they just click the "Clear" button.
Test it out, go to Build -> Execute, and click OK.
OnButtonOpen
.
TODO: Add your control notification handler code here
.
int a;
CString filename;
m_dialogbox.SetDialogTitle("Open");
m_dialogbox.SetFilter("Text Files|*.txt|All Files|*.*");
m_dialogbox.ShowOpen();
if (m_dialogbox.GetFileName().IsEmpty() == FALSE)
filename = m_dialogbox.GetFileName();
else return;
OnButtonClear();
najin(filename);
while(1)
{
a = fgetc(naji_input);
if (a == EOF) break;
m_buffer += (char) a;
}
UpdateData(FALSE);
fclose(naji_input);
This should make the "Open" button functionly as well now, test it out if you want,remember how to build and execute a program? Build -> Execute, and click OK.
OnButtonSaveAs
.
TODO: Add your control notification handler code here
.
CString filename;
m_dialogbox.SetDialogTitle("Save As");
m_dialogbox.SetFilter("Text Files|*.txt|All Files|*.*");
m_dialogbox.ShowOpen();
if (m_dialogbox.GetFileName().IsEmpty() == FALSE)
filename = m_dialogbox.GetFileName();
else return;
najout(filename);
fputs(m_buffer, naji_output);
fclose(naji_output);
That's all there is to it, all the buttons are now fully functional, and the simple text editor is complete.Try them all out, Build -> Execute -> OK. Don't forget to save your work now, File -> Save All.