📄 ch01.htm
字号:
<li> Win32 Console Application</P>
<li> Win32 Dynamic-Link Library</P>
<li> Win32 Static Library</P>
</ul>
<P>These projects are explained in the following sections.</P>
<P><B>ATL COM AppWizard</B></P>
<P>ATL is the Active Template Library, and it's used to write small ActiveX controls. It's generally used by developers who have already mastered writing MFC ActiveX controls. <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A>, "Building an ActiveX
Control," introduces important control concepts while demonstrating how to build an MFC control; <A HREF="index21.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index21.htm" target="text">Chapter 21</A>, "The Active Template Library," teaches you ATL.</P>
<P><B>Custom AppWizard</B></P>
<P>Perhaps you work in a large programming shop that builds a lot of applications. Although AppWizard saves a lot of time, your programmers may spend a day or two at the start of each project pasting in your own boilerplate, which is material that is the
same in every one of your projects. You may find it well worth your time to build a Custom AppWizard, a wizard of your very own that puts in your boilerplate as well as the standard MFC material. After you have done this, your application type is added to
the list box on the left of the Projects tab of the New dialog box shown in Figure 1.1. Creating and using Custom AppWizards is discussed in <A HREF="index25.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index25.htm" target="text">Chapter 25</A>, "Achieving Reuse with the Gallery and Your Own
AppWizards."</P>
<P><B>Database Project</B></P>
<P>If you have installed the Enterprise Edition of Visual C++, you can create a database project. This is discussed in <A HREF="index23.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index23.htm" target="text">Chapter 23</A>, "SQL and the Enterprise Edition."</P>
<P><B>DevStudio Add-in Wizard</B></P>
<P>Add-ins are like macros that automate Developer Studio, but they are written in C++ or another programming language; macros are written in VBScript. They use Automation to manipulate Developer Studio.</P>
<P><B>ISAPI Extension Wizard</B></P>
<P>ISAPI stands for Internet Server API, and refers to functions you can call to interact with a running copy of Microsoft Internet Information Server, a World Wide Web server program that serves out Web pages in response to client requests. You can use
this API to write DLLs that can be used by programs that go far beyond browsing the Web to automating information retrieval. This process is discussed in <A HREF="index18.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index18.htm" target="text">Chapter 18</A>, "Sockets, MAPI, and the Internet."</P>
<P><B>Makefile</B></P>
<P>If you want to create a project that is used with a different "make" utility than Developer Studio, choose this wizard from the left-hand list in the New Project Workspace dialog box. No code is generated. If you don't know what a make
utility is, don't worry—this wizard is for those who prefer to use a stand-alone tool to replace one portion of Developer Studio.</P>
<P><B>MFC ActiveX Control Wizard</B></P>
<P>ActiveX controls are controls you write that can be used on a Visual C++ dialog, a Visual Basic form, even a Web page. These controls are the 32-bit replacement for the VBX controls many developers were using to achieve intuitive interfaces or to save
reinventing the wheel on every project. <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A>, "Building an ActiveX Control," guides you through building a control with this wizard.</P>
<P><B>MFC AppWizard (DLL)</B></P>
<P>If you want to collect a number of functions into a DLL, and these functions use MFC classes, choose this wizard. (If the functions don't use MFC, choose Win32 Dynamic-link Library, discussed a little later in this section.) Building a DLL is covered
in <A HREF="index28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index28.htm" target="text">Chapter 28</A>, "Future Explorations." App Wizard generates code for you so you can get started.</P>
<P><B>New Database Wizard</B></P>
<P>This wizard is for those with Visual InterDev installed. It simplifies connecting a Web page to an SQL database. </P>
<P><B>Win32 Application</B></P>
<P>There are times when you want to create an application in Visual C++ that does not use MFC and does not start with the boilerplate code that AppWizard produces for you. To create such an application, choose the Win32 Application wizard from the
left-hand list in the Projects tab, fill in the name and folder for your project, and click OK. You are not asked any questions; AppWizard simply creates a project file for you and opens it. You have to create all your code from scratch and insert the
files into the project.</P>
<P><B>Win32 Console Application</B></P>
<P>A console application looks very much like a DOS application, though it runs in a resizable window. It has a strictly character-based interface with cursor keys rather than mouse movement. You use the Console API and character based I/O functions like
<font color="#008000">printf()</font> and <font color="#008000">scanf()</font> to interact with the user. No boilerplate code is generated for you, just an empty project. <A HREF="index28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index28.htm" target="text">Chapter 28</A>, "Future Explorations,"
discusses building and using console applications.</P>
<P><B>Win32 Dynamic-Link Library</B></P>
<P>If you plan to build a DLL that does not use MFC and does not need any boilerplate, choose the Win32 Dynamic-Link Library option rather than MFC AppWizard (dll). You get an empty project created right away with no questions.</P>
<P><B>Win32 Static Library</B></P>
<P>While most code you reuse is gathered into a DLL, you may prefer to use a static library, because that means you do not have to distribute the DLL with your application. Choose this wizard from the left-hand list in the New Project Workspace dialog box
to create a project file into which you can add object files to be linked into a static library, which is then linked into your applications.</P>
<H3><B>Changing Your AppWizard Decisions</B></H3>
<P>Running AppWizard is a one-time task. Assuming you are making a typical application, you choose <U>F</U>ile, <U>N</U>ew, click the Projects tab, enter a name and folder, choose MFC Application (exe), go through the six steps, create the application
starter files and then never touch AppWizard again. But what if you choose, for example, not to have online Help, and then later realize you should have included it?</P>
<P>AppWizard, despite the name, is not really magic. It pastes in bits and pieces of code you need, and you can paste in those very same bits yourself. Here's how to find out what you need to paste in.</P>
<P>First, create a project with the same options you used in creating the project whose settings you wish to change, and don't add any code to it. Second, in a different folder create a project with the same name, and all the same settings except the one
thing you want to change (context-sensitive Help in this example). Now, compare the files using WinDiff, which comes with Visual C++. Now you know what bits and pieces you need to add to your full-of-code project to implement the feature you forgot to ask
AppWizard for.</P>
<P>Some developers, if they discover their mistake soon enough, find it quicker to create a new project with the desired features and then paste their own functions and resources from the partially-built project into the new empty one. It's only a matter
of taste, but after you've gone through either process for changing your mind, you probably will move a little more slowly through those AppWizard dialog boxes.</P>
<H3><B>Understanding AppWizard's Code</B></H3>
<P>The code generated by AppWizard may not make sense to you right away, especially if you haven't written a C++ program before. You don't need to understand this code in order to write your own simple applications. But your programs will be better ones
if you know what they are doing, and so a quick tour of AppWizard's boilerplate code is a good idea. You'll see the guts of an SDI application, an MDI application, and a dialog-based application.</P>
<P>If you didn't create the starter applications FirstSDI, FirstMDI, and FirstDialog, you can open them from the CD-ROM as you read this chapter. If you did create them, open them from your own hard drive. If you're unfamiliar with the Developer Studio
interface, glance through Reference <A HREF="indexb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexb.htm" target="text">Appendix B</A>, "The Developer Studio Interface," to learn how to edit code and look at classes.</P>
<P><A ID="I2" NAME="I2"></A><B>A Single Document Interface Application</B></P>
<P>An SDI application has menus that the user uses to open one document at a time and work with that document. This section presents the code that is generated when you create an SDI application with no database or compound document support, with a
toolbar, a status bar, Help, 3-D controls, source file comments, and with the MFC library as a shared DLL. In other words, when you accept all the AppWizard defaults after Step 1.</P>
<P>Five classes have been created for you.For the application called <font color="#008000">FirstSDI,</font> they are as follows:</P>
<ul>
<li> <font color="#008000">CAboutDlg</font>, a dialog class for the About dialog box</P>
<li> <font color="#008000">CFirstSDIApp</font>, a CWinApp class for the entire application</P>
<li> <font color="#008000">CFirstSDIDoc</font>, a document class</P>
<li> <font color="#008000">CFirstSDIView</font>, a view class</P>
<li> <font color="#008000">CMainFrame</font>, a frame class</P>
</ul>
<P>Dialog classes are discussed in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, "Dialogs and Controls." Document, view, and frame classes are discussed in <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>, "Documents and Views."
The header file for CFirstSDIApp is shown in Listing 1.1.</P>
<P><I>Listing 1.1—FirstSDI.h—main header file for the FirstSDI </I><I>application</I></P>
<pre><font color="#008000">// FirstSDI.h : main header file for the FIRSTSDI application</font></pre>
<pre><font color="#008000">//</font></pre>
<pre><font color="#008000">#if !defined(AFX_FIRSTSDI_H__CDF38D8A_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)</font></pre>
<pre><font color="#008000">#define AFX_FIRSTSDI_H__CDF38D8A_8718_11D0_B02C_0080C81A3AA2__INCLUDED_</font></pre>
<pre><font color="#008000">#if _MSC_VER >= 1000</font></pre>
<pre><font color="#008000">#pragma once</font></pre>
<pre><font color="#008000">#endif // _MSC_VER >= 1000</font></pre>
<pre><font color="#008000">#ifndef __AFXWIN_H__</font></pre>
<pre><font color="#008000"> #error include 'stdafx.h' before including this file for PCH</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000">#include "resource.h" // main symbols</font></pre>
<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">// CFirstSDIApp:</font></pre>
<pre><font color="#008000">// See FirstSDI.cpp for the implementation of this class</font></pre>
<pre><font color="#008000">//</font></pre>
<pre><font color="#008000">class CFirstSDIApp : public CWinApp</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000"> CFirstSDIApp();</font></pre>
<pre><font color="#008000">// Overrides</font></pre>
<pre><font color="#008000"> // ClassWizard generated virtual function overrides</font></pre>
<pre><font color="#008000"> //{{AFX_VIRTUAL(CFirstSDIApp)</font></pre>
<pre><font color="#008000"> public:</font></pre>
<pre><font color="#008000"> virtual BOOL InitInstance();</font></pre>
<pre><font color="#008000"> //}}AFX_VIRTUAL</font></pre>
<pre><font color="#008000">// Implementation</font></pre>
<pre><font color="#008000"> //{{AFX_MSG(CFirstSDIApp)</font></pre>
<pre><font color="#008000"> afx_msg void OnAppAbout();</font></pre>
<pre><font color="#008000"> // NOTE - the ClassWizard will add and remove member functions here.</font></pre>
<pre><font color="#008000"> // DO NOT EDIT what you see in these blocks of generated code !</font></pre>
<pre><font color="#008000"> //}}AFX_MSG</font></pre>
<pre><font color="#008000"> DECLARE_MESSAGE_MAP()</font></pre>
<pre><font color="#008000">};</font></pre>
<pre><font color="#008000">/////////////////////////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">//{{AFX_INSERT_LOCATION}}</font></pre>
<pre><font color="#008000">// Microsoft Developer Studio will insert additional declarations immediately before the </font><font color="#008000">previous line.</font></pre>
<pre><font color="#008000">#endif // !defined(AFX_FIRSTSDI_H__CDF38D8A_8718_11D0_B02C_0080C81A3AA2__INCLUDED_)</font></pre>
<P>This code is confusing at the beginning. The <font color="#008000">#if(!defined)</font> followed by the very long string (yours will be different) is a clever form of <I>include guarding</I>. You may have seen a code snippet like this before:</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -