⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
</UL>

<P>After answering a few questions using AppWizard, you can compile and run the first
version 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 Workspace
window opens. The Project Workspace window contains four tabs, each used to show
a 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 your
project is created by AppWizard, the <TT>OnDraw</TT> function really doesn't do much
useful work--it's up to you to supply a version of this function that does something
meaningful.</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-&gt;TextOut(50,50,&quot;Hello MFC!&quot;, 10);</TT>
<TT>}</TT>
</FONT></PRE>
<P>Compile the HelloMFC project by selecting Build|Build HelloMFC.exe from the main
menu (or press F7). The build window displays the progress of the build, which should
look 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 HelloMFC
project, select Execute from the Build menu or press F5 on the keyboard. The most
common way to launch a project from Developer Studio is to use the debugger. To start
the 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 Windows
95.</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 fixed
location. If the window is resized, the text doesn't move. This is because the call
to <TT>DrawText</TT> needs a fixed location for the message string in the first two
parameters:</P>
<PRE><FONT COLOR="#0066FF"><TT>pDC-&gt;TextOut(50,50,&quot;Hello MFC!&quot;, 10);</TT>
</FONT></PRE>
<P>The third parameter is the actual message to be displayed, and the last parameter
is 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 the
main window.
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this chapter, you were introduced to Developer Studio and Visual C++, as well
as 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 application
that displayed &quot;Hello World!&quot; and a Windows application that was built
with AppWizard.
<H2><FONT COLOR="#000077"><B>Q&amp;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 what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<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"><IMG
SRC="../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>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -