Jay
Jay

Reputation: 15

OnLButtonUp not firing after mouse click for CStatic control

I have a MFC application where I am trying to let the user draw a rectangle via mouse drag over a picture control. I created my own PictureCtrl class subclassed by CStatic. However, the OnLButtonUp() is not firing when I do any mouse clicks.

void PictureCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    SetCapture();

    anchor = point;
    CRect rect(point,point);

    CDC* pDC = GetDC();
    pDC->DrawDragRect(&rect, CSize(1,1), NULL, CSize(1,1), NULL, NULL);
    m_lastRect = rect;
    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), &m_lastRect, CSize(1,1), NULL, NULL);
        m_lastRect = rect;
        ReleaseDC(pDC);

        ReleaseCapture();
    }

    CStatic::OnMouseMove(nFlags, point);
}

void PictureCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
    if(GetCapture() == this) 
    {
        CDC *pDC = GetDC();
        CRect rect(0,0,0,0);
        pDC->DrawDragRect(rect, CSize(1,1), &m_lastRect, CSize(1,1), NULL, NULL);
        ReleaseDC(pDC);

        ReleaseCapture();
    }

    CStatic::OnLButtonUp(nFlags, point);
}

If anyone could give me any insight to why the OnLButtonUp is not firing that would be appreciated. Is it because the OnMouseMove is eating up all the calls when i drag my mouse?

Also, can anyone give me a suggestion on how to modify my code so that if a rectangle has already been drawn, if the user draws a new one the old rectangle will be deleted?

Upvotes: 0

Views: 1703

Answers (2)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

To delete the old rectangle and then draw a new one use a special pen that doesn't have a color of its own but inverts the existing image pixels. Then drawing over the old rect will erase it. See the example in WM_MOUSEMOVE here:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx

Upvotes: 0

mark
mark

Reputation: 5469

You shouldn't ReleaseCapture() in OnMouseMove or you'll miss OnLButtonUp if the mouse is outside of the window. Regarding your rectangle, use regular drawing primitives not dragging ones once the rectangle is complete.

Upvotes: 2

Related Questions