vc 文档绘图

void CSinusoidView::OnDraw(CDC* pDC)
{
	CSinusoidDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//建立画笔
	CPen cpen,pen;
	pen.CreatePen(PS_SOLID,4,RGB(0,0,0));
	cpen.CreatePen(PS_SOLID,2,RGB(0,0,255));
	pDC->SelectObject(&cpen);
	//指定原点
	pDC->SetViewportOrg(100,245);	
	pDC->SetTextColor(RGB(255,0,0));
	//绘制横坐标
	CString sPIText[]={"-1/2π","","1/2π","π","3/2π","2π","5/2π","3π","7/2π","4π","9/2π","5π"};
	for(int n=-1,nTmp=0;nTmp<=660;n++,nTmp+=60)
	{
		pDC->LineTo(60*n,0);
		pDC->LineTo(60*n,-5);
		pDC->MoveTo(60*n,0);
		pDC->TextOut(60*n-sPIText[n+1].GetLength()*3,16,sPIText[n+1]);
	}
	pDC->MoveTo(0,0);
	CString sTmp;
	//绘制纵坐标
	for(n=-4,nTmp=0;nTmp<=180;n++,nTmp=60*n)
	{
		pDC->LineTo(0,60*n);
		pDC->LineTo(5,60*n);
		pDC->MoveTo(0,60*n);
		sTmp.Format("%d",-n);
		pDC->TextOut(10,60*n,sTmp);
	}	
	double y,radian;	
	pDC->SelectObject(&pen);
	for(int x=-60;x<600;x++)
	{
		//弧度=X坐标/曲线宽度*角系数*π
		//Y坐标=振幅*曲线宽度*sin(弧度)
		radian =x/((double)60*2)*PI;
		y=sin(radian)*2*60;
		pDC->MoveTo((int)x,(int)y);
		pDC->LineTo((int)x,(int)y);
	}
	cpen.DeleteObject();
	pen.DeleteObject();
}
使用
MovoTo指定画笔位置
LineTo用指定画笔画一条线,从当前位位置画到一个指定的位置

ondraw和onpaint的区别:
onpaint是WM_PAINT消息处理函数,ondraw是一个虚函数只存在于document/view框架里。
在document/view框架里,当窗口显示有变化时系统自动调用onpaint,在onpaint里调用的ondraw。
refer to http://www.codeguru.com/forum/archive/index.php/t-268550.html

example78
http://download.csdn.net/detail/luck_good/3788016

你可能感兴趣的:(vc 文档绘图)