Question

As follow code, I want use mpDC to draw a cross line on mouse point, when I move the mouse, the cross line will shift with my mouse point,

but maybe I dont know the usage of BitBlt, so I cant see any line in my draw area (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);

My another method to draw cross line as follow code

 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);
}

Current situation is the picture enter image description here

but this code will draw many cross line when shift the mouse location,

How do I Clear the previous cross line...

Was it helpful?

Solution

the key point to do this without using bitmap is cdc->SetROP2(R2_NOT). and you should record the point for the last drawing. I try to test it by the following steps, hope is will help

  1. define CPoint m_lastPoint in C**view

  2. Initilize m_lastPoint = CPoint(-100,-100) in C**view construct function

  3. add OnMouseMove function for the message WM_MOUSEMOVE or other message you want to add.

    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;
    

OTHER TIPS

Ok, found an answer for you that doesn't use BitBlt based on micaheltang's answer: When drawing the line, first call cdc->SetROP2(R2_XORPEN); as stated here 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 under Line Blending. When erasing the line use the same mode and it should restore the previous color.

I use as follow code

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);

the cross line will reflash, but no keep in screen, how fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top