VC move the mouse to draw a Rectangular Box dynamically

Source: Internet
Author: User

The function to be implemented is actually very simple. Most people are not familiar with the feature, but it is a bit interesting to implement it.

Detailed Problem description: (three steps)

Click the left mouse button to record the click point;

When you move the mouse, the displayed Rectangular Box dynamically follows the display;

When the left mouse button is released, the rectangular box is wiped out.

In this way, the problem is actually simpler. You only need to correspond to the three events of the mouse.

The Code has been modified in three places:

First: Set private variables in the View class (in the View class. h file)

Bool m_startrect; // draw a rectangle frame sign
Cpoint m_startpoint; // the start point of the rectangle frame.
Cpoint m_oldpoint; // the end point of the rectangle frame (but it is the last vertex, so the old mark is used here)

Second: Initialize private variables in the view Constructor (in the View class. cpp file)

Cmousedragview: cmousedragview ()
// Initialize private variables
M_startrect = false;
M_startrect = 0;
M_oldpoint = 0;

Third: Define the message response function (in the View class. cpp file)

// Click the left mouse button
Void cmousedragview: onlbuttondown (uint nflags, cpoint point)
// Todo: add your message handler code here and/or call default
M_startrect = true; // click the left mouse button to set the rectangle to be drawn.
M_startpoint = point; // record start point
M_oldpoint = point; // set the old point to the start point.

Cview: onlbuttondown (nflags, point );

// Drag the mouse
Void cmousedragview: onmousemove (uint nflags, cpoint point)
Cclientdc DC (this); // get the device handle

// Setrop2 specifies the new drawing mode. (msdn)
// R2_not pixel is the inverse of the screen color. (msdn)
// This function is used to define the color of the painting. This parameter sets the color to the reversed color of the original screen color.
// In this way, if you draw the screen twice in a row, you can restore the color of the original screen (as shown below)
// However, the two consecutive draws are not completed in a message response.
// It can be displayed (that is, what you see) when you drag the response for the first time, and the second drag and draw will be wiped out (you will not be able to see it)
DC. setrop2 (r2_not); // This is the key !!!
DC. selectstockobject (null_brush); // do not use a paint brush
If (true = m_startrect) // determines whether a rectangle can be drawn based on whether a click exists.
DC. rectangle (crect (m_startpoint, m_oldpoint ));
DC. rectangle (crect (m_startpoint, point ));
M_oldpoint = point;

Cview: onmousemove (nflags, point );

// Release the left mouse button
Void cmousedragview: onlbuttonup (uint nflags, cpoint point)
M_startrect = false; // reset the rectangle frame sign.

// Hide the last rectangle (the principle is the same as that when dragging the rectangle)
Cclientdc DC (this );
DC. setrop2 (r2_not );
DC. selectstockobject (null_brush );
DC. rectangle (crect (m_startpoint, m_oldpoint ));

Cview: onlbuttonup (nflags, point );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.