Use of CRectTracker class: two eggs CRectTracker (commonly known as the "rubber band" category) is a very interesting class. You often see this situation in Windows: it can be used to display the boundary, you can also use it to zoom in to zoom in and out. How to implement this function by programming? This is the role of the CRECTRACKER class; (box selection) (display boundary and scaled) example (source program) Open the project file above, compile and run. You will see several functions of CRectTracker; let us make a new project file from the head to slowly grasp it. Create a single document project file, name it RECT. Click Finish to complete the establishment of the project; first build the OBJ file for the first time, we continue to explain under the process of it; The reason why it is set to be public because it is called in the View. Then we initialize it, in CRectDoc :: CrectDoc constructor: CRectDoc :: CRectDoc () {// TODO: add one-time construction code here m_rectTracker.m_rect.SetRect (0,0,100,100); m_rectTracker.m_nStyle = CRectTracker: : resizeinside | CRECTTRACKER :: DottedLine;} where: m_rect is a data member used to control the size of the quadrilateral position in CRECTRACKER. SETRECT uses View's coordinates; M_NStyle is the type of CRECTTRACKER, where: CRECTRACKER :: ResizeInde and CRectTracker :: ResizeOutside is an inside of M_RECT or an external painted area (they are mutually different), CRECTTRAKCER :: DottedLine is a quadrilateral boundary with a dotted line. Other values include: CRECTRACKER :: SolidLine: It is used to draw a solid line boundary; (and dottedLine is mutual) CRECTTRACKER :: Hatchedborder: Boundary belt Boil; CRECTTRACKER :: Hatchinside: Internal belt Boil; you can Operating the previous example, the above parameters are used.
You can also use them one by one in the second step to deepen the understanding of their respective meaning; step 2: I first draw a blue ellipse in the view; continue our work in CRECTVIEW: Void CRectView :: OnDraw (CDC * PDC) {CRECTDOC * PDOC = getDocument (); assert_valid (pdoc); // Todo: add draw code for native data here cbrush brush (RGB (0,0,255)); // Generate blue brush CBRUSH * OLDBRUSH = PDC-> SelectObject (& Brush); // Select the painting brush into the DC; "CRECT; // getTrueRect (& Rect) gets the size of the M_Rect in CRECTTRACK, passes it to the RECT; PDOC-> M_RectTracker. Gettruerect (& Rect); PDC-> Ellipse (Rect); // Painting Ellipse; // Draw Tracking Rectangle. PDOC-> M_RectTracker.draw (PDC); // This sentence is really painted to draw this four-sided shape; // SELECT Blue Brush Out of Device Context. PDC-> SelectObject (Oldbrush); // Restore the original painting brush;} The comment is already in the program, don't say more, compile. An elliptical tetra-sided boundary (dotted), and eight black spots around the quadrangularity; this is CRECTTRACKER. You can now change the M_NStyle to try the meaning of each parameter; Step 3: How to see the example As the mouse moves automatically changes the cursor around the ellipse? Simple as long as you add the following code to create :: OnsetCursor () You can call the setCursor () function in CRECTRACKER: BOOL CRECTVIEW :: OnsetCursor (CWnd * Pwnd, uint nhittest, uint message) {// Todo : Add your message handler code here and / or call default CRectDoc * pDoc = GetDocument (); if (pWnd == this && pDoc-> m_rectTracker.SetCursor (this, nHitTest)) return TRUE; return CView :: OnSetCursor (pWnd, NHITTEST, Message);} Compile runs, the mouse changes. Step 4: We do another CRECTRACKER class for the mouse. Its role is to display the dashed line selection box after the mouse is pressed: Let's take a look at the following code: void createView :: ONLButtondown (uint nflags, cpoint point) {CRECTTRACKER TEMP; TEMP.TRACKRUBBERBAND (this, Point, True); Temp.m_Rect.NormalizeRect (); // Formalization; CView :: ONLBUTTONDOWN (NFLAGS, POINT);} Compile operation, when you press the mouse and drag, you will see the effect. .
How do we make your mouse painted a "rubber band" area? The member function in the CRECTRACKER class is: TRACKRUBBERBAND (THIS, POINT, TRUE); Note the three parameters of them: the first parameter, the pointer of the "rubber band" form, of course, the second parameter, painting "Rubber band" starting point. Let us pay attention to the third parameter, it is very interesting. When you use false (True Value is default), your "rubber band" can only be reversed from the top left to the lower right, not allowed. Compile this value for False. It is particularly worth noting that in the process of TRACKRUBBERBAND, it is ended with the right button, which is not a cView's mousemove. This must remember! At this time, the area of the mouse has been recorded in the m_rect of TEMP, and you can work according to its subsequent judgment. As for the role of the following normalized graphical function, the role of the normalization function in CRECT: the coordinates of the four corners of the quadrango match the right larger than the left and bottom larger than the top coordinate value. It is mainly to prevent you may have any errors that you can use using the false parameters of Trackrubberband. Step 5: Let us return to the blue ellipse. Before starting the new step, let's introduce the HitTest (CPoint Point) function: When your mouse is pressed, you can call this function, it will Return to the mouse point in the quad-axis:
The meanings of the return value represents -1 points in the upper left corner of the quadrilateral 0 left corner 2 upper right corner 3 left lower corner (0, 1, 2, 3 clockwise) 4 top 5 Right portion 6 bottom 7 left (Or clockwise a circle) 8 points in the quadrangular interior, but the eight points that have not hit it can be seen that if it is greater than or equal to zero, it is within the quadrangular area. If it is smaller than, it is not described within the area. So we need to add a public member function: BOOL BDRAW; for convenience, I add it to createview, (you may say, why not add DOC, I also know the principle of this, anyway, I Happy, you have to say that C gives people a lot of freedom, so you don't limit me). First initialize it to false, indicating that you don't draw the boundary, indicating that you want to draw the boundary. Definition: Class CRECTVIEW: PUBLIC CVIEW: PUBLIC CVIEW {.......... public: BOOL BDRAW; .......} Initialization: create :: createview () {// Todo: add construction code Here bdraw = false;} Change the onDraw, plus one sentence Word: Void CRECTVIEW :: OnDraw (CDC * PDC) {CRECTDOC * PDOC = getDocument (); assert_valid (pdoc); // Todo: add draw code for native data here cbrush brush (RGB (0,0,255)); //// Generating blue paintings; CBRUSH * OLDBRUSH = PDC-> SelectObject (& brush); // Select DC; CRECT RECT; PDOC-> M_RectTracker.getTrueERECT (& Rect); // GettrueRect (& Re) You can get CRECTTRACKER The size of M_Rect, passes it to the RECT; if (bdraw) // ************************************************************ PDC-> Ellipse (Rect); // Draw Ellipse; // Draw Tracking Rectangle. PDOC-> M_RectTracker.draw (PDC); // *** This drawn is really painted; *** // Select Blue Brush Out of Device Context. PDEXT. PDC-> SelectObject (Oldbrush); // Restore the original painting brush;} Compile runs, the boundary of the ellipse is not.