📄 apf.htm
字号:
<P>These lines create three new CPlanetDets type objects and add them to the array.The last line displays the diameter in the TRACE macro without needing to cast thereturned value from myPlanetArray[i] because it's already a pointer of the CPlanetDets*type.</P><P>However, later you might forget the exact nature of myPlanetArray and try to adda CStatic object instead:</P><P><PRE>myPlanetArray.Add(new CStatic());</PRE><P>Fortunately, the compiler will spot the transgression and issue a compiler errorsuch as</P><P><PRE>`Add' : cannot convert parameter 1 from `class ÂCStatic *' to `class CPlanetDets *</PRE><P>However, the error wouldn't have been spotted if you had been using a CObArrayto hold the planet details:</P><P><PRE>CObArray myPlanetArray;</PRE><P>The CStatic object would be happily stored along with the CPlanetDets objects,causing untold havoc when you try to retrieve the CStatic object, thinking it's aCPlanetDets object.</P><P>The template used to generate type-safe lists is CList; it takes the same generalform as CArray:</P><P><PRE>CList<CMyCustomClass*, CMyCustomClass *> myCustomClassList;</PRE><P>Again, the first parameter is the required returned object type, and the secondparameter specifies the accepted object types for functions that accept elementsfor storage.</P><P>All the functions available for lists are available for your own specific type-safecustomized lists, again checking and returning your specified types. Therefore, theequivalent list-based code for the planet storing array would be coded like this:</P><P><PRE>CList<CPlanetDets*,CPlanetDets*> myPlanetList;myPlanetList.AddTail(new CPlanetDets Â(4878, 0.054, 57.91, 87.969));myPlanetList.AddTail(new CPlanetDets Â(12100, 0.815, 108.21, 224.701));myPlanetList.AddTail(new CPlanetDets Â(12756, 1.000, 149.60, 365.256));POSITION pos = myPlanetList.GetHeadPosition();while(pos) TRACE("Diameter = %f\n",myPlanetList.</PRE><H3><A NAME="Heading5"></A>ÂGetNext(pos)->m_dDiameter);</H3><P>The template for customized maps differs from the list and arrays in that it needsfour parameters: an input and a return value for both the key and element value.So the general form is like this:</P><P><PRE>CMap<MyType, MyArgType, CMyCustomClass *, CMyCustomClassArg *>myCustomClassMap;</PRE><P>The first parameter, MyType, specifies the internally stored key value for eachmap association. This can be any of the basic types such as int, WORD, DWORD, double,float, or CString, or it can be a pointer to your own specific type.</P><P>The second parameter, MyArgType, specifies the argument type used to pass keyvalues in and out of the map functions.</P><P>The third parameter, CMyCustomClass *, is how you want the internal element valuesto be stored (as specific type-safe pointers to your objects).</P><P>The fourth parameter, CMyCustomClassArg *, specifies the argument type used topass your element values in and out of the map functions. For example, to associatethe planet details with their names, you might code the following:</P><P><PRE>CMap<CString,LPCSTR,CPlanetDets*,CPlanetDets*> myPlanetMap;myPlanetMap.SetAt("Mercury", new CPlanetDets(4878, 0.054, 57.91, 87.969));myPlanetMap.SetAt("Venus", new CPlanetDets(12100, 0.815, 108.21, 224.701));myPlanetMap.SetAt("Earth", new CPlanetDets(12756, 1.000, 149.60, 365.256));CPlanetDets* pPlanet = NULL;if (myPlanetMap.Lookup("Venus",pPlanet))TRACE("Diameter = %f\n",pPlanet->m_dDiameter);</PRE><P>The map declaration indicates that the objects should be stored internally asCStrings but use LPCSTR (pointers to constant character arrays) to pass values intoand out of the map. The planet's details themselves will be both stored internallyand accessed as pointers to CPlanetDets objects (such as CPlanetDets*).</P><BLOCKQUOTE> <P><HR><B>POTENTIAL PROBLEMS WHEN USING THE MAP'S INTERNAL HASH KEY TYPES</B></P> <P>You must be wary of the conversion between the passed parameters and the internal hash key storage system. For example, if you were to replace the CString in the previous example with another LPCSTR for the internal storage object, the Lookup() would fail to find "Venus" because it would be comparing the pointer values (to different instances of "Venus") rather than the contents of the strings. <HR></BLOCKQUOTE><H2><A NAME="Heading6"></A>Using the Coordinate-Handling Classes</H2><P>Because Windows is a graphically oriented environment, you'll often need to holdpoint positions, rectangles, and sizes. Three MFC classes help store and manipulatethese coordinates: CPoint, CRect, and CSize. Each has several member functions andoperator overloads that take much of the work out of adding, constructing, and findingderivatives of these coordinates.</P><P>Also several of the MFC and GDI functions understand their types or underlyingtypes as parameter values, so you don't have to perform any messy mangling operationsto pass them into functions.</P><P><H3><A NAME="Heading7"></A>Using the CPoint Class</H3><P>CPoint encapsulates a POINT structure that just holds an x and y position to representa point on a two-dimensional surface. You can always access x and y members directlyto get or set their current values like this:</P><P><PRE>CPoint ptOne;ptOne.x = 5;ptOne.y = 20;TRACE("Co-ordinate = (%d,%d)\n",ptOne.x,ptOne.y);</PRE><P>You set these values when you construct a CPoint object by passing values to oneof CPoint's several constructors, as shown in Table F.4.</P><P><H4>TABLE F.4. CONSTRUCTOR TYPES FOR THE CPoint CLASS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Constructor Definition</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPoint()</TD> <TD ALIGN="LEFT">Constructs an uninitialized object</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPoint(POINT ptInit)</TD> <TD ALIGN="LEFT">Copies the settings from a POINT structure or another CPoint object</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPoint(int x, int y)</TD> <TD ALIGN="LEFT">Initializes the object from the x and y parameter values</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPoint(DWORD dwInit)</TD> <TD ALIGN="LEFT">Uses the low 16 bits for the x value and the high 16 bits for the y value</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPoint(SIZE sizeInit)</TD> <TD ALIGN="LEFT">Copies the settings from a SIZE structure or CSize object</TD> </TR></TABLE></P><P>For example, you could replace the last sample lines with these for the same result:</P><P><PRE>CPoint ptOne(5,20);TRACE("Co-ordinate = (%d,%d)\n",ptOne.x,ptOne.y);</PRE><P>One of the most useful aspects of the CPoint class is its many operator overloads.By using the +, -, +=, and -= operators with other CPoint, CRect, or CSize objects,you can add or subtract coordinate pairs from other coordinate pairs or from rectanglesor sizes. For example, the long way to subtract two points from each other to givea third would be like this:</P><P><PRE>CPoint ptOne(5,20);CPoint ptTwo(25,40);CPoint ptThree;ptThree.x = ptTwo.x - ptOne.x;ptThree.y = ptTwo.y - ptOne.y;</PRE><P>This can be simplified by using the operator overload:</P><P><PRE>CPoint ptOne(5,20);CPoint ptTwo(25,40);CPoint ptThree = ptTwo - ptOne;</PRE><P>Or you can add the coordinates of one point to another like this:</P><P><PRE>ptTwo += ptOne;</PRE><P>You can also use the == and != logical operator overloads to perform comparisons.For example, to check whether ptTwo is equal to ptOne in both x and y values, youcan code the following:</P><P><PRE>if (ptOne == ptTwo) TRACE("Points are the same");</PRE><P>There is also an Offset() function that adds an offset value specified by passingx and y values, or a CPoint class or POINT structure, or a CSize or SIZE structure.Therefore, the following two lines are functionally identical:</P><P><PRE>ptOne.Offset(75,-15);ptOne-=CPoint(-75,15);</PRE><H3><A NAME="Heading8"></A>Using the CRect Class</H3><P>The CRect class encapsulates a RECT structure to hold two pairs of coordinatesthat describe a rectangle by its top-left point and its bottom-right point. You canconstruct a CRect object using one of its several constructors, as shown in TableF.5.</P><P><H4>TABLE F.5. CONSTRUCTOR TYPES FOR THE CRect CLASS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Constructor Definition</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect()</TD> <TD ALIGN="LEFT">Constructs an uninitialized object</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect(const RECT& rcInit)</TD> <TD ALIGN="LEFT">Copies the settings from another RECT structure or CRect object</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect(LPCRECT lprcInit)</TD> <TD ALIGN="LEFT">Copies the settings via a RECT or CRect pointer</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect(int l,int t,int r,int b)</TD> <TD ALIGN="LEFT">Initializes the coordinates from left, top, right, and bottom parameters</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect(POINT point, SIZE size)</TD> <TD ALIGN="LEFT">Initializes from a POINT or CPoint and a SIZE or CSize</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CRect(POINT ptTL, POINT ptBR)</TD> <TD ALIGN="LEFT">Initializes from a top-left POINT and a bottom-right POINT</TD> </TR></TABLE></P><P>After you've constructed a CRect object, you can access each of the top, left,bottom, and right members individually using the (LPRECT) cast to cast it into aRECT structure as shown in these lines:</P><P><PRE>CRect rcOne(15,15,25,20); ((LPRECT)rcOne)->bottom += 20;TRACE("Rect is (%d,%d)-(%d,%d)",((LPRECT)rcOne)->left,((LPRECT)rcOne)->top, ((LPRECT)rcOne)->right,((LPRECT)rcOne)->bottom);</PRE><P>Alternatively, you can access the members via either the top-left CPoint or thebottom-right CPoint. References to these member objects are returned by the TopLeft()and BottomRight() functions. When you've accessed either the top-left or bottom-rightpoints, you can then manipulate them using any of the CPoint functions shown in theprevious section. For example, the following lines are functionally identical tothe previous lines, but differ in that they construct and access the rectangle usingCPoint objects:</P><P><PRE>CRect rcOne(CPoint(15,15),CPoint(25,20));rcOne.BottomRight().y += 20;TRACE("Rect is (%d,%d)-(%d,%d)", rcOne.TopLeft().x,rcOne.TopLeft().y, rcOne.BottomRight().x,rcOne.BottomRight().y);</PRE><P>You can also use the SetRect() function to set the coordinates by passing fourintegers to represent the top-left x- and y-coordinates and the bottom-right x- andy-coordinates. SetRectEmpty() sets all these coordinates to zero to make a NULL rectangle.The IsRectNull() function will return TRUE if called on such a NULL rectangle, andIsRectEmpty() will return TRUE if the width and height are both zero (even if theindividual values are not zero).</P><P>Several helper functions help you calculate various aspects of the rectangle'sgeometry. The width and height can be found by calling the Width()and Height() functions,each of which returns the relevant integer value. Alternatively, you can find a CSizethat represents both width and height by calling the Size() function. For example,the following line displays the width and height of the rectangle rcOne:</P><P><PRE>TRACE("Rect Width = %d, Height = %d\n", rcOne.Width(), rcOne.Height());</PRE><P>The point in the center of the rectangle is often a useful coordinate to know;you can find this by calling the CenterPoint() function, which returns a CPoint objectto represent the center of the rectangle.</P><P>You might use this to find the center of your window's client area and draw adot there like this:</P><P><PRE>CRect rcClient;GetClientRect(&rcClient);dc.SetPixel(rcClient.CenterPoint(),0);</PRE><P>You can also find the union or intersection of two rectangles by calling UnionRect()and InterSectRect(), which both take two source rectangles as parameters and setthe coordinates of the calling CRect object to the union or intersection. The unionis the smallest rectangle that will enclose the two source rectangles. The intersectionis the largest rectangle that is enclosed by both source rectangles. The diagramin Figure F.1 shows the union and intersection of two source rectangles labeled Aand B.</P><P><A HREF="javascript:popUp('33fig01.gif')"><B>FIGURE F.1.</B></A><B> </B><I>Theunion and intersection between two rectangles.</I></P><P>The following lines calculate the intersection and union of the source rectanglesrcOne and rcTwo:</P><P><PRE>CRect rcOne(10,10,100,100);CRect rcTwo(50,50,150,200);CRect rcUnion, rcIntersect;rcUnion.UnionRect(rcOne,rcTwo);rcIntersect.IntersectRect(rcOne,rcTwo);</PRE><P>When this code is run, rcUnion will be set to coordinates (10,10)-(150,200) andrcIntersect will be set to coordinates (50,50)-(100,100).</P><P>You can use SubtractRect() to find the subtraction of one rectangle from another.This is the smallest rectangle that contains all the points not intersected by thetwo source rectangles (or the smallest non-overlapping section). For example, byadding the following lines to an OnPaint() handler, you can see the effects of SubtractRect()to subtract rcTwo from rcOne to produce rcDst. The resulting subtraction is the sectionthat will be drawn in blue at the bottom of the diagram, as shown in Figure F.2.</P><P><PRE>CRect rcOne(10,10,220,220), rcTwo(50,50,150,260), rcDst;rcDst.SubtractRect(rcTwo,rcOne);dc.FillSolidRect(rcOne,RGB(255,0,0));//reddc.FillSolidRect(rcTwo,RGB(0,255,0));//greendc.FillSolidRect(rcDst,RGB(0,0,255));//blue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -