📄 ch01.htm
字号:
//
#if !defined(AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)
#define AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include `stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CFirstMDIApp:
// See FirstMDI.cpp for the implementation of this class
//
class CFirstMDIApp : public CWinApp
{
public:
CFirstMDIApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFirstMDIApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CFirstMDIApp)
afx_msg void OnAppAbout();
// NOTE - The ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately
// before the previous line.
</PRE>
<PRE>#endif //!defined(AFX_FIRSTMDI_H__CDF38D9E_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)
</PRE>
<P>How does this differ from FirstSDI.h? Only in the classnames. The constructor
is also the same as before. OnAppAbout() is just like the SDI version. How about
InitInstance()? It is in Listing 1.4.</P>
<P>
<H4>Listing 1.4  CFirstMDIApp::InitInstance()</H4>
<PRE>BOOL CFirstMDIApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and want to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you don't need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which your settings are stored.
// You should modify this string to be something appropriate,
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including // MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows, and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_FIRSTMTYPE,
RUNTIME_CLASS(CFirstMDIDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CFirstMDIView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
</PRE>
<PRE>}
</PRE>
<P>What's different here? Using WinDiff can help. WinDiff is a tool that comes with
Visual C++ and is reached from the Tools menu. (If WinDiff isn't on your Tools menu,
see the "Tools" section of Appendix C.) Using WinDiff to compare the FirstSDI
and FirstMDI versions of InitInstance() confirms that, other than the classnames,
the differences are</P>
<UL>
<LI>The MDI application sets up a CMultiDocTemplate and the SDI application sets
up a CSingleDocTemplate, as discussed in Chapter 4.
<P>
<LI>The MDI application sets up a mainframe window and then shows it; the SDI application
does not.
</UL>
<P>This shows a major advantage of the Document/View paradigm: It enables an enormous
design decision to affect only a small amount of the code in your project and hides
that decision as much as possible.</P>
<P>
<H2><A NAME="Heading29"></A>Understanding the Components of a Dialog-Based Application</H2>
<P>Dialog applications are much simpler than SDI and MDI applications. Create one
called <I>FirstDialog</I>, with an About box, no Help, 3D controls, no automation,
ActiveX control support, no sockets, source file comments, and MFC as a shared DLL.
In other words, accept all the default options.</P>
<P>Three classes have been created for you for the application called <I>FirstMDI</I>:</P>
<UL>
<LI>CAboutDlg, a dialog class for the About dialog box
<P>
<LI>CFirstDialogApp, a CWinApp class for the entire application
<P>
<LI>CFirstDialogDlg, a dialog class for the entire application
</UL>
<P>The dialog classes are the subject of Chapter 2. Listing 1.5 shows the header
file for CFirstDialogApp.</P>
<P>
<H4>Listing 1.5  dialog16.h--Main Header File</H4>
<PRE>// FirstDialog.h : main header file for the FIRSTDIALOG application
//
#if !defined(AFX_FIRSTDIALOG_H__CDF38DB4_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)
#define AFX_FIRSTDIALOG_H__CDF38DB4_8718_11D0_B02C_0080C81A3AA2__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#ifndef __AFXWIN_H__
#error include `stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CFirstDialogApp:
// See FirstDialog.cpp for the implementation of this class
//
class CFirstDialogApp : public CWinApp
{
public:
CFirstDialogApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFirstDialogApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CFirstDialogApp)
// NOTE - The ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately
// before the previous line.
#endif // !defined(AFX_FIRSTDIALOG_H__CDF38DB4_8718_11D0_B02C_0080C81A3AA2
</PRE>
<PRE>¬__INCLUDED_)
</PRE>
<P>CFirstDialogApp inherits from CWinApp, which provides most of the functionality.
CWinApp has a constructor, which does nothing, as did the SDI and MDI constructors
earlier in this chapter, and it overrides the virtual function InitInstance(), as
shown in Listing 1.6.</P>
<P>
<H4>Listing 1.6  FirstDialog.cpp--CDialog16App::InitInstance()</H4>
<PRE>BOOL CFirstDialogApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and want to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you don't need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CFirstDialogDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Because the dialog has been closed, return FALSE so that you exit the
// application, rather than start the application's message pump.
return FALSE;
</PRE>
<PRE>}
</PRE>
<P>This enables 3D controls, because you asked for them, and then puts up the dialog
box that is the entire application. To do that, the function declares an instance
of CDialog16Dlg, dlg, and then calls the DoModal() function of the dialog, which
displays the dialog box onscreen and returns IDOK if the user clicks OK, or IDCANCEL
if the user clicks Cancel. (This process is discussed further in Chapter 2.) It's
up to you to make that dialog box actually do something. Finally, InitInstance()
returns FALSE because this is a dialog-based application and when the dialog box
is closed, the application is ended. As you saw earlier for the SDI and MDI applications,
InitInstance() usually returns TRUE to mean "everything is fine--run the rest
of the application" or FALSE to mean "something went wrong while initializing."
Because there is no "rest of the application," dialog-based apps always
return FALSE from their InitInstance().</P>
<P>
<H2><A NAME="Heading30"></A>Reviewing AppWizard Decisions and This Chapter</H2>
<P>AppWizard asks a lot of questions and starts you down a lot of roads at once.
This chapter explains InitInstance and shows some of the code affected by the very
first AppWizard decision: whether to have AppWizard generate a dialog-based, SDI,
or MDI application. Most of the other AppWizard decisions are about topics that take
an entire chapter. The following table summarizes those choices and where you can
learn more:</P>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Step</B></TD>
<TD ALIGN="LEFT"><B>Decision</B></TD>
<TD ALIGN="LEFT"><B>Chapter</B></TD>
<TD ALIGN="LEFT"><B>Dialog</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">0</TD>
<TD ALIGN="LEFT">MFC DLL or</TD>
<TD ALIGN="LEFT">28, Future Explorations</TD>
<TD ALIGN="LEFT"></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">non-MFC DLL</TD>
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT"></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">0</TD>
<TD ALIGN="LEFT">OCX Control</TD>
<TD ALIGN="LEFT">17, Building an ActiveX</TD>
<TD ALIGN="LEFT"></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT"></TD>
<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -