画直线(橡皮筋)

来源:互联网 发布:查看linux安装的字体 编辑:程序博客网 时间:2024/04/28 06:51

  在《深入浅出MFC》中的step2的程序的基础上进行修改,原程序是连续画线,进行修改使它画直线

void CScribbleView::OnLButtonDown(UINT, CPoint point) 

{

    // Pressing the mouse button in the view window starts a new stroke

 

m_pStrokeCur = GetDocument()->NewStroke();

// Add first point to the new stroke

m_pStrokeCur->m_pointArray.Add(point);

 

SetCapture();       // Capture the mouse until button up.

m_ptPrev = point;   // Serves as the MoveTo() anchor point for the

// LineTo() the next point, as the user drags the

// mouse.

m_ptNext = m_ptPrev;

 

m_button = TRUE;

return;

}

 

void CScribbleView::OnLButtonUp(UINT, CPoint point) 

{

// Mouse button up is interesting in the Scribble application

// only if the user is currently drawing a new stroke by dragging

// the captured mouse.

 

if (GetCapture() != this)

return; // If this window (view) didn't capture the mouse,

// then the user isn't drawing in this window.

 

CScribbleDoc* pDoc = GetDocument();

 

CClientDC dc(this);

 

CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());

dc.MoveTo(m_ptPrev);

dc.LineTo(point);

dc.SelectObject(pOldPen);

m_pStrokeCur->m_pointArray.Add(point);

 

ReleaseCapture();   // Release the mouse capture established at

// the beginning of the mouse drag.

m_button =FALSE;

return;

}

 

void CScribbleView::OnMouseMove(UINT, CPoint point) 

{

// Mouse movement is interesting in the Scribble application

// only if the user is currently drawing a new stroke by dragging

// the captured mouse.

//

if(m_button)

{

CClientDC   dc(this);       

dc.SetROP2(R2_NOT); 

//该函数的主要的作用是根据nDrawMode设置的方式重新设定绘图的方式,下面就不同的nDrawMode值具体解释绘图模式是如何改变的

dc.MoveTo(m_ptPrev);    

dc.LineTo(m_ptNext);     

m_ptNext = point;     

dc.MoveTo(m_ptPrev);    

dc.LineTo(m_ptNext);    

}

return;

}