Visual C++/MFC Tutorial – Lesson 6: SDI and MDI Applications

Lesson 6: SDI and MDI Applications

We are getting to some advanced stuff now. In this lesson I am not going
to go in depth at all. I will just give you a flavor of the structure of
a SDI (single document interface) and a MDI (multiple document interface)
application. In the last lesson we will build a SDI application and you
can see the nitty gritty there.

The SDI application is typically used when you intend to work with only
one data set at a time. For instance, the program notepad.exe is a SDI
application. Netscape is also an SDI application. At any one time, there
is only one document open at a time. Word for Windows and the VC++ developer
studio are MDI applications. In these you can have several documents opened
at once. This is particularly useful when you want to cut and paste between
documents. Another use for MDI applications is to have one document, but
several different views open that view the data differently.  A graphing
application comes to mind where in one window you have a spreadsheet-like
data list, and in another window you have a plot of the data. For small
applications, a SDI application will usually be all you need. After you
master it, the jump to MDI is a snap. Let’s go over the structure of a
SDI app.

Remember that in a dialog app, we had just two main classes. CWinApp
and CDialog. Here again we have a CWinApp which serves
the same purpose as it did in lesson 5. The CDialog class however
is replaced by 3 other classes: CMainFrame, CDocument,
and CView.

CDocument is a class that has no display, and typically doesn’t
react much with the messaging system of windows. It is used as a class
to manage your data. MFC will create the code automatically which handles
the event of File->Save, File->SaveAs, File->Close, and File->Open. All
you need to do is to fill in the blank functions in the CDocument

Next is the CView. Most likely you will spend more time writing
code to display and interact with the document’s data then you will writing
any other code. This is where the CView comes in. The CView
is a class derived from CWnd, which is used for displaying your
CDocument in some way. It is also one of the places where you
can handle events like mouse clicks and what not. The heart of the CView
is usually a call to get a pointer to your document followed by some drawing
routines to display the data in your document.

The CMainFrame acts as a way to bridge the gap between your
document/view classes and the rest of the application. Do you see that
frame which goes all around applications boarders? That is the Main Frame
window of the application. The title bar, the menu, the scroll bars, the
status bar, and the tool bars are all part of the main frame window in
an SDI application.  You typically put the code to handle these objects
in the CMainFrame class. The CMainFrame class is the
main window of the application. The CView class is typically a
child window of the CMainFrame. (For the most part, the child/parent
window relations just tell windows what windows are ‘stuck’ to what other
windows. If you move a parent window, all of the children will move also.
If you destroy a parent window, all of the children will be destroyed.

You should have a pretty good idea now of how SDI applications are constructed.
MDI applications are similar, but there can be several CDocument
classes in existence at the same time and there is an added class called
CChildFrame which acts as a connection between the CView
and the CMainFrame.

More by Author

Must Read