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绘制简单折线图)