Click here to Skip to main content
15,885,645 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
2.64/5 (28 votes)
18 Nov 20055 min read 91K   3.8K   20   16
An article good for beginners giving step by step instructions.

Project Screenshot

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

  • Start Visual C++.

  • Go to File -> New.

  • Click on Projects, if it isn't already selected.

  • Select MFC AppWizard (exe), type a "Project name" and click OK.

  • Choose "Dialog based" and click Finish, then click OK.

  • Delete the TODO: Place dialog controls here. static text.
    (by clicking on it once and pressing the delete key on the keyboard)

  • And also delete the OK and Cancel buttons.

  • Now go to Project -> Add To Project -> Components And Controls...

  • Then click on "Registered ActiveX Controls"

  • Find "Microsoft Common Dialog Control" and double click it.

  • Click OK.

  • And click OK again.

  • Then click Close.

  • A new icon should now appear. Image 2

  • Click that, then click on the dialog.

  • Resize the thing that appears, make it a bit smaller.

  • Right click on the dialog, and select "Properties".

  • Go to "Styles" check "Minimize box", then click on the [x].

  • Resize the dialog around how big you want the text editor window to be.

  • Move the "Common Dialog" thing to the bottom.

  • Click on the button icon, then click near the bottom of the dialog.

  • Right click the button that appeared and select Properties.

  • Select "General" if it isn't already selected.

  • Then delete the Button1 text for the "Caption" and type &Clear.

  • Delete the 1 on IDC_BUTTON1 and type _CLEAR, renaming it to IDC_BUTTON_CLEAR, for the ID, then click [x].

  • Use those same steps to make a button called &Open... and Save &As... using the appropriate ID.

  • Now click on the 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.

  • Click on the massive Edit box that you drew (once), then right click it and select Properties.

  • Select Styles, uncheck "Auto HScroll".

  • Check "Multiline".

  • And check "Vertical scroll".

  • Also check "Auto VScroll".

  • Finally check "Want return" and click the [x].

  • Now it's time to test out what you did so far, go to Build -> Execute -> OK.

    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.

    C++
    #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...

    C++
    #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.

  • Right click the dialog and select "ClassWizard".

  • Select "Member Variables" if it isn't already selected.

  • Select IDC_COMMONDIALOG1, and click "Add Variable".

  • Name the "Member variable name" as m_dialogbox and click OK.

  • Now select IDC_EDIT1, and click "Add Variable".

  • Name the "Member variable name" as m_buffer and click OK.

  • Click OK again.

  • Double click the massive Edit box, click Ok, it should make a function called, OnChangeEdit1.

  • Paste the following code under the comment
    TODO: Add your control notification handler code here
    .

    C++
    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.

  • Close the code window to go back to the resource thing.

  • Double click the "Clear" button, click OK, it should make a function called, OnButtonClear.

  • Now paste the following code under the comment TODO: Add your control notification handler code here .

    C++
    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.

  • Close the code window if you already haven't to go back to the resource thing.

  • Double click the "Open" button, click OK, it should make a function called, OnButtonOpen.

  • Paste the following code under the comment TODO: Add your control notification handler code here.

    C++
    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.

  • Close the code window if you already haven't to go back to the resource thing again.

  • Double click the "Save As" button, click OK, it should make a function called, OnButtonSaveAs.

  • Finally paste the following code under the comment TODO: Add your control notification handler code here .

    C++
    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.

  • 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
    Web Developer
    United Kingdom United Kingdom
    Necdet got his first computer in 1996 at the age of 11, a 386 at 3MHz, with 4MB RAM and a 10MB hard disk. His next computer was a 486 which he got 6 months later, he was shown how to remove and install hardware on that computer. Then he got a Pentium one year after that, and then a Pentium 3 in 2000 which he still uses. He learned programming in 1999, when he finally had access to the Internet and was able to find tutorials on C++, he then found tutorials on ANSI standard C, and didn't see the point in C++ so he used ANSI C instead for all his programs. He made lots of small but useful programs (almost 100) until 2000, but with an unfortunate hard disk crash, all his programs were lost which made him very dishearted about programming. He done no programming whatsoever until 2003, when he decided that people could learn programming from his simple programs so he tried to remember all the programs he wrote in 1999-2000 which were lost in the hard disk crash, and rewrite them from scratch, as a single program having all the functions of all of them. He called this program najitool, and posted the first version on sourceforge in 2004 with 9 functions he rewrote which he could remember. In 2005, he realized that most people didn't use command line programs, so he decided to make a Windows GUI (Graphical User Interface) version of najitool called najitool GUI a.k.a. naji_gui, written in Visual C++ 6.0 using MFC.

    Comments and Discussions

     
    GeneralMy vote of 1 Pin
    auge__10-Jun-10 1:18
    auge__10-Jun-10 1:18 
    Questionis this program only can open .text file?if i want open other file,how can i do that? Pin
    jet_zeng3-Aug-07 3:09
    jet_zeng3-Aug-07 3:09 
    GeneralH [modified] Pin
    animesh tripathi3-Mar-07 23:25
    animesh tripathi3-Mar-07 23:25 
    GeneralI have a problem!!!! Pin
    animesh tripathi16-Feb-07 19:47
    animesh tripathi16-Feb-07 19:47 
    GeneralRe: I have a problem!!!! Pin
    NECDET COKYAZICI10-Mar-07 11:47
    NECDET COKYAZICI10-Mar-07 11:47 
    Questiontext editor Pin
    anysiddiqui13-Feb-07 22:49
    anysiddiqui13-Feb-07 22:49 
    AnswerRe: text editor Pin
    NECDET COKYAZICI10-Mar-07 11:21
    NECDET COKYAZICI10-Mar-07 11:21 
    Generalplease answer me ! Pin
    hodasj8-Feb-07 1:30
    hodasj8-Feb-07 1:30 
    Generala good artical Pin
    wzh198312219-Dec-06 23:26
    wzh198312219-Dec-06 23:26 
    GeneralThank you a lot Pin
    myvr25-May-06 1:25
    myvr25-May-06 1:25 
    GeneralRe: Thank you a lot Pin
    NECDET COKYAZICI28-May-06 21:50
    NECDET COKYAZICI28-May-06 21:50 
    GeneralAccelerators Pin
    Roger6521-Nov-05 1:31
    Roger6521-Nov-05 1:31 
    QuestionHow To Make A Very Simple Text Editor Using MFC in Visual C++ Pin
    PJ Arends19-Nov-05 10:05
    professionalPJ Arends19-Nov-05 10:05 
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ Pin
    NECDET COKYAZICI19-Nov-05 10:31
    NECDET COKYAZICI19-Nov-05 10:31 
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ Pin
    Sam Hobbs13-Sep-06 10:55
    Sam Hobbs13-Sep-06 10:55 
    Also see my Simple CEditView Application[^].
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ Pin
    anysiddiqui14-Mar-07 18:59
    anysiddiqui14-Mar-07 18:59 

    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.