MFC绘制简单折线图

void CMFC_CDCView::OnDraw(CDC* pDC)
{
	CMFC_CDCDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	
	CRect rc;
	GetClientRect(rc);
	rc.DeflateRect(50,50);

	int gridXnums = 10;
	int gridYnums = 8;
	int dx = rc.Width() / gridXnums;
	int dy = rc.Height() / gridYnums;
	
	CRect gridRect(rc.left,rc.top,rc.left+dx*gridXnums,rc.top+dy*gridYnums);
	CPen gridPen(PS_SOLID,0,RGB(0,100,200));
	CPen *oldPen = pDC->SelectObject(&gridPen);

	for (int i=0;i<=gridXnums;i++)
	{
		pDC->MoveTo(gridRect.left+i*dx,gridRect.bottom);
		pDC->LineTo(gridRect.left+i*dx,gridRect.top);
	}
	for (int j=0;j<=gridYnums;j++)
	{
		pDC->MoveTo(gridRect.left,gridRect.top+j*dy);
		pDC->LineTo(gridRect.right,gridRect.top+j*dy);
	}

	pDC->SelectObject(oldPen);
	gridPen.Detach();
	gridPen.CreatePen(PS_SOLID,0,RGB(0,0,200));
	pDC->SelectObject(gridPen);

	CBrush gridBrush(RGB(255,0,0));
	CBrush *oldBrush = pDC->SelectObject(&gridBrush);

	POINT ptRect[4] = {{-10,-10},{-10,10},{10,10},{10,-10}};
	POINT ptDraw[4];
	POINT pt[256];

	int data[20] = {18,32,54,34,25,24,36,33,48,56,12,14,37,54,32,45,34,25,34,18};
	int nCount = 20;
	int deta = gridRect.Width() / nCount;

	for (int i=0; i<nCount; i++)
	{
		pt[i].x = gridRect.left + i * deta;
		pt[i].y = gridRect.bottom - (int) (data[i] / 60.0 * gridRect.Height());
		for (int j=0; j<4; j++)
		{
			ptDraw[j].x = ptRect[j].x + pt[i].x;
			ptDraw[j].y = ptRect[j].y + pt[i].y;
		}
		pDC->Polygon(ptDraw,4);
	}
	pDC->Polyline(pt,nCount);
	pDC->SelectObject(oldPen);
	pDC->SelectObject(oldBrush);
	
}


阅读更多
文章标签: mfc
个人分类: C++
上一篇vmware中装mac os遇到的问题
下一篇Android Activity的各种重载方法
想对作者说点什么? 我来说一句

MFC动态折线图

2013年07月11日 2.69MB 下载

MFC实现折线图,柱形图,饼图

2011年12月04日 7.2MB 下载

MFC —柱状图,饼图,折线图

2011年12月08日 4.68MB 下载

没有更多推荐了,返回首页

关闭
关闭