关闭

MFC绘制简单折线图

标签: mfc
4417人阅读 评论(0) 收藏 举报
分类:
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);
	
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:114881次
    • 积分:1481
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:32篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论