?? ch03.htm
字號(hào):
<P>The simple Windows program provided in Listing 3.3 might seem large, but it'sactually about half the size of a similar program written in C. Using the MFC classlibrary enables you to use a large amount of source code that has already been writtenfor you. There is a lot of strange-looking code in Listing 3.3, so don't try to understandit all right now.<H3><FONT COLOR="#000077"><B>Building the HelloWin Example</B></FONT></H3><P>To build the program, create an MFC Windows project named HelloWin. Begin by selectingFile | New from the Visual C++ main menu; select the Projects tab in the New dialogbox. Next, select Win32 Application as the project type. You must also specify aname and location for your project, just as you did for the projects in the firsttwo hours.</P><P>After the project has been created, open a new C++ source file document and enterthe contents of Listing 3.3 exactly as they are shown. Save the file as <TT>HelloWin.cpp</TT>and add it to the project. (If necessary, refer to Hour 1, "Introducing VisualC++ 5," for specific instructions.)</P><P>Set the linking options for the project by selecting Project | Settings from themain menu. On the tab marked General is an item labeled Microsoft Foundation Classes.It will have the value <TT>Not Using MFC</TT>. Change the selection to <TT>Use MFCin a Shared Dll</TT>. You can do this by clicking on the down arrow beside the NotUsing MFC selection. This opens a box where you can then make the appropriate selection.</P><P>Compile the HelloWin project by selecting Build | Build HelloWin.exe from themain menu (or Press F7).</P><P>To start the HelloWin program, select Build | Start Debug | Go from the main menu(or Press F5). Figure 3.3 shows an example of HelloWin running.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 3.3.</B></A> <I><BR>The HelloWin program displaying its message in a window.</I><H2><FONT COLOR="#000077"><B>The Common Elements of a Windows Program</B></FONT></H2><P>Two elements are found in almost every Windows program; each of these elementscan be found in the HelloWin program that you just compiled and ran:<UL> <LI>Windows are used for visible parts of an application<BR> <BR> <LI>Messages are used to control the interaction between an application and the Windows operating system</UL><H3><FONT COLOR="#000077"><B>Windows Are Everywhere</B></FONT></H3><P>One of the fundamental concepts in Windows programming is that everything yousee is a window. Some examples of windows are<UL> <LI>Controls such as pushbuttons, list boxes, and text edit controls<BR> <BR> <LI>Dialog boxes and property pages<BR> <BR> <LI>Toolbars and menu bars<BR> <BR> <LI>The Windows 95 taskbar<BR> <BR> <LI>The DOS command box that is used for console-mode applications</UL><P>All windows have a common set of operations that can be applied to them. Theyare all re-sized, moved, enabled, disabled, hidden, and displayed in the same way.<H3><FONT COLOR="#000077"><B>The Client and Non-Client Areas</B></FONT></H3><P>A window is divided into two main areas, as shown in Figure 3.4:<UL> <LI>The non-client area, which contains the border, menus, and caption area for the window<BR> <BR> <LI>The client area, which is the area that is left over, also known as the "main" part of the window</UL><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 3.4.</B></A> <I><BR>Client and non-client areas of a window.</I></P><P>The non-client area of a window is normally maintained by Windows; your applicationswill normally be concerned only with the client area.<H3><FONT COLOR="#000077"><B>Messages and Functions</B></FONT></H3><P>When Windows needs to communicate with an application, it sends it a message.A message is similar to a function call--in fact, the MFC library will route mostmessages as function calls into your application. For example, in an AppWizard application,the MFC library calls the <TT>OnDraw</TT> function whenever Windows sends a <TT>WM_PAINT</TT>message.</P><P>When your application communicates with a window, it will usually send it a message.To enable or disable a control, you must send the control a <TT>WM_ENABLE</TT> message.When using C, this process is very tedious and error prone. MFC simplifies thingsby providing functions that you can call and then handling the message sending foryou.<H2><FONT COLOR="#000077"><B>What Are Statements and Expressions?</B></FONT></H2><P>Statements and expressions are the elements defined by the C++ language that areconverted into machine code by the compiler to build your C++ programs. Seems likea textbook-type definition, doesn't it? In reality, though, it is very hard to defineexactly what they are. When talking about a building, we can say that it is madeof bricks, boards, and other things; we can define the brick or board very easily.In the case of the C++ programming language, it is much more difficult. Here we aredealing with abstract concepts. The difference between a statement and expressionis very subtle, as you will soon see. Although it appears to be confusing at first,the language will become understandable with practice. Eventually the C++ languagewill become as natural to you as your native language.</P><P>Just like the simple Hello programs, all C++ programs are made up of statementsand expressions. Expressions and statements range from the simple statements thatwere shown in the Hello programs to very complex expressions that stretch acrossseveral lines.<H3><FONT COLOR="#000077"><B>Statements</B></FONT></H3><P>All statements end with semicolons. In fact, the simplest statement is calledthe null statement, and it consists of only a single semicolon, as follows:</P><PRE><FONT COLOR="#0066FF"><TT>;</TT></FONT></PRE><P>The null statement isn't used often; it's used only in situations in which theC++ syntax requires a statement, but no real work needs to be done.</P><P>You use a statement to tell the compiler to perform some type of specific action.For example, you know from the console mode programs you created that the followingstatement will cause the characters <TT>Hello World!</TT> to be displayed on yourscreen:</P><PRE><FONT COLOR="#0066FF"><TT>cout << "Hello World!" << endl;</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Declarations</B></FONT></H3><P>A declaration is another type of statement. As discussed earlier, declarationsintroduce a variable to the compiler. The following line is an example of a simpledeclaration:</P><PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT></FONT></PRE><P>This tells the compiler that <TT>myAge</TT> is an integer.<H3><FONT COLOR="#000077"><B>Assignment</B></FONT></H3><P>An assignment expression is used to assign a value to a variable, using the assignmentoperator, <TT>=</TT>, as follows:</P><PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT><TT>myAge = 135;</TT></FONT></PRE><P>Every expression has a value. The value of an assignment expression is the valueof the assignment. This means that the following statement assigns the value <TT>42</TT>to the variables <TT>yourAge</TT> and <TT>myAge</TT>:</P><PRE><FONT COLOR="#0066FF"><TT>myAge = yourAge = 42;</TT></FONT></PRE><P>The program in Listing 3.4 demonstrates how to assign a value to a variable.<H4><FONT COLOR="#000077">TYPE: Listing 3.4. A C++ program that assigns a value toa variable.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>#include <iostream></TT><TT>using namespace std;</TT><TT>int main()</TT><TT>{</TT><TT> int myAge;</TT><TT> myAge = 42;</TT><TT> cout << "Hello" << endl;</TT><TT> cout << "My age is " << myAge << endl;</TT><TT> return 0;</TT><TT>}</TT></FONT></PRE><P>The assignment operator is just one example of the operators available in C++.More operators are discussed in the next section.<H3><FONT COLOR="#000077"><B>Other Common Expressions and Operators</B></FONT></H3><P>The C++ language contains operators that you can use to write addition, subtraction,multiplication, and other expressions. Some common math operators are shown in Table3.1.<H4><FONT COLOR="#000077">Table 3.1. Some common math operators used in C++.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Operator</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>+</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Addition</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>-</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Subtraction</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>/</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Division</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>*</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Multiplication</TD> </TR></TABLE></P><P>All math operators group from left to right. The multiplication and division operatorshave a higher precedence than the addition and subtraction operators. This meansthat the following expressions are equivalent: a + 5 * 3 a + 15 You can use parenthesesto force an expression to be evaluated in a preferred order. Note the grouping ofthe following expression: (a + 5) * 3 This expression adds 5 to the value storedin a and then multiplies that value by 3. The math operators can also be combinedwith an assignment operator, as follows:</P><PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT><TT> myAge = 40 + 2;</TT></FONT></PRE><P>The expression <TT>40 + 2</TT> has a value of <TT>42</TT>. After that value iscalculated, the value of the expression is stored in the <TT>myAge</TT> variable.<H2><FONT COLOR="#000077"><B>Rectangles and Regions</B></FONT></H2><P>The rectangle is a fundamental component of most Windows programs. Because mostwindows and controls are rectangular, it isn't surprising that one of the most commonlyused data structures in Windows programming is used to represent a rectangle.</P><P>Rectangles are often used to represent the position or size of all types of windows:main windows as well as controls, toolbars and dialog boxes. There are two basictypes of rectangle coordinates:<UL> <LI>Screen rectangle coordinates, which place a rectangle in relationship to the entire screen <LI>Client rectangle coordinates, which always have their top and left values set to zero and provide the size of a rectangle that represents a window's client area</UL><P>Screen rectangle coordinates are often used when moving a window in relation tothe entire screen. Client rectangles are most commonly used when positioning controlsor drawing inside a control or other window.</P><P>When requesting the dimensions of a rectangle, you must pass a <TT>CRect</TT>variable to one of the Windows rectangle functions. The following two lines of codedeclare an instance of <TT>CRect</TT> as a variable and pass it to the <TT>GetClientRect</TT>function:</P><PRE><FONT COLOR="#0066FF"><TT>CRect rcClient;</TT><TT>GetClientRect(rcClient);</TT></FONT></PRE><P>The next example uses a client area rectangle to display a message to the user,just like the HelloMFC program in the first hour. The new example will draw the messagein the center of the client area; if the window is resized, the message will be redrawnin the center of the new rectangle.</P><P>Create an MFC AppWizard application named HelloRect, following the steps presentedin the first hour. Modify the <TT>OnDraw</TT> function found in the <TT>CHelloRectView</TT>class so that it looks like the function shown in Listing 3.5.<H4><FONT COLOR="#000077">TYPE: Listing 3.5. Using a rectangle to center a messagein a window.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CHelloRectView::OnDraw(CDC* pDC)</TT><TT>{</TT><TT> CRect rcClient;</TT><TT> GetClientRect(rcClient);</TT><TT> pDC->DrawText("Hello Client Rectangle!", -1, rcClient,</TT><TT> DT_SINGLELINE | DT_CENTER | DT_VCENTER );</TT><TT>}</TT></FONT></PRE><P>Build the HelloRect application, and run it from Developer Studio. Note that ifyou resize the window, the message is redrawn so that it remains in the center ofthe client area.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you have learned about some of the more advanced building blocksthat make up C++ programs: functions, structures, and classes. You also looked atsome basic information about the MFC class library and built an MFC application withoutusing ClassWizard.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q What is the difference between a rectangle that uses screen coordinates and a rectangle that uses client coordinates?</B><BR> <BR> <B>A</B> Every window in a Windows application can be represented by a rectangle; this rectangle will typically use either screen or client coordinates. The rectangle that results from these coordinates is always the same, the difference is only in the point of reference that is used to measure the rectangle.<BR> <BR> <B>Q Can a structure have member functions?</B><BR> <BR> <B>A</B> Absolutely. A class and a structure are exactly the same, except that all members of a structure are accessible by default, while class members are private (not accessible) by default. You will learn more about access restrictions in the next hour.<BR> <BR> <B>Q Why is no function prototype required for <TT>main()</TT>?</B><BR> <BR> <B>A</B> The short answer: because the C++ standard says you don't need one. The purpose of function prototypes is to introduce new functions to the compiler; because every C++ program is required to have a main function, no function is necessary.</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 are some examples of the different types of windows found in a Windows application?<BR> <BR> 2. What is a function?<BR> <BR> 3. What are the four parts of a function definition?<BR> <BR> 4. How are classes different from structures?<BR> <BR> 5. What function is called when an instance of a class is created?<BR> <BR> 6. What function is called when an instance of a class is destroyed?<BR> <BR> 7. What is the difference between the client and non-client areas?<BR> <BR> 8. What is the value of the expression <TT>a = 42</TT>?<BR> <BR> 9. What symbol is used for multiplication?<BR> <BR> 10. What symbol is used for division?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL> <DD>1. Write a console-mode program that asks for a distance in miles and converts the distance into feet. There are 5,280 feet in a mile.<BR> <BR> 2. Modify the HelloWin program to display different messages in different parts of the main window.</DL><CENTER><P><HR><A HREF="../ch02/ch02.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch04/ch04.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>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -