📄 ch01.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 1</TITLE>
<LINK REL="Next" HREF="ch02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch02.htm">
<LINK REL="Previous" HREF="intro.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/intro.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Chapter 1</B></H2>
<H2><B>Building Your First Application</B></H2>
<hr>
<P>Visual C++ doesn't just compile code, it generates code. You can create a Windows application in minutes by telling AppWizard to make you a "starter app" with all the Windows boilerplate code you want. AppWizard is a very effective tool. It
copies code that almost all Windows applications need into your application. After all, you aren't the first programmer who has needed an application with resizable edges, minimize and maximize buttons, a File menu with Open, Close, Print Setup, Print, and
Exit options, are you?</P>
<ul>
<li><B>Using AppWizard to Make a Typical Application</B></P>
<P> When most people think "application," they expect an EXE file. AppWizard makes skeleton, executable Windows programs in less than a minute.</P>
<li><B>Other Applications AppWizard Can Make</B></P>
<P> Other application generating wizards can make DLLs, ActiveX controls, console applications, libraries, makefile, Internet Server extensions and filters, and more.</P>
<li><B>If You Change Your Mind</B></P>
<P> AppWizard can add a lot of functionality to an application that you are building for the first time. What if you want to add some of the functionality to an application that is already built? This section shows you how.</P>
<li><B>AppWizard's Code</B></P>
<P> See the code generated for you by AppWizard and understand the different application types.</P>
<li><B>Other AppWizard Decisions</B></P>
<P> This section directs you to the places in this book where the consequences of AppWizard decisions such as database support, ActiveX technology, and application type are explained in detail.</P>
</ul>
<H3><B>Creating a Windows Application</B></H3>
<P>AppWizard can make many different kinds of applications, but what most people want, at least at first, is an executable (.exe) program. Most people also want AppWizard to produce boilerplate code—the classes, objects, and functions that have to be
in every program. To create a program like this, Choose <U>F</U>ile, <U>N</U>ew and click the Projects tab in the New dialog box, as shown in Figure 1.1.</P>
<A HREF="Bfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs01.gif"><b>Fig. 1.1</b></A>
<P><I>The Projects tab of the New dialog box is where you choose the kind of application you want to build.</I></P>
<P>Choose MFC AppWizard (EXE) from the list box on the left, fill in a project name, and click OK. AppWizard then works through a number of steps. At each step, you make a decision about what kind of application you want, and click Next. At any time, you
can click Back to return to a previous decision, Cancel to abandon the whole process, Help for more details, or Finish to skip to the end and create the application without answering any more questions (not recommended before the last step). Each step is
covered in the following sections.</P>
<P><B>Deciding How Many Documents the Application Supports</B></P>
<P>The first decision to communicate to AppWizard, as shown in Figure 1.2, is whether your application should be MDI, SDI, or dialog-based. App Wizard generates different code and classes for each of these application types.</P>
<A HREF="Bfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs02.gif"><b>Fig. 1.2</b></A>
<P><I>The first step in building a typical application with App Wizard is choosing the interface.</I></P>
<P>The three application types to choose from are as follows:</P>
<ul>
<li> A Single Document Interface (SDI) application, such as Notepad, has only one document open at a time. When you choose <U>F</U>ile, <U>O</U>pen, the currently open file is closed before the new one is opened.</P>
<li> A Multiple Document Interface (MDI) application, such as Excel or Word, can open many documents (typically files) at once. There is a <U>W</U>indow menu and a <U>C</U>lose item on the <U>F</U>ile menu. It's a quirk of MFC that if you like multiple
views on a single document, you must build an MDI application.</P>
<li> A dialog-based application, such as the Character Map utility that comes with Windows, does not have a document at all. There are no menus. (If you'd like to see Character Map in action, it's usually in the Accessories folder reached by clicking
Start. You may need to install it using Add/Remove programs under Control Panel.)</P>
</ul>
<A HREF="Bfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs03.gif"><b>Fig. 1.3</b></A>
<P><I>Character Map is a dialog-based application.</I></P>
<P>As you change the radio button selection, the picture on the left of the screen changes to remind you what the application looks like if you choose this type of application.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>Dialog-based applications are quite different from MDI or SDI applications. The AppWizard dialogs are different when creating a dialog-based application, and are presented in the next section entitled “Creating a Dialog-Based
Application.”</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Lower on the screen is a drop-down box to select the language for your resources. If you have set your system language to anything other than the default US English, make sure you set your resources to that language too. If you don't, you will
encounter unexpected behavior from ClassWizard later. (Of course, if your application is for users who will have their language set to US English, you might not have a choice. In that case, change your system language under Control Panel.) Click Next after
you make your choices.</P>
<P><B>Databases</B></P>
<P>The second step in creating an executable Windows program with AppWizard is to choose the level of database support, as shown in Figure 1.4.</P>
<A HREF="Bfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs04.gif"><b>Fig. 1.4</b></A>
<P><I>The second step to building a typical application with AppWizard is to set the database options you will use.</I></P>
<P>There are four choices for database support, as follows:</P>
<ul>
<li> If you are not writing a database application, choose None.</P>
<li> If you want to have access to a database, but don't want to derive your view from CFormView or have a Record menu, choose Header Files Only.</P>
<li> If you want to derive your view from CFormView and have a Record menu, but do not need to serialize a document, choose Database View Without File Support. You can update database records with Crecordset, an MFC class discussed in more detail in <A
HREF="index22.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index22.htm" target="text">Chapter 22</A>, "Database Access."</P>
<li> If you want to support databases as in the previous option, but also need to serialize a document (perhaps some user options), choose Database View With File Support.</P>
</ul>
<P><A HREF="index22.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index22.htm" target="text">Chapter 22</A>, "Database Access," clarifies these choices and demonstrates database programming with MFC. If you choose to have a database view, you must specify a data source now. Click the Data Source
button to set this up.</P>
<P>As you select different radio buttons, the picture on the left changes to show you the consequences of your choice. Click Next to move to the next step.</P>
<P><B>Compound Document Support</B></P>
<P>The third step in running AppWizard to create an executable Windows program is to decide on the amount of compound document support you want to include, as shown in Figure 1.5. OLE (Object Linking and Embedding) has been officially renamed ActiveX to
clarify the recent technology shifts, most of which are hidden from you by MFC. ActiveX and OLE technology are jointly referred to as <I>compound document technology</I>. <A HREF="index13.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index13.htm" target="text">Chapter 13</A>, "ActiveX Concepts,"
covers this technology in detail.</P>
<A HREF="Bfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs05.gif"><b>Fig. 1.5</b></A>
<P><I>The third step of building a typical application with AppWizard is to set the compound document support </I><I>you will need.</I></P>
<P>There are five choices for compound document support, as follows:</P>
<ul>
<li> If you are not writing an ActiveX application, choose None.</P>
<li> If you want your application to be able to contain embedded or linked ActiveX objects, such as Word documents or Excel worksheets, choose Container. You learn to build an ActiveX container in <A HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>,
"Building an ActiveX Container Application."</P>
<li> If you want your application to serve documents that can be embedded in other applications but that do not need to run as a stand-alone application, choose Mini Server.</P>
<li> If your application serves documents and also functions as a stand-alone application, choose Full Server. In <A HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>, "Building an ActiveX Container Application," you learn to build an ActiveX full
server.</P>
<li> If you want your application to have the capability to contain objects from other applications and also to serve its objects to other applications, choose Both Container and Server.</P>
</ul>
<P>If you choose to support compound documents, you can also support <I>compound files</I>. Compound files contain one or more ActiveX objects and are saved in a special way, so that one of the objects can be changed without rewriting the whole file. This
can save a great deal of time. Use the radio buttons in the middle of this Step 3 dialog box to say Yes, Please or No, or thank you to compound files.</P>
<P>If you want your application to surrender control to other applications through Automation, check the Automation checkbox. (Automation is the subject of <A HREF="index16.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index16.htm" target="text">Chapter 16</A>, "Building an Automation Server.") If
you want your application to use ActiveX controls, select the ActiveX Controls checkbox. Click Next to move to the next step.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>If you want your application to <I>be</I> an ActiveX control, you do not create a typical .exe application as described in this section. Creating ActiveX controls with the ActiveX ControlWizard is covered in <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."</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><B>Appearance and Other Options</B></P>
<P>The fourth step in running AppWizard to create an executable Windows program (see Figure 1.6) is to determine some of the interface appearance options for your application. This Step 4 dialog box contains a number of independent checkboxes; check them
if you want a feature, leave them unchecked if you do not.</P>
<A HREF="Bfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch01/Bfigs06.gif"><b>Fig. 1.6</b></A>
<P><I>The fourth step of building a typical application with AppWizard is to set some interface options.</I></P>
<P>The following are the options that affect your interface’s appearance:</P>
<ul>
<li> <I>Docking toolbar</I>—AppWizard sets up a toolbar for you. You can edit it to remove unwanted buttons, or add new ones linked to your own menu items. This is described in <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm" target="text">Chapter 10</A>, "Status Bars and
Toolbars."</P>
<li> <I>Initial status bar</I>—AppWizard creates a status bar to display menu prompts and other messages. Later, you can write code to add indicators and other elements to this bar, as described in <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm" target="text">Chapter 10</A>,
"Status Bars and Toolbars."</P>
<li> <I>Printing and print preview</I>—Your application will have <U>P</U>rint and Print Pre<U>v</U>iew options on the <U>F</U>ile menu, and much of the code you need to implement printing will be generated by AppWizard. <A HREF="index07.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index07.htm"
target="text">Chapter 7</A>, "Printing and Print Preview," discusses the rest.</P>
<li> <I>Context sensitive help</I>—Your <U>H</U>elp menu will gain <U>I</U>ndex and <U>U</U>sing Help options, and some of the code needed to implement Help will be provided by AppWizard. This decision is hard to change later since quite a lot of
code is added in different places when implementing context-sensitive Help. <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, "Help," describes Help implementation.</P>
<li> <I>3D controls</I>—Your application will look like a typical Windows 95 application. If you do not select this option, your dialog boxes have a white background and there are no shadows around the edges of edit boxes, checkboxes, and other
controls.</P>
<li> <I>MAPI</I>—Your application will be able to use the Messaging API to send fax, e-mail, or other messages. <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," discusses the Messaging API.</P>
<li> <I>Sockets</I>—Your application can access the Internet directly, using protocols like FTP and HTTP (the World Wide Web protocol). <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," discusses
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -