根据这段源代码 一步一步剖析CDC的使用
void CDrawSinXView::OnDraw(CDC* pDC)
{
CDrawSinXDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//建立画笔
CPen pen_Zuobixi,pen_sinx;
pen_Zuobixi.CreatePen(PS_SOLID,4,RGB(0,0,0));
pen_sinx.CreatePen(PS_SOLID,2,RGB(0,0,255));
pDC->SelectObject(&pen_Zuobixi);
//指定原点
pDC->SetViewportOrg(100,255);
pDC->SetTextColor(RGB(255,0,0));
//绘制横坐标
CString sPIText[]={"-1/2π","","1/2π","π","3/2π","2π","5/2π","3π","7/2π","4π","9/2π","5π"};
int n=-1;
int nTemp=0;
while (nTemp<=660)
{
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]);
n++;
nTemp +=60;
}
pDC->MoveTo(0,0);
CString strTemp;
//绘制纵坐标
for(n=-4,nTemp = 0;nTemp<=180;n++,nTemp+=60)
{
pDC->LineTo(0,60*n);
pDC->LineTo(5,60*n);
pDC->MoveTo(0,60*n);
strTemp.Format("%d",-n);
pDC->TextOut(10,60*n,strTemp);
}
double y,radian;
pDC->SelectObject(&pen_sinx);
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);
}
pen_sinx.DeleteObject();
pen_Zuobixi.DeleteObject(); }
void CDrawSinXView::OnDraw(CDC* pDC)
这个函数在File 中 的view.cpp中.
一个一个剖析..
CDrawSinXDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
系统自带的,并不清楚有什么卵用....
<span style="font-family: Arial, Helvetica, sans-serif;">//建立画笔</span>
CPen pen_Zuobixi,pen_sinx;
pen_Zuobixi.CreatePen(PS_SOLID,4,RGB(0,0,0));
pen_sinx.CreatePen(PS_SOLID,2,RGB(0,0,255));
pDC->SelectObject(&pen_Zuobixi);
如注释所示
1.用CPen 建立一个画笔,并且规定画笔的虚实线(画笔类型),粗,颜色;
此处详情见
http://blog.sina.com.cn/s/blog_71f17cbd0100qsgn.html
2.通过SelectObject来选择当前的画笔
3.RGB 0 0 0 是黑色
4.RGB 0 0 255是 蓝色
//指定原点
pDC->SetViewportOrg(100,255);
pDC->SetTextColor(RGB(255,0,0));
1.SetViewportOrg 来设定原点的坐标 ..
2.SetTextColor 设置字体颜色
//绘制横坐标
CString sPIText[]={"-1/2π","","1/2π","π","3/2π","2π","5/2π","3π","7/2π","4π","9/2π","5π"};
int n=-1;
int nTemp=0;
while (nTemp<=660)
{
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]);
n++;
nTemp +=60;
}
pDC->MoveTo(0,0);
1.LinkTo,表示画笔从当前位置往(x,y)的位置画去
2.MoveTo 表示画笔回到(x,y)的位置(不留下轨迹)
3.TextOut (x,y,string);
CString strTemp;
//绘制纵坐标
for(n=-4,nTemp = 0;nTemp<=180;n++,nTemp+=60)
{
pDC->LineTo(0,60*n);
pDC->LineTo(5,60*n);
pDC->MoveTo(0,60*n);
strTemp.Format("%d",-n);
pDC->TextOut(10,60*n,strTemp);
}
1.与上面的类似...但是利用FOrmat来得到CSrting;
double y,radian; //绘制sinx
pDC->SelectObject(&pen_sinx);
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);
}
pen_sinx.DeleteObject();
pen_Zuobixi.DeleteObject();
直接绘制吧..
记得DeleteObject来删除画笔...
照猫画虎 完成了 -2Pi->2Pi 的sin图像。。
代码如下:
void CWj1401_0302140107_6View::OnDraw(CDC* pDC)
{
CPen pen_Zuobixi,pen_sinx; //创建画笔
pen_Zuobixi.CreatePen(PS_SOLID,4,RGB(0,0,0)); //描述画笔
pen_sinx.CreatePen(PS_SOLID,2,RGB(0,0,255)); //描述画笔
pDC->SelectObject(&pen_Zuobixi); //设定谁为现在的画笔
//指定原点
pDC->SetViewportOrg(300,255); //指定原点
pDC->SetTextColor(RGB(255,0,0)); //指定文本颜色
//绘制横坐标
CString sPIText[]={"-2π","-3/2π","-1π","-1/2π","","1/2π","1π","3/2π","2π"}; //创建文本
int n=-1;
int nTemp=0;
while (nTemp<=480)
{
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]); // (横坐标,纵坐标,字符串)
n++;
nTemp +=60;
}
pDC->MoveTo(180,0);
CString strTemp;
//绘制纵坐标
for(n=-2,nTemp = 0;nTemp<=240;n++,nTemp+=60)
{
pDC->LineTo(180,60*n);
pDC->LineTo(185,60*n);
pDC->MoveTo(180,60*n);
strTemp.Format("%d",-n);
pDC->TextOut(190,60*n,strTemp);
}
double y,radian;
pDC->SelectObject(&pen_sinx);
for(int x=-60;x<420;x++)
{
//弧度=X坐标/曲线宽度*角系数*π
//Y坐标=振幅*曲线宽度*sin(弧度)
radian =x/((double)60*2)*PI;
y=-cos(radian)*60;
pDC->MoveTo((int)x,(int)y);
pDC->LineTo((int)x,(int)y);
}
pen_sinx.DeleteObject();
pen_Zuobixi.DeleteObject(); }