0% found this document useful (0 votes)
71 views

MFC Supports Two Types of Document

MFC supports two types of document/view applications: single document interface (SDI) applications that allow only one document open at a time and multiple document interface (MDI) applications that allow multiple documents to be open simultaneously. SDI application frames are derived from CFrameWnd and have a single window, while MDI applications can have multiple document windows open concurrently. The document discusses the differences between SDI and MDI applications and provides details on initializing an SDI application using the MFC framework.

Uploaded by

excitekarthik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

MFC Supports Two Types of Document

MFC supports two types of document/view applications: single document interface (SDI) applications that allow only one document open at a time and multiple document interface (MDI) applications that allow multiple documents to be open simultaneously. SDI application frames are derived from CFrameWnd and have a single window, while MDI applications can have multiple document windows open concurrently. The document discusses the differences between SDI and MDI applications and provides details on initializing an SDI application using the MFC framework.

Uploaded by

excitekarthik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MFC supports two types of document/view applications.

Single document interface (SDI)


applications support just one open document at a time. Multiple document interface (MDI)
applications permit two or more documents to be open concurrently and also support multiple
views of a given document. The WordPad applet is an SDI application; Microsoft Word is an MDI
application.

SDI
SDI application frame windows are derived from the class CFrameWnd.
The MFC library supports two distinct application types: Single Document Interface (SDI) and
Multiple Document Interface (MDI). An SDI application has, only one window. If the application
depends on disk-file "documents," only one document can be loaded at a time. The original
Windows Notepad is an example of an SDI application.

The standard SDI frame menus


The child windows within an SDI main frame window
SDI Document View Architecture
SDI Application
Startup steps in a Microsoft Windows MFC library application:
Windows loads your program into memory.
The global object theApp is constructed. (All globally declared objects are constructed
immediately when the program is loaded.)
Windows calls the global function WinMain, which is part of the MFC library. (WinMain is
equivalent to the non-Windows main function—each is a main program entry point.)
Steps…
WinMain searches for the one and only instance of a class derived from CWinApp.
WinMain calls the InitInstance member function for theApp, which is overridden in your derived
application class.
Your overridden InitInstance function starts the process of loading a document and displaying the
main frame and view windows.
WinMain calls the Run member function for theApp, which starts the processes of dispatching
window messages and command messages.

Object Relationship
Steps for processing SDI InitInstance
1. Create an SDI document template from MFC's CSingleDocTemplate class.
2. Adding to list of document templates
3. Initializing command line info values
4. Processing command line parameters
5. Displaying applications frame window
InitInstance function for an SDI application generated by AppWizard
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate
(IDR_MAINFRAME,
RUNTIME_CLASS (CMyDoc),
RUNTIME_CLASS (CMainFrame),
RUNTIME_CLASS (CMyView)
);

AddDocTemplate (pDocTemplate);
……..
……..
CCommandLineInfo cmdInfo;
ParseCommandLine (cmdInfo);
if (!ProcessShellCommand (cmdInfo)) return FALSE;
m_pMainWnd->ShowWindow (SW_SHOW);
m_pMainWnd->UpdateWindow ();
1. create an SDI document template from CSingleDocTemplate class
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate
( IDR_MAINFRAME,
RUNTIME_CLASS (CMyDoc),
RUNTIME_CLASS (CMainFrame),
RUNTIME_CLASS (CMyView)
);
The template's constructor was passed four parameters: an integer value equal to
IDR_MAINFRAME and three RUNTIME_CLASS pointers.
AppWizard uses the resource ID IDR_MAINFRAME in the code that it generates. The
RUNTIME_CLASS macro surrounding the class names returns a pointer to a CRuntimeClass
structure for the specified class, which enables the framework to create objects of that class at run
time.
2. Adding to list of document templates
After the document template is created, the statement
AddDocTemplate (pDocTemplate);
adds it to the list of document templates maintained by the application object. Each template
registered in this way defines one document type the application supports. SDI applications
register just one document type
3. Initialize command line info values
The statements
CCommandLineInfo cmdInfo; Par
seCommandLine (cmdInfo);
use CWinApp::ParseCommandLine to initialize a CCommandLineInfo object with values
reflecting the parameters entered on the command line, which often include a document file name.
4. Process command line parameters
The statements
if (!ProcessShellCommand (cmdInfo)) return FALSE;
"process" the command line parameters. Among other things, ProcessShellCommand calls
CWinApp::OnFileNew to start the application with an empty document if no file name was entered
on the command line, or CWinApp::OpenDocumentFile to load a document if a document name
was specified. It's during this phase of the program's execution that the framework creates the
document, frame window, and view objects using the information stored in the document template.
ProcessShellCommand returns TRUE if the initialization succeeds and FALSE if it doesn't.
5. Display applications frame window
If initialization is successful, the statements
m_pMainWnd->ShowWindow (SW_SHOW); m_pMainWnd->UpdateWindow ();
display the application's frame window (and by extension, the view) on the screen.
Routing of command messages sent to an SDI frame window.
Example Program - The SdiSquares Application
The program shown in Figure is an SDI document/view application that displays a grid of squares
four rows deep and four columns wide. Initially, each square is colored white. However, you can
change a square's color by clicking it with the left mouse button. By default, clicking changes a
square's color to red. You can select alternate colors from the Color menu and thereby create a
multicolored grid containing squares of up to six different colors.

The SdiSquares Application…


Use AppWizard to create a new project named SdiSquares. In AppWizard's Step 1 dialog box,
choose Single Document as the application type and check the Document/View Architecture
Support box, as shown in Figure

The SdiSquares Application…


In the Step 3 dialog box, uncheck the ActiveX Controls box.
In Step 4, uncheck Docking Toolbar, Initial Status Bar, Printing And Print Preview, and 3D
Controls.

The SdiSquares Application…


Also in the Step 4 dialog box, click the Advanced button and type the letters sqr into the File
Extension box (as shown in Figure) to define the default file name extension for SdiSquares
documents.

The SdiSquares Application…


1. In the Step 6 dialog box, manually edit the class names as CSquaresApp. Everywhere else,
accept the AppWizard defaults.

The SdiSquares Application…


Add the member variables m_clrGrid and m_clrCurrentColor to the document class, and add code
to initialize them to OnNewDocument. AppWizard overrides OnNewDocument, so all you have to
do is add the statements that initialize the data members.

The SdiSquares Application…

You might also like