VC ++ MFC rubber band technology

Source: Internet
Author: User

Draw a straight line under MFC and use the rubber band technology to move the line effect following the mouse

// Onlbuttiondown
M_ptorigin = m_ptend = point;
// Onmousemove
Cclientdc DC (this );
If (nflags = mk_lbutton)
{
DC. setrop2 (r2_not );
DC. moveTo (m_ptorigin );
DC. lineto (m_ptend );
M_ptend = point;
DC. moveTo (m_ptorigin );
DC. lineto (m_ptend );
}

However, this can only be used to draw black straight lines. We want to draw lines of other colors and use different types of lines and widths. Therefore, we need to create a paint brush and specify the type of the paint brush we want, line, dotted line, dot line, specify the line width and paint color.

Void cgraphic1view: onmousemove (uint nflags, cpoint point)
{
If (mk_lbutton = nflags)
{
Cclientdc DC (this );
Int oldmode = Dc. setrop2 (r2_notxorpen );
Cpen pen (m_nlinestyle, m_nlinewidth, m_clr), * oldpen;
Oldpen = Dc. SelectObject (& pen );

DC. moveTo (m_ptorigin );
DC. lineto (m_ptend );

M_ptend = point;

DC. moveTo (m_ptorigin );
DC. lineto (m_ptend );

DC. SelectObject (oldpen );
DC. setrop2 (oldmode );
Releasedc (& DC );
}
Cscrollview: onmousemove (nflags, point );
}

Where

Cpen pen (m_nlinestyle, m_nlinewidth, m_clr), * oldpen;

M_nlinestyle is a line style, m_nlinewidth is a line width, m_clr is a paint brush color, you can also specify the following:

Cpen pen (0, 0, RGB (255, 0, 0), * oldpen;

 

The following describes how the rubber band effect is achieved. When we press the left mouse button, m_ptorigin = m_ptend = point;
When the mouse moves, the wm_mousemove message is sent, and onmousemove is called for processing. In this response function, the effect of the rubber band is implemented. If (mk_lbutton = nflags) determines whether to move the mouse with the Left click. The setrop2 function is used to set the hybrid mode of the current foreground color. The value r2_not indicates the inverse, that is, the reversed color of the background color of the foreground color. r2_not is often used to draw the rubber line. Because the background color can be restored after two reverse operations, only the black line can be used.

The color drawn by r2_notxorpen is the opposite of that drawn by r2_xorpen. r2_xorpen is the same or different between the screen color and the paint brush color. When onmousemove is called for the first time, there is no draw line, so the screen color is white. r2_xorpen is the color of the current paint brush, so r2_notxorpen is the color of the current paint brush. That is to say, the line drawn for the first time is the color of the paint brush.

When onmousemove is called for the second time, the m_ptorigin and m_ptend points have not changed. Therefore, you can use these two points to draw a line to overwrite the line drawn for the first time and change it to the canvas color, then draw a line between the new point and m_ptorigin, and the color is the paint brush color. Draw a line on the old line. Because the line has a color, r2_xorpen (screen color = paint color) will become black (1 XOR 1 = XOR 0 = 0 ), reversed: r2_notxorpen is white, which is the color of the canvas. It looks like it disappears, but the line turns white (if the paint brush is not white, for example, if you use the system to set the eye protection color, the customer area turns to a light green color that does not hurt the eye, so that the color displayed is still white, rather than the customer area color ). After the old line is deleted, you can draw a line again on the new point.

 

Extension: If you want to implement the effect of the rubber band of a rectangle or an elliptical shape

DC. moveTo (m_ptorigin );
DC. lineto (m_ptend );

Replace:

DC. rectangle (crect (this-> m_ptorigin, m_ptend ));

Or

DC. ellipse (crect (this-> m_ptorigin, m_ptend ));

You can.

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: info-contact@alibabacloud.com 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.