Click here to Skip to main content
Licence 
First Posted 18 Nov 2005
Views 45,012
Bookmarked 20 times

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

By | 18 Nov 2005 | Article
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.

  • 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.

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

  • 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 .

        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 .

        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.

    
    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 .

    
    
    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

    About the Author

    NECDET COKYAZICI

    Web Developer

    United Kingdom United Kingdom

    Member

    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.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralMy vote of 1 Pinmemberauge__1:18 10 Jun '10  
    Questionis this program only can open .text file?if i want open other file,how can i do that? Pinmemberjet_zeng3:09 3 Aug '07  
    GeneralH [modified] Pinmemberanimesh tripathi23:25 3 Mar '07  
    GeneralI have a problem!!!! Pinmemberanimesh tripathi19:47 16 Feb '07  
    GeneralRe: I have a problem!!!! PinmemberNECDET COKYAZICI11:47 10 Mar '07  
    Questiontext editor Pinmemberanysiddiqui22:49 13 Feb '07  
    AnswerRe: text editor PinmemberNECDET COKYAZICI11:21 10 Mar '07  
    Generalplease answer me ! Pinmemberhodasj1:30 8 Feb '07  
    Generala good artical Pinmemberwzh1983122123:26 9 Dec '06  
    GeneralThank you a lot Pinmembermyvr1:25 25 May '06  
    GeneralRe: Thank you a lot PinmemberNECDET COKYAZICI21:50 28 May '06  
    GeneralAccelerators PinmemberRoger651:31 21 Nov '05  
    QuestionHow To Make A Very Simple Text Editor Using MFC in Visual C++ PinmemberPJ Arends10:05 19 Nov '05  
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ PinmemberNECDET COKYAZICI10:31 19 Nov '05  
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ PinmemberSam Hobbs10:55 13 Sep '06  
    AnswerRe: How To Make A Very Simple Text Editor Using MFC in Visual C++ Pinmemberanysiddiqui18:59 14 Mar '07  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 18 Nov 2005
    Article Copyright 2005 by NECDET COKYAZICI
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid