MFC下编写的简易画图系统

主要声明及函数实现:

class CDrawDoc : public CDocument
{
public:
	CList<CPoint,CPoint&> linelist;
	CList<CPoint,CPoint&> rectlist;
	int lineCount;
	int rectCount;
........
}

class CDrawView : public CView
{
private:
	bool m_bFlag;
	CPoint m_headp;
	CPoint m_endp;
	CPoint *line;
	CPoint *rect;
	int lineCount;
	int rectCount;
......
}

CDrawView::CDrawView()
{
    // TODO: add construction code here
    m_bFlag = true;
    line = new CPoint[30];
    rect = new CPoint[30];
    lineCount = 0;
    rectCount = 0;
}

void CDrawView::OnDraw(CDC* pDC)
{
    CDrawDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
	if(lineCount>=30)
	{
		CPoint *Line = line;
		line = new CPoint[lineCount+30];
		for(int i=0;i<lineCount;i++)
			line[i] = Line[i];
		delete Line;
	}
	if(rectCount>=30)//数组动态实现
	{
		CPoint *Rect = rect;
		rect = new CPoint[rectCount+30];
		for(int i=0;i<rectCount;i++)
			rect[i] = Rect[i];
		delete Rect;
	}
    if(m_bFlag)
    {
		line[lineCount++]=m_headp;      //将点计入数组中
		line[lineCount++]=m_endp;
        pDoc->linelist.AddTail(m_headp);//将点加入到链表中
        pDoc->linelist.AddTail(m_endp);
        pDoc->lineCount++;
    }
    else
    {
		rect[rectCount++]=m_headp;
		rect[rectCount++]=m_endp;
        pDoc->rectlist.AddTail(m_headp);
        pDoc->rectlist.AddTail(m_endp);
        pDoc->rectCount++;
    }
	//for(int i=0;i<lineCount/2;i++)
	//{
	//pDC->MoveTo(line[i*2]);
	//pDC->LineTo(line[i*2+1]);
	//      	}
	//     
	//      	for(int j=0;j<rectCount/2;j++)
	//      	{
	//      		CRect rect(line[j*2],line[j*2+1]);
	//      		pDC->Rectangle(rect);
	//      	}
	// 	for(int j=0; j<lineCount; j++)
	// 	{
	// 		//循环打印图形,起到重绘的效果
	// 		pDC->MoveTo(m_headp);
	// 		pDC->LineTo(m_endp);
	// 	}
	//     for(int k=0; k<lineCount; k++)
	//     {
	//         CRect rect(m_headp,m_endp);
	//         pDC->Rectangle(rect);
	//     }
    POSITION posLine;
    POSITION posRect;
    posLine=pDoc->linelist.GetHeadPosition();
    posRect=pDoc->rectlist.GetHeadPosition();
    while(posLine!=NULL)
    {
        m_headp=pDoc->linelist.GetNext(posLine);
        m_endp=pDoc->linelist.GetNext(posLine);
        pDC->MoveTo(m_headp);
        pDC->LineTo(m_endp);
    }
    while (posRect!=NULL)
    {
        m_headp=pDoc->rectlist.GetNext(posRect);
        m_endp=pDoc->rectlist.GetNext(posRect);
        pDC->Rectangle(&CRect(m_headp,m_endp));
    }
}

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_headp = point;
    CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    m_endp = point;
    Invalidate();
    CView::OnLButtonUp(nFlags, point);
}

void CDrawView::OnAppLine()
{
    // TODO: Add your command handler code here
    m_bFlag = true;//修改属性画直线
}

void CDrawView::OnAppRect()
{
    // TODO: Add your command handler code here
    m_bFlag = false;//修改属性画矩形
}

void CDrawDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
	linelist.Serialize(ar);
//打开保存串行化实现	
	rectlist.Serialize(ar);
}

MFC下编写的简易画图系统_第1张图片

你可能感兴趣的:(MFC画图)