简体   繁体   English

OnLButtonUp在鼠标单击以进行CStatic控制后不会触发

[英]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. 我有一个MFC应用程序,我试图让用户通过鼠标拖动图片控件绘制一个矩形。 I created my own PictureCtrl class subclassed by CStatic. 我创建了自己的由CStatic子类化的PictureCtrl类。 However, the OnLButtonUp() is not firing when I do any mouse clicks. 但是,当我点击任何鼠标时,OnLButtonUp()不会触发。

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. 如果有人能给我任何洞察力,为什么OnLButtonUp不会被解雇,这将是值得赞赏的。 Is it because the OnMouseMove is eating up all the calls when i drag my mouse? 是因为当我拖动鼠标时OnMouseMove正在吃掉所有的电话?

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? 此外,任何人都可以给我一个关于如何修改我的代码的建议,以便如果已经绘制了一个矩形,如果用户绘制一个新的矩形,旧的矩形将被删除?

You shouldn't ReleaseCapture() in OnMouseMove or you'll miss OnLButtonUp if the mouse is outside of the window. 您不应该在OnMouseMove ReleaseCapture() ,否则如果鼠标位于窗口之外,您将错过OnLButtonUp Regarding your rectangle, use regular drawing primitives not dragging ones once the rectangle is complete. 关于矩形,使用常规绘图基元,而不是在矩形完成后拖动它们。

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: 请参阅WM_MOUSEMOVE中的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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