The classic rubber-banding approach is to do all the painting in OnMouseMove using one of
the complement-methods, such as XOR, XORNOT, etc.; each time you repaint the existing
rectangle and then draw the new one using CClientDC. When the mouse is released, you
typically Invalidate() and the OnPaint handler knows how to draw the selected items (e.g.,
reverse-video or some such). This is a classic from the Scribble tutorial
CPoint anchor;
CPoint endpoint;
void CMyClass::OnLButtonDown(...CPoint point)
{
anchor = endpoint = point;
CClientDC dc(this);
dc.DrawFocusRect(anchor.x, anchor.y, endpoint.x, endpoint.y);// very small rectangle
SetCapture();
}
void CMyClass::OnMouseMove(..., CPoint point)
{
if(GetCapture() != NULL)
{ /* rubber band */
CClientDC dc(this);
dc.DrawFocusRect(anchor.x, anchor.y, endpoint.x, endpoint.y);
endpoint = point;
dc.DrawFocusRect(anchor.x, anchor.y, endpoint.x, endpoint.y);
} /* rubber band */
void CMyClass::OnLButtonUp(..., CPoint point)
{
if(GetCapture() != NULL)
{ /* banding */
CClientDC dc(this);
dc.DrawFocusRect(anchor.x, anchor.y, endpoint.x, endpoint.y);
Selection = CRect(anchor.x, anchor.y, point.x, point.y);
ReleaseCapture();
InvalidateRect(&Selection);
} /* banding */
I think DrawFocusRect does an xor; if not, you will have to SetROP2 to select a
complementing mode.
The advantage of this technique is that it doesn't require redrawing everything, or for
that matter, anything, until the selection process has finished.
joe
Post by Charles TamIn MFC 6, how do I draw a rectangle via a mouse drag?
Is there any example code I could look at?
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm