Experiment No. 2: Aim: - Get Familiar With Essential Classes in A Typical (Document-View Architecture) VC++
Experiment No. 2: Aim: - Get Familiar With Essential Classes in A Typical (Document-View Architecture) VC++
2
Aim: - Get familiar with essential classes in a typical (Document- view architecture)VC++
Program and their relationship with each other.
Parts of Application
The application source code generated for you by AppWizard actually consists of 4 major
classes. All these classes can be seen by expanding the class listing in the Class View tab.
For each of these classes corresponding header file (.h) and an implementation file (.cpp) is
generated.
The MainFrame Window Class
This class is named CMainFrame and is derived from the MFC class CFrameWnd. This
class manages the main window of the application that contains the window frame, Menu
bar, Toolbar, Status bar, System menu and Minimise, Maximise and Close boxes and also
contains the view window of the application.
The Application Class
This class named C ProjectNameApp is derived from the MFC class CWinApp. It manages
the application as a whole by managing the tasks, like initialising the application instance,
managing the message loop and performing final cleanup of the program.
The Document Class
This class named C ProjectNameDoc is derived from the MFC class CDocument. It is
responsible for storing the program data as a single string and reading and writing this
data to disk files.
The View Class
The class named C projectNameView is derived from CView class from the MFC classes.It
manages the view window, which is used to display program data on the screen and also
used in processing the input from the user.
MFC and AppWizard use the Document/View architecture to organize programs written for
Windows. Document/View separates the program into four main classes:
Each of these classes has a specific role to play in an MFC Document/View application.
The document class is responsible for the program's data. The view class handles
interaction between the document and the user. The frame class contains the view and other
user interface elements, such as the menu and toolbars. The application class is responsible
for actually starting the program and handling some general-purpose interaction with
Windows. Figure shows the four main parts of a Document/View
create almost any type of Windows program. One of the big advantages of the
Document/View architecture is that it divides the work in a Windows program into welldefined categories. Most classes fall into one of the four main class categories:
Controls and other user-interface elements related to a specific view
Data and data-handling classes, which belong to a document
Work that involves handling the toolbar, status bar, and menus, usually belonging to the
frame class
Interaction between the application and Windows occurring in the class derived from
CWinApp
Dividing work done by your program helps you manage the design of your program more
effectively. Extending programs that use the Document/View architecture is fairly simple
because the four main Document/View classes communicate with each other through
well-defined interfaces. For example, to change an SDI program to an MDI program, you
must write little new code. Changing the user interface for a Document/View program
impacts only the view class or classes; no changes are needed for the document, frame, or
application classes.
DOCUMENTS
Documents are the basic elements that are created and manipulated by the application.
Windows provides a graphic interface that gives a user a natural way to use the
application. To implement this interface, the developer has to provide ways to see and
interact with the information that the application creates and uses.
A document is simply a place to collect common data elements that form the processing
unit for the application.
Documents and the Application Relationship
The application class uses documents as a way to organize and present information to the
user. Each application derived from the MFC defines at least one type of document that is a
part of the application. The type of document and the number of documents that the
application uses are defined in the code of the application class.
As we have already discussed, MFC supports two types of applications MDI and SDI.
MDI - In applications that support Multiple Document Interface, a number of text files can
be opened for editing at once; each in a different window. Each of the open files has a
corresponding document. Also, in MDI, the same document can have multiple views,
where a window is split. Each pane of the window can show a different portion of the data
whereas this data is coming from a common source, the document.
SDI - In applications with Single Document Interface only one document is open at a
time. A SDI application does not have a Window menu and the File menu does not have a
Close option because only one document can be open at a time, opening a document
automatically closes the current document. These are two common characteristics of a SDI
application.
Why Documents
The fundamental responsibility of the document is to store all the elements that constitute an
application unit. An application may support more than one type of data like numbers, text,
drawings etc.
The document also controls all the views associated with it. As the user opens and
manipulates windows on the screen, views are created and the document is associated
with each view. The document is responsible for controlling and updating the elements
within a given view.
The view may either request the document to draw its components or directly request the
components to draw themselves. It must provide a device context where the drawing occurs.
All elements of a drawing must be able to display themselves correctly upon request.
VIEWS
The view is the users window to the document. The user interacts with a document using
the view. Each active document will have one or more active views available on the display.
Generally, each view is displayed in a single window.
Gaining Access to Document Data from the View
The view accesses its documents data either with the GetDocument() function, which returns
a pointer to the document or by making the view class a C++ friend of the document class.
The view then uses its access to the data to obtain the data when it is ready to draw or
otherwise manipulate it.For example, from the views OnDraw() member function, the
view usesGetDocument()to obtain a document pointer. Then it uses that pointer to access
a CString data memberin the document. The view passes the string to the TextOut()
function.