?? ch01.htm
字號:
</UL><P>After answering a few questions using AppWizard, you can compile and run the firstversion of your application in a few minutes.<H3><FONT COLOR="#000077"><B>Building Windows Applications with AppWizard</B></FONT></H3><P>In general, the following steps are used to build a program using AppWizard:<DL> <DD>1. Create a program skeleton using AppWizard.<BR> <BR> 2. Create any additional resources used by the program.<BR> <BR> 3. Add any additional classes and message-handling functions using ClassWizard.<BR> <BR> 4. Add the functionality required by your program. You actually have to write some code yourself for this part.<BR> <BR> 5. Compile and test your program, using the Visual C++ integrated debugger if needed.</DL><P>To start AppWizard and create your first Windows program, follow these steps:<DL> <DD>1. Select New from the File menu. The New dialog box is displayed.<BR> <BR> 2. Select the Projects tab. A list of project types will be displayed.<BR> <BR> 3. To create an MFC-based project, select MFC AppWizard(exe) as the project type.<BR> <BR> 4. Specify HelloMFC as the project name in the Project name box; a default location for your project will automatically be entered in the Location box (see Figure 1.6).</DL><P><A NAME="06"></A><A HREF="06.htm"><B>Figure 1.6.</B> </A><BR><I>The New Projects dialog box for the HelloMFC project.</I><DL> <DD>5. Make sure the Create New Workspace radio button is selected, and click OK to create the project.<BR> <BR> 6. The first MFC AppWizard screen asks for a project type, as shown in Figure 1.7. MFC AppWizard works similarly to the Developer Studio Setup Wizard, enabling you to move forward and backward using the Next and Back buttons. Select the radio button labeled Single Document and then click the Next button.</DL><P><A NAME="07"></A><A HREF="07.htm"><B>Figure 1.7.</B> </A><I><BR>The first AppWizard screen for HelloMFC.</I><DL> <DD>7. Move through all six MFC AppWizard screens. Each screen enables you to change a different option about the HelloMFC project. Although this example won't use any optional features, feel free to experiment with the options offered by MFC AppWizard.<BR> <BR> 8. The last MFC AppWizard screen presents a list of classes that is generated for the project. Click the button labeled Finish. MFC AppWizard displays a summary of the project, listing the classes and features you selected, as shown in Figure 1.8.</DL><P><A NAME="08"></A><A HREF="08.htm"><B>Figure 1.8.</B></A> <I><BR>The New Project Information dialog box for the Hello project.</I><DL> <DD>9. Click the OK button to start generating files required for the HelloMFC project.</DL><H3><FONT COLOR="#000077"><B>Exploring the HelloMFC AppWizard Project</B></FONT></H3><P>After you create the HelloMFC project using MFC AppWizard, the Project Workspacewindow opens. The Project Workspace window contains four tabs, each used to showa different view of the current project:<UL> <LI>The <I>ClassView</I> tab displays information about the C++ classes used in the HelloMFC project.<BR> <BR> <LI>The <I>ResourceView</I> tab displays information about the resources, such as bitmaps and dialog boxes, used in the HelloMFC project.<BR> <BR> <LI>The <I>FileView</I> tab displays information about the files used for the HelloMFC project.<BR> <BR> <LI>The final view is the <I>InfoView</I>, which is used for online help information.</UL><H3><FONT COLOR="#000077"><B>Handling Output Using MFC</B></FONT></H3><P>The HelloMFC project already contains a function that handles output. It's called<TT>OnDraw</TT>, and it can be found in the <TT>CHelloMFCView</TT> class. When yourproject is created by AppWizard, the <TT>OnDraw</TT> function really doesn't do muchuseful work--it's up to you to supply a version of this function that does somethingmeaningful.</P><P>To edit the <TT>CHelloMFCView</TT> class, follow these steps:<DL> <DD>1. Click the ClassView tab in the Project Workspace window. A list of the classes used in the HelloMFC application will be displayed. Note that all the class names begin with the letter C. This is a Microsoft naming convention--all of Microsoft's classes begin with C.<BR> <BR> 2. Expand the <TT>CHelloMFCView</TT> node of the tree control. A list of functions that are used in the <TT>CHelloMFCView</TT> class will be displayed.<BR> <BR> 3. Double-click the function named <TT>OnDraw</TT>. The editor will open to the <TT>OnDraw</TT> member function. Edit the <TT>CHelloMFCView::OnDraw</TT> function so that it looks like the function in Listing 1.2. You will need to remove a comment and two existing lines of code that were in the function already.</DL><H4><FONT COLOR="#000077">TYPE: Listing 1.2. The OnDraw function used for HelloMFC.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CHelloMFCView::OnDraw(CDC* pDC)</TT><TT>{</TT><TT> pDC->TextOut(50,50,"Hello MFC!", 10);</TT><TT>}</TT></FONT></PRE><P>Compile the HelloMFC project by selecting Build|Build HelloMFC.exe from the mainmenu (or press F7). The build window displays the progress of the build, which shouldlook something like the following:</P><PRE><FONT COLOR="#0066FF"><TT>Compiling resources...</TT><TT>Compiling...</TT><TT>StdAfx.cpp</TT><TT>Compiling...</TT><TT>HelloMFCDoc.cpp</TT><TT>HelloMFC.cpp</TT><TT>MainFrm.cpp</TT><TT>HelloMFCView.cpp</TT><TT>Generating Code...</TT><TT>Linking...</TT><TT>HelloMFC.exe - 0 error(s), 0 warning(s)</TT></FONT></PRE><P>Congratulations; you have created a simple Windows program! To execute the HelloMFCproject, select Execute from the Build menu or press F5 on the keyboard. The mostcommon way to launch a project from Developer Studio is to use the debugger. To startthe debugger, click the Go button on the toolbar or press F5 on the keyboard.</P><P>Figure 1.9 shows an example of the HelloMFC application running under Windows95.</P><P><A NAME="09"></A><A HREF="09.htm"><B>Figure 1.9.</B></A> <I><BR>The HelloMFC program.</I></P><P>One unusual aspect of the HelloMFC application is that the message is in a fixedlocation. If the window is resized, the text doesn't move. This is because the callto <TT>DrawText</TT> needs a fixed location for the message string in the first twoparameters:</P><PRE><FONT COLOR="#0066FF"><TT>pDC->TextOut(50,50,"Hello MFC!", 10);</TT></FONT></PRE><P>The third parameter is the actual message to be displayed, and the last parameteris the number of characters in the message.</P><P>In the next hour, you will learn how to display the message in the center of themain window.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this chapter, you were introduced to Developer Studio and Visual C++, as wellas the main tools and wizards included in Developer Studio and the MFC class library.</P><P>You also created two small programs using Visual C++: a console-mode applicationthat displayed "Hello World!" and a Windows application that was builtwith AppWizard.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q If I know C, how much effort is needed to learn C++?</B><BR> <BR> <B>A</B> C++ is very close to C in a number of ways. Almost every legal C program is also a legal C++ program. C++ introduces the idea of classes, which are discussed in Hour 3. A C++ compiler also has a different standard library than a C compiler. As you will see, Visual C++ makes it very easy to develop Windows programs using C++, even if you have no experience in C or C++.<BR> <BR> <B>Q Can I replace the Developer Studio editor with my own favorite editor?</B><BR> <BR> <B>A</B> No, but you can use your favorite editor to edit files, then use Developer Studio to build those files into a final executable. You will lose many of the integrated benefits of the integrated editor if you do this, however. You can change the Developer Studio editor to emulate Brief and Epsilon editors if you prefer their keyboard mappings.</DL><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL> <DD>1. What is a library?<BR> <BR> 2. How do you build a project using Developer Studio?<BR> <BR> 3. What is a wizard?<BR> <BR> 4. What are the three most commonly used wizards?<BR> <BR> 5. How do you invoke context-sensitive help inside the editor?<BR> <BR> 6. What are the four tab views inside the Project Workspace window?<BR> <BR> 7. What MFC function is used to display output?<BR> <BR> 8. What keyboard function is used to start the build process in Developer Studio?<BR> <BR> 9. What keyboard editor command is used for Undo?<BR> <BR> 10. What is the difference between Undo and Redo?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL> <DD>1. Change the Hello World console-mode program to display your name.<BR> <BR> 2. The first two parameters in the <TT>TextOut</TT> function call are the position coordinates for the text message. Experiment with the HelloMFC application, and change the position of the output message.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../fm/fm.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch02/ch02.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A><BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -