MFC Supports Two Types of Document
MFC Supports Two Types of Document
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.
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.