简体   繁体   中英

MFC User drawn resizeable and moveable rectangle in picture control

I'm writing an application using MFC and I'm trying to allow the user to be able to draw a rectangle via mouse drag on top of a picture control. I want to also allow the rectangles to be resizeable and moveable via mouse click by the user.

Currently, I have written part of a custom picture control class to handle the drawing on the picture control.

void PictureCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    SetCapture();
    anchor = point;
    CDC* pDC = GetDC();

    CRect rect(point,point);
    pDC->DrawDragRect(&rect, CSize(1,1), NULL, CSize(1,1), NULL, NULL);

    ReleaseDC(pDC);
    CStatic::OnLButtonDown(nFlags, point);
}
void PictureCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
    if(GetCapture() == this) 
    {
        CRect rect(anchor, point);
        rect.NormalizeRect();
        CDC *pDC = GetDC();
        pDC->DrawDragRect(&rect, CSize(1,1), &userRect, CSize(1,1), NULL, NULL);
        ReleaseDC(pDC);

    }

    CStatic::OnMouseMove(nFlags, point);
}
void PictureCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
    if(GetCapture() == this) 
    {
        CDC *pDC = GetDC();

        // pDC brush color and fill transparent
        CPen penBlack;
        penBlack.CreatePen(PS_SOLID, 3, RGB(0,0,0));
        pDC->SelectStockObject(NULL_BRUSH);
        pDC->SelectObject(penBlack);
        pDC->Rectangle(userRect);

        ReleaseDC(pDC);
        ReleaseCapture();
    }
    CStatic::OnLButtonUp(nFlags, point);

Can anyone get me started on how to make the rectangle resizeable and moveable using a mouse drag?

How can I access the rectangle object that i drew using pDC->Rectangle(userRect); ?

And I also want to delete the an old rectangle if the user draws another one, allowing only one rectangle to be on the picture control at one time. Thanks!

There is no access to the rectangle object that you drew. It is no longer an object, just paint. To select it or change the cursor when near an edge you have to compare every position reported in OnMouseMove with every line location of the rectangle. There are examples of doing similar work in the MSDN MFC sample named DrawCLI.

To delete the old rectangle you call Invalidate() to cause a full repaint of the control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM