Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Creating your first MFC Doc/View application

Rate me:
Please Sign up or sign in to vote.
4.28/5 (27 votes)
10 Sep 2000CPOL 317.8K   7.1K   86   29
A brief step-by-step tutorial that demonstrates creating an SDI and MDI based applications using the MFC Doc/View architecture.
  • Download demo SDI application - 26 Kb
  • Download demo MDI application - 28 Kb
  • Introduction

    Creating applications using the MFC document/view architecture can save time, can help you create more structured programs, and can also help alleviate a lot of tedious boilerplate coding that you may otherwise be forced to write. It can, however, force you to structure your applications in a way that doesn't quite fit the problem you are trying to solve (such as games), and it does also mean you are forced to carry the overhead of using the MFC runtime libraries. If you are sure that your user will have the version of MFC already installed, or you don't mind including it in a setup program, and you don't mind an executable that is a touch larger than an equivalent win32/SDK version then MFC can save you a lot of time.

    The doc/view architecture that is mentioned a lot in MFC literature simply refers to the practice of separating your appplications data storage and minipulation logic from the data visualisation logic. Basically, you have a CDocument derived class to load, manipulate and store your data, and a CView derived class to display the data. Your document and view classes (you can have multiple document and multiple view classes within a single app) are linked together using internal MFC classes by your main application's (CWinApp derived) class, and physically managed on the screen by a Main Frame (CFrameWnd or CMDIChildWnd derived) class.

    In Single Document Interface (SDI) applications, there is one CFrameWnd class and and one view. Each time a document is loaded the view is cleared and redrawn using the information in the new document. The view is essentially reused.

    The image below shows a typical SDI application. The main window contains the menu, toolbar, status bar, and the view window. The menus, toolbar, status bar are created and owned by the CFrameWnd class. You can create entire applications without having to touch the frame class at all.

    Image 1

    The CView class has, as its display area, the dotted section shown in the image. Everytime that area needs repainting the class' OnDraw method will be called, and the view will be expected to query it's associated document so it knows what to draw.

    In Multiple Document Interface (MDI) applications there is one main frame per application (in this case a CMDIFrameWnd, and one CMDIChildWnd derived child frame for each document.

    Image 2

    Each time a new document is opened a new CMDIChildWnd is created that lives inside the main applications CMDIFrameWnd window. The main frame window contains and owns the menus, toolbars and status bar, and each CMDIChildWnd window contains a view window. When you switch between different child frames the main frame automatically updates the menu and toolbars to match that associated with the current view inside the child frame.

    In Visual Studio .NET there is a third option that creates a new CFrameWnd each time a new document is opened. This option won't be covered here yet.

    It's important to note that each view can only be associated with one document and one frame window. A CMDIFrameWnd window contains zero or more CMDIChildWnd windows, and each CFrameWnd or CMDIChildWnd contains one CView window. However, each document can have more than one view associated with it. A typical example is the case of a splitter window that can be split to show two different views of the same document.

    Creating an application

    Creating an MFC doc/view application is very simple. Fire up the AppWizard (File | New...) and follow along.

    Image 3

    First we select the MFC AppWizard.

    Image 4

    Choose either MDI or SDI (dialog based have been covered elsewhere) and click Next until you hit step 4.

    Image 5

    This step allows you to customise a bunch of stuff including toolbars, menus, status bar and print preview. Check the boxes and you get all this for free! One important step here is the "Advanced" button. This allows you to specify a file extension that your application will open by default - and which the Windows Shell will associate with your application should the user double click on a file of that type.

    Image 6

    Here I've simple entered "my" as my file extension. Once the program is compiled and run for the first time, all .my files will have the icon specified by the IDR_MYSDIATYPE resource (the actual resource name will obviously change with the name of your application).

    Continue clicking Next and you will come to the final step that allows you to choose the type of view you want. MFC provides a ton of different CView derived types to make life easier, including HTML viewers, scrolling views, views that wrap common controls and a dialog-type Form view.

    Image 7

    Once the wizard has finished you have an application that can be compiled and run immediately. It won't do anything useful, but in a way it already does a lot: toolbars, status bars, menu, printing and print preview and file type registration. All that remains is for you to fill in the details of your document (Serialize will load and save documents, OnNewDocument will be called to create a new document), and your view (OnDraw to do the drawing, and OnInitialUpdate for initialising variables when the view is first created).

    To customise the menus, icons and toolbars simply use the resource editors provided with Visual Studio. Life doesn't get any easier.

    Image 8

    Further Reading

    For further tutorials, check out

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Founder CodeProject
    Canada Canada
    Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

    In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

    In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

    Comments and Discussions

     
    GeneralCool - still 'Featuring' after a decade! Pin
    Kyudos4-Nov-10 11:35
    Kyudos4-Nov-10 11:35 
    QuestionChanging the Application name in SDI Pin
    AVIHAR2-Dec-08 1:33
    AVIHAR2-Dec-08 1:33 
    QuestionMDI New Document [modified] Pin
    AllAtSea26-Aug-07 23:29
    AllAtSea26-Aug-07 23:29 
    GeneralCButton Placement Pin
    Walt Austin9-Apr-07 15:59
    Walt Austin9-Apr-07 15:59 
    QuestionSerialize to certain file type Pin
    N-O-R-B-E-R-T5-May-06 3:00
    N-O-R-B-E-R-T5-May-06 3:00 
    Questiontwo MDI frame windows in a single mdi app or two frame windows in single sdi app? Pin
    v_srinu_26_f17-Feb-06 3:25
    v_srinu_26_f17-Feb-06 3:25 
    AnswerRe: two MDI frame windows in a single mdi app or two frame windows in single sdi app? Pin
    v_srinu_26_f17-Feb-06 4:50
    v_srinu_26_f17-Feb-06 4:50 
    Questionhow to show two different rtf file in a single window Pin
    GnanaprakashJebaraj28-Jun-05 4:10
    GnanaprakashJebaraj28-Jun-05 4:10 
    GeneralQuestion Please Help Pin
    edwardking2-Jan-04 1:55
    edwardking2-Jan-04 1:55 
    Generaltaaaatataaa Pin
    Anonymous29-Dec-03 4:52
    Anonymous29-Dec-03 4:52 
    GeneralMDI / SDI in a dialog based Application Pin
    HohohoMyHo28-Oct-03 22:05
    HohohoMyHo28-Oct-03 22:05 
    GeneralOne Document and two views Pin
    MuhammadAsifJavaid21-Jun-03 0:42
    MuhammadAsifJavaid21-Jun-03 0:42 
    QuestionWhat the @#$%^&* Pin
    Anonymous23-May-03 9:20
    Anonymous23-May-03 9:20 
    AnswerRe: What the @#$%^&* Pin
    Chris Maunder23-May-03 9:31
    cofounderChris Maunder23-May-03 9:31 
    QuestionHow do I specify CView's size? Pin
    Anonymous15-Dec-02 10:25
    Anonymous15-Dec-02 10:25 
    AnswerRe: How do I specify CView's size? Pin
    Member 144234215-Oct-04 1:57
    Member 144234215-Oct-04 1:57 
    Questionam I stupide ? Pin
    Anonymous7-Sep-02 3:17
    Anonymous7-Sep-02 3:17 
    AnswerRe: am I stupide ? Pin
    Brian Delahunty20-Oct-02 3:02
    Brian Delahunty20-Oct-02 3:02 
    AnswerRe: am I stupide ? Pin
    Balkrishna Talele18-Dec-03 18:56
    Balkrishna Talele18-Dec-03 18:56 
    Questionserialize? Pin
    DuFF14-Aug-02 17:58
    DuFF14-Aug-02 17:58 
    AnswerRe: serialize? Pin
    RobMac22-Dec-03 5:00
    RobMac22-Dec-03 5:00 
    GeneralThanks! ... but Pin
    rbc22-Apr-02 7:06
    rbc22-Apr-02 7:06 
    GeneralMultiple Views with one Document Pin
    akraus1-Oct-01 3:57
    akraus1-Oct-01 3:57 
    GeneralRe: Multiple Views with one Document Pin
    Anonymous1-Jun-05 20:14
    Anonymous1-Jun-05 20:14 
    GeneralImporting existing dialog interfaces into a new SDI application. Pin
    Kimball M. Rudeen27-May-01 9:50
    Kimball M. Rudeen27-May-01 9:50 

    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.