CrecttrackerClass usage
Author: 2 eggs
Crecttracker (commonly known as the "rubber band" class) is a very interesting class. You often see this situation in Windows: it can be used for display boundary, you can also use its eight corners to zoom in and out, or use it as a box. How can we implement this function through programming? This is the role of the crecttracker class;
(Box selected) (display borders and zoom)
Example (source program)
Open the project file above and compile and run it. You will see several features of crecttracker;
Next, let's create a new project file from scratch to gradually master its functions.
Create a project file for a single document and name it rect. Click Finish to create the project. compile the project to generate the OBJ file for the first time. During the process of generating the object, we will continue to explain it;
Step 1:
Generate a public data member in the crectdoc class: m_recttracker; set it to public because it needs to be called in view. Next we will initialize it, in the 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; }
M_rect is the data member used in the crecttracker to control the size and position of the Quadrilateral, setrect uses the View coordinates, and m_nstyle is the crecttracker type, where: crecttracker: resizeinside and crecttracker :: resizeoutside indicates the internal or external painting areas of m_rect (they are different from each other). crecttrakcer: dottedline draws the boundary of the Quadrilateral using a dot line. Other values are: crecttracker: solidline: used to draw solid line boundaries; (and dottedline are different) crecttracker: hatchedborder: border strip line; crecttracker: hatchinside: internal strip; you can run the preceding example. All the above parameters are used. You can also use them one by one in step 2 to better understand their respective meanings;
Step 2:
Next we draw a blue oval in the view; continue our work in the ondraw of the crectview:
Void crectview: ondraw (CDC * PDC) {crectdoc * pdoc = getdocument (); assert_valid (pdoc); // todo: add draw code for native data here cbrush brush (RGB (255,); // generate a blue paint brush; cbrush * oldbrush = PDC-> SelectObject (& brush ); // select the image brush into DC; crect rect; // gettruerect (& rect) to get the m_rect size in the crecttracker and pass it to rect; pdoc-> m_recttracker.gettruerect (& rect ); PDC-> ellipse (rect); // draw an elliptic; // draw tracking rectangle. pdoc-> m_recttracker.draw (PDC); // This sentence is used to draw the Quadrilateral. // select blue brush out of device context. PDC-> SelectObject (oldbrush); // restore the original paint brush ;}
The annotation is already in the program. You don't need to talk about it. compile it. An Elliptical outer band quadrilateral boundary (DOT strip) with eight black spots around the Quadrilateral. This is crecttracker. You can change m_nstyle to see the meaning of each parameter;
Step 3: How does one automatically change the cursor around the ellipse as the mouse moves in the example? Simply add the following code to crectview: 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 and run it. The mouse changes. Step 4: Let's create another crecttracker class for the mouse. Its function is to show the selection box of the dotted line after the mouse is pressed: Let's see the effect first:
Add the following code to the crectview:
Void crectview: onlbuttondown (uint nflags, cpoint point) {crecttracker temp; temp. trackrubberband (this, point, true); temp. m_rect.normalizerect (); // normalization; cview: onlbuttondown (nflags, point );}
Compile and run. When you press the mouse and drag it, you will see the effect.
How can we draw a "rubber band" area with the mouse? The member function in the crecttracker class is: trackrubberband (this, point, true). Note the three parameters:
The first parameter is the pointer to the form of the "rubber band", of course, this
The second parameter draws the starting point of the "rubber band.
The third parameter is very interesting. When you use false (the default value is true), your "rubber band" can only be drawn from the top left to the bottom right, and the reverse direction is not allowed. Compile and run the value false.
It is particularly worth noting that the process of trackrubberband ends with the right-click lift, and no cview mousemove occurs. Remember this 1.1 million! At this time, the area drawn by the mouse has been recorded in m_rect of temp. You can perform subsequent judgment based on it. As for the following normalization statement functions, they serve the same purpose as the normalization functions in crect: Make the coordinates of the four corners of the Quadrilateral conform to the coordinates with the right greater than the left and the base greater than the top. It is mainly used to prevent possible errors caused by the use of the false parameter of trackrubberband.
Step 5:
Let's go back to the blue oval. Before starting the new step, we will first introduce the hittest (cpoint point) function: when your mouse is pressed, you can call this function to return the cursor position in the Quadrilateral:
|
Return Value |
Meaning |
|
-1 |
Point outside the Quadrilateral |
|
0 |
Upper left corner |
|
1 |
Upper right corner |
|
2 |
Bottom right corner |
|
3 |
In the lower left corner (0, 1, 2, 3, and 1 circle clockwise) |
|
4 |
Top |
|
5 |
Right |
|
6 |
Bottom |
|
7 |
Left (or clockwise) |
|
8 |
Points inside the Quadrilateral, but does not hit the first eight points |
It can be seen that if the returned value is greater than or equal to zero, it is within the quadrilateral area. If the value is smaller than the value, it indicates that it is not in the region range. Therefore, we need to add a public member function: bool bdraw; for convenience, I add it to the crectview. (You may say, why not add it to the doc, I also know that there is a working principle of Bo's programming. I'm so happy that C ++ gives people a lot of freedom, so don't restrict me ). Initialize it to false, indicating that the boundary is not painted. If it is true, it indicates that the boundary is to be painted. Definition:
Class crectview: Public cview {............ Public: bool bdraw ;.......} Initialization: crectview () {// todo: Add construction code here bdraw = false;} change ondraw and add a sentence: 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 a blue paint brush; cbrush * oldbrush = PDC-> SelectObject (& brush); // select the paint brush to DC; crect rect; pdoc-> m_recttracker.gettruerect (& rect ); // gettruerect (& rect) to obtain crecttrac The m_rect size in the Ker and pass it to the rect; If (bdraw) // ************* newly added statement ***************** PDC-> ellipse (rect); // draw an ellipse; // draw tracking rectangle. pdoc-> m_recttracker.draw (PDC); // *** this sentence is used to draw the Quadrilateral. *** // select blue brush out of device context. PDC-> SelectObject (oldbrush); // restore the original paint brush;} compile and run it. the boundary of the ellipse is gone. Okay, the preparation is complete. Let's complete this program: void crectview: onlbuttondown (uint nflags, cpoint point) {// todo: add your message handler code here and/or call default int Nin; // defines the click value of a mouse; nin = getdocument ()-> m_recttracker.hittest (point ); // see where it is. If (nin <0) // It is not in the quadrilateral area; {crecttracker temp; temp. trackrubberband (this, point, true); temp. m_rect.normalizerect (); crecttracker interrect; // create a crecttracker to record the intersection of the mouse and the ellipse. If (interrect. m_rect.intersectrect (temp. m_rect, getdocument ()-> m_recttracker.m_rect) bdraw = true; // if there is an intersection, draw the boundary of the Quadrilateral, indicating that the else bdraw = false is selected; invalidate (); // cause ondraw function;} else // In the quadrilateral area: {cclientdc (this); getdocument ()-> m_recttracker.draw (& DC); getdocument () -> m_recttracker.track (this, point, true); // track () is the most attractive function in crecttracker. It changes the caller's m_rect; bdraw = true; invalidate ();} cview: onlbuttondown (nflags, point );}
You may ask why I didn't write the mousemove function and it automatically changed the size? This is the credit of the track () function. From calling it to lifting the mouse key, it changes the size of the Quadrilateral of m_recttracker at all times. Then, because we used the invalidate () function, we re-painted the ellipse, so it seemed to have been zoomed in and out. I have finished writing my article. I want to write a letter to me if I have any questions. Before closing this file, you 'd better review it and try other functions.