Search code examples
mfcmousemovebitblt

MFC draw a cross line using OnMouseMove


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...


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;