VC mouse Drag dynamic draw rectangular box

Source: Internet
Author: User

To realize the function is actually very simple, the average person used to see accustomed to, but to achieve a little meaning.

Detailed description of the problem: (three steps)

Record the click Point when the left mouse button is clicked;

Drag the mouse, the display of the rectangular box can be dynamic follow the display;

When you release the left mouse button, wipe out the rectangular box.

This analysis in fact, the problem is simple, only need to three of the mouse event to be appropriate.

The code was modified mainly in three places:

The first: Set private variables in the view class (in the view class. h file)

BOOL M_startrect; Draw a rectangle box flag
CPoint M_startpoint; Rectangular box Start point
CPoint M_oldpoint; The end of the rectangle box (but it was the last point, so the old logo is used here)

Second: In the View class constructor, initialize the private variable (in the View class. cpp file)

Cmousedragview::cmousedragview ()
Initializing private variables
M_startrect = FALSE;
M_startrect = 0;
M_oldpoint = 0;

Third: Define message response functions (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; Left mouse click, set to start drawing rectangular box
M_startpoint = point; Record start point
M_oldpoint = point; Set the old point also for the start point

Cview::onlbuttondown (nflags, point);

Drag the mouse
void Cmousedragview::onmousemove (UINT nflags, CPoint Point)
CCLIENTDC DC (this); Get device handle

SETROP2 Specifies the new drawing mode. (MSDN)
R2_not Pixel is the inverse of the screen color. (MSDN)
That is, the function is used to define the painted color, which sets the color to the reverse color of the original screen color
In this way, if you draw two consecutive times, you can restore the original screen color (below)
However, the two consecutive drawings here are not done in a single message response
Instead, it can be displayed (that is to see) when the first drag response is drawn, and the second drag drawing implementation is erased (and is not visible)
dc.   SetROP2 (R2_not); This is the key!!!
dc. Selectstockobject (Null_brush); Do not use painting brushes
if (TRUE = = m_startrect)////depending on whether there is a click to determine whether a rectangle can be drawn
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 Draw Rectangle Border Flag

The last rectangle is hidden (the principle is the same as the rectangle drawing principle when dragging)
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.