MFC使用OnMouseMove绘制一条十字线

时间:2014-01-16 06:21:13

标签: mfc mousemove bitblt

如下面的代码,我想用mpDC在鼠标点上画一条十字线, 当我移动鼠标时,十字线会随着我的鼠标点移动,

但也许我不知道BitBlt的用法,所以我在绘制区域看不到任何一行 (rectRange),

    CWnd *pWnd;
    CRect rect;
    CDC mShowDC;
    CBitmap mShowBmp;
    CPen mpen;
    CPen *mOldpen;

    CDC *mpDC;

    mpDC = GetDC();

    mShowDC.CreateCompatibleDC(mpDC);
    mShowBmp.CreateCompatibleBitmap(mpDC,rectRange.Width(),rectRange.Height());
    mShowDC.SelectObject(mShowBmp);

    BitBlt(mShowDC,0,0,rectRange.Width(),rectRange.Height(),
        mbkCurveDC,0,0,SRCCOPY);

    //InvalidateRect(rectRange);
    if(boolShowMouseLine)
    {
        mpen.CreatePen(PS_SOLID,1,RGB(0,0,0));
        mOldpen = mShowDC.SelectObject(&mpen);

        mShowDC.MoveTo(rectRange.left,mMousePoint.y);
        mShowDC.LineTo(mMousePoint.x - 1,mMousePoint.y);

        mShowDC.MoveTo(mMousePoint.x + 1,mMousePoint.y);
        mShowDC.LineTo(rectRange.bottom,mMousePoint.y);

        mShowDC.MoveTo(mMousePoint.x,rectRange.top);
        mShowDC.LineTo(mMousePoint.x,mMousePoint.y - 1);

        mShowDC.MoveTo(mMousePoint.x,mMousePoint.y + 1);
        mShowDC.LineTo(mMousePoint.x,rectRange.right);

        mpen.DeleteObject();
        mShowDC.SelectObject(mOldpen);
    }

    mpDC->BitBlt(rectRange.left, rectRange.top,
        rectRange.Width(), rectRange.Height(), 
        &mShowDC, rectRange.left, rectRange.top, SRCCOPY);

我的另一种绘制十字线的方法,如下面的代码

 CDC *cdc;
cdc = GetDC();

if(boolShowMouseLine)
{
    cdc->MoveTo(rectRange.left,mMousePoint.y);
    cdc->LineTo(mMousePoint.x - 1,mMousePoint.y);

    cdc->MoveTo(mMousePoint.x + 1,mMousePoint.y);
    cdc->LineTo(rectRange.right,mMousePoint.y);

    cdc->MoveTo(mMousePoint.x,rectRange.top);
    cdc->LineTo(mMousePoint.x,mMousePoint.y - 1);

    cdc->MoveTo(mMousePoint.x,mMousePoint.y + 1);
    cdc->LineTo(mMousePoint.x,rectRange.bottom);
}

目前的情况是图片enter image description here

但是当移动鼠标位置时,此代码将绘制许多交叉线,

如何清除上一个交叉线...

3 个答案:

答案 0 :(得分:0)

不使用位图的关键点是cdc-> SetROP2(R2_NOT)。 你应该记录最后一幅画的点。 我尝试通过以下步骤测试它,希望是有帮助的

  1. 在C **视图中定义CPoint m_lastPoint

  2. 在C **视图构造函数中初始化m_lastPoint = CPoint(-100,-100)

  3. 为消息WM_MOUSEMOVE或您要添加的其他消息添加OnMouseMove函数。

    CDC * cdc;     cdc = GetDC();

    CPoint mMousePoint = point;
    
    CRect rectRange;//(0,0,500,500);
    GetClientRect(&rectRange);
    
    if(m_lastPoint.x >= 0 && m_lastPoint.y >= 0)
    {
        cdc->SetROP2(R2_NOT);
        mMousePoint = m_lastPoint;
        cdc->MoveTo(rectRange.left,mMousePoint.y);
        cdc->LineTo(mMousePoint.x - 1,mMousePoint.y);
    
        cdc->MoveTo(mMousePoint.x + 1,mMousePoint.y);
        cdc->LineTo(rectRange.right,mMousePoint.y);
    
        cdc->MoveTo(mMousePoint.x,rectRange.top);
        cdc->LineTo(mMousePoint.x,mMousePoint.y - 1);
    
        cdc->MoveTo(mMousePoint.x,mMousePoint.y + 1);
        cdc->LineTo(mMousePoint.x,rectRange.bottom);
    }
    cdc->SetROP2(R2_BLACK);
    mMousePoint=point;
    cdc->MoveTo(rectRange.left,mMousePoint.y);
    cdc->LineTo(mMousePoint.x - 1,mMousePoint.y);
    
    cdc->MoveTo(mMousePoint.x + 1,mMousePoint.y);
    cdc->LineTo(rectRange.right,mMousePoint.y);
    
    cdc->MoveTo(mMousePoint.x,rectRange.top);
    cdc->LineTo(mMousePoint.x,mMousePoint.y - 1);
    
    cdc->MoveTo(mMousePoint.x,mMousePoint.y + 1);
    cdc->LineTo(mMousePoint.x,rectRange.bottom);
    m_lastPoint = mMousePoint;
    

答案 1 :(得分:0)

好的,根据micaheltang的回答找到了不使用BitBlt的答案: 绘制线时,首先调用cdc-> SetROP2(R2_XORPEN);如同在线混合下的http://books.google.co.il/books?id=eDvx4Qx63b0C&pg=PA105&lpg=PA105&dq=MFC+blending+line+with+background&source=bl&ots=v3ycFSlHL7&sig=agpZHLBgnocSXZLQ6qSM6nWFIzM&hl=en&sa=X&ei=wZ_XUsOvLYXStAa054H4Cw&ved=0CCoQ6AEwAA#v=onepage&q=MFC%20blending%20line%20with%20background&f=false所述。擦除线条时使用相同的模式,它应恢复以前的颜色。

答案 2 :(得分:0)

我使用以下代码

CClientDC dc(this);
int oldmode=dc.SetROP2(R2_NOTXORPEN);
        COLORREF color;

        color = RGB(0,0,0);

        CPen pen(PS_DASH, 2, color), *oldpen;
        oldpen = dc.SelectObject(&pen);

        dc.MoveTo(rectRange.left, mMousePoint.y);
        dc.LineTo(mMousePoint.x - 1, mMousePoint.y);

        dc.MoveTo(mMousePoint.x + 1, mMousePoint.y);
        dc.LineTo(rectRange.right, mMousePoint.y);

        dc.MoveTo(mMousePoint.x, rectRange.top);
        dc.LineTo(mMousePoint.x, mMousePoint.y - 1);

        dc.MoveTo(mMousePoint.x, mMousePoint.y + 1);
        dc.LineTo(mMousePoint.x, rectRange.bottom);

        PEndPoint = point;

        dc.MoveTo(rectRange.left, mMousePoint.y);
        dc.LineTo(mMousePoint.x - 1, mMousePoint.y);

        dc.MoveTo(mMousePoint.x + 1, mMousePoint.y);
        dc.LineTo(rectRange.right, mMousePoint.y);

        dc.MoveTo(mMousePoint.x, rectRange.top);
        dc.LineTo(mMousePoint.x, mMousePoint.y - 1);

        dc.MoveTo(mMousePoint.x, mMousePoint.y + 1);
        dc.LineTo(mMousePoint.x, rectRange.bottom);

        dc.SelectObject(oldpen);
        dc.SetROP2(oldmode);
        ReleaseDC(&dc);

十字线会重新闪烁,但屏幕上没有保留,如何修复它。