Use of CRECTTRACKER class

zhaozj2021-02-08  227

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 will initialize it, in the CRECTDOC :: CRECTDOC constructor: cRectorDoc :: CRECTDOC ()

{// Todo: Add One-Time Construction Code Here

m_recttracker.m_rect.setRect (0, 0, 100, 100);

M_recTracker.m_nStyle = CRECTTRACKER :: ResizeIndeside | CRECTTRACKER :: DottedLine

} Where: M_RECT is the data member used to control the size of the four-sided shape, and setRect uses View's coordinates; m_nstyle is the type of CRECTTRACKER, where: CRECTRACKER :: ResizeInde and CRectTracker :: ResizeOutside are inside the M_RECT or External painting area (which is mutually varied), 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 ();

Ask_VALID (PDOC);

// Todo: Add Draw Code for Native Data HERE

CBRUSH Brush (RGB (0, 0, 255)); // Generate a blue brush;

CBRUSH * OLDBRUSH = PDC-> SelectObject (& Brush); // Select DC in DC;

CRECT RECT;

// getTruerect (& Re) Get the size of M_Rect in CRECTTRACKER, passes it to Rect; pdoc-> m_recttracker.gettruerect (& RECT);

PDC-> Ellipse (RECT); // Draw an ellipse;

// Draw Tracking Rectangle.

PDOC-> M_Recttracker.draw (PDC); // This sentence is really painted by 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 it. 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? It is easy to add the following code to create :: OnsetCursor (): it calls the setCursor () function in CRECTTRACKER: 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 dotted selection box after the mouse is pressed: Let us look at the effect: Add to the following code in CRECTVIEW: Void CRectView :: ONLBUTTONDOWN (uint nflags, cpoint point)

{

CRECTTRACKER TEMP;

Temp.trackrubberband (this, Point, True);

Temp.m_rect.Normalizer (); // normalization;

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 What position is returned to the mouse point in the quadrangular shape: The meaning 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 6 bottom 7 left (still clockwise) 8 points in the quadrangular interior, but there is no eight points in the previous point, if the return value is larger than or equal to zero, in the quadrangular area within. 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:

Bool BDRAW;

.......

}

initialization:

CRECTVIEW :: CRECTVIEW ()

{

// Todo: Add Construction Code Here

BDRAW = FALSE;

}

Change the onDraw, plus a sentence:

Void CRECTVIEW :: OnDraw (CDC * PDC)

{

CRECTDOC * PDOC = getDocument ();

Ask_VALID (PDOC);

// Todo: Add Draw Code for Native Data HERE

CBRUSH Brush (RGB (0, 0, 255)); // Generate a blue brush;

CBRUSH * OLDBRUSH = PDC-> SelectObject (& Brush); // Select DC in DC;

CRECT RECT;

PDOC-> M_RectTracker.gettruerect (& Re); // getTrueRect (& Re) You can get the size of the M_Rect in CRectTracker and pass it to the REC.

IF (BDRAW) // ************ New statement **************

PDC-> Ellipse (RECT); // Draw an ellipse;

// Draw Tracking Rectangle.

PDOC-> M_RectTracker.draw (PDC); // *** This drawn is really painted by this four-sided shape; ***

// Select Blue Brush Out of Device Context.

PDC-> SelectObject (Oldbrush); // Restore the original painting brush;

}

Compilation and running, the border of the ellipse is not.

Ok, the preparatory knowledge is finished, let us complete this program:

Void CRECTVIEW :: ONLBUTTONDOWN (uint nflags, cpoint point)

{

// Todo: add your message handler code here and / or call default

INT nin; // Defines a click value of a mouse;

nin = getDocument () -> m_recttracker.hittest (point); // Look at what is going on?

IF (nin <0) // is not in the quadrangular area;

{

CRECTTRACKER TEMP;

Temp.trackrubberband (this, Point, True);

Temp.m_Rect.Normalizer ();

CRECTTRACKER INTERRECT;

// Create a CRECTRACKER; used to record the intersection of the mouse and the ellipse.

IF (InterRrect.m_Rect. InterSecTRect (Temp.m_Rect, getDocument () -> m_recttracker.m_rect))

BDRAW = true; // If there is an intersection, draw the border of the quadrilateral, indicating that the ellipse is selected.

Else bdraw = false;

INVALIDATE (); // Causes the occurrence of the onDraw function;

}

Else

// In the quadrangular area:

{

CClientDC DC (this);

GetDocument () -> M_Recttracker.draw (& DC);

GetDocument () -> M_RectTracker.track (this, point, true);

// Track () is the most charming function in CRECTRACKER. It changes the M_RECT of the caller.

BDRAW = True;

Invalidate ();

}

CView :: ONLBUTTONDOWN (NFLAGS, POINT);

} You may ask, why didn't I write a mousemove function, it automatically changes? This is the trial of the track () function, from calling it to lift the mouse button, and it changes the size of the square of the M_RectTracker. Then, because we used the Invalidate () function, re-painted this ellipse, so it seems to be zoomed out. My article is finished, and there is something that I don't understand, write to me. Before closing this file, you'd better review it, try another function.

转载请注明原文地址:https://www.9cbs.com/read-1636.html

New Post(0)