在OnPaint()或者在OnDraw()画图都可以。下面是以一个数组里边的数据画图的例子
void CFftDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
CRect rect;
int m_Low=0;
int m_High=100;
int m_Low1=-1;
int m_High1=1;
CString str;
int i;
int m_left,m_top,m_right,m_bottom;
int m_Interval=(m_High - m_Low)/10;
int m_Interval1=(m_High1 - m_Low1)/10;
if (m_Interval < 1) m_Interval = 1;
if (m_Interval1 < 1) m_Interval1 = 1;
int a[100]={1,0,0,1,0,1,1,1,0,0,0,0,0,1,1,1,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,0,0};
GetClientRect(&rect);
dc.Rectangle(&rect);
/*m_left = rect.Width()/8;
m_top = rect.top+10;
m_right = rect.right-50;
m_bottom = rect.bottom-30;*/
m_left = rect.Width()/8;
m_top = rect.top+20;
m_right = rect.right-50;
m_bottom = rect.bottom/3;
int m_IntervalPan = (m_right - m_left)/10;
if (m_IntervalPan < 1 ) m_IntervalPan =1;
int m_IntervalPan1 = ( m_bottom-m_top)/2;
if (m_IntervalPan < 1 ) m_IntervalPan1 =1;
//绘制数据,使用蓝色画笔
CPen myPen;
myPen.CreatePen(PS_SOLID,0,RGB(0,0,255));
dc.SelectObject(&myPen);
dc.MoveTo(m_left+m_IntervalPan/ m_Interval,m_bottom-a[0]*m_IntervalPan1/m_Interval1);
for(i=1;i<=m_High;i++)
dc.LineTo(m_left+(i+1)*m_IntervalPan/ m_Interval,m_bottom-a[i]*m_IntervalPan1/m_Interval1);
myPen.DeleteObject();
free(demodData2);
//将画笔颜色改为黑色
myPen.CreatePen(PS_SOLID,0,RGB(0,0,0));
dc.SelectObject(&myPen);
//绘制坐标轴
/*dc.MoveTo(m_left,m_top);
dc.LineTo(m_left,m_bottom);
dc.LineTo(m_right+20,m_bottom);
dc.MoveTo(m_left,m_bottom);
dc.LineTo(m_left,m_bottom+150);*/
//dc.MoveTo(m_left,m_top+70);
//dc.LineTo(m_left,m_bottom);
//dc.LineTo(m_right+40,m_bottom);
dc.MoveTo(m_left,m_top+20);
dc.LineTo(m_left,m_bottom);
dc.LineTo(m_right+20,m_bottom);
dc.MoveTo(m_left,m_bottom);
dc.LineTo(m_left,m_bottom+100);
//绘制X轴
for(i=1;i<=10;i++)
{
//str.Format(_T("%d"),m_Min+i*m_Interval);
str.Format(_T("%d"),m_Low+i*m_Interval);
dc.TextOut(m_left+i*m_IntervalPan,m_bottom+3,str);
}
//绘制Y轴
/*str.Format(_T("%d"),m_Low1+1);
dc.TextOut(m_left-20,m_bottom,str);
str.Format(_T("%d"),m_Low1+m_Interval1);
dc.TextOut(m_left-20,m_bottom-m_IntervalPan1,str);*/
str.Format(_T("%d"),m_Low1);
dc.TextOut(m_left-20,m_bottom+m_IntervalPan1,str);
str.Format(_T("%d"),m_Low1+1);
dc.TextOut(m_left-20,m_bottom,str);
str.Format(_T("%d"),m_Low1+2);
dc.TextOut(m_left-20,m_bottom-m_IntervalPan1,str);
/*str.Format(_T("%d"),m_Low1+2);
dc.TextOut(m_left-20,m_bottom-m_IntervalPan1,str); */
//绘制x轴刻度
for (i = m_left; i < m_right; i+=5)
{
if ((i & 1) == 0)
{
// 10的倍数
dc.MoveTo(i + 10, m_bottom);
dc.LineTo(i + 10, m_bottom+4);
}
else
{
// 10的倍数
dc.MoveTo(i + 10, m_bottom);
dc.LineTo(i + 10, m_bottom+2);
}
}
//绘制Y轴刻度
for (i = m_top+30; i < m_bottom+80; i+=5)
{
if ((i & 1) == 0)
{
// 10的倍数
dc.MoveTo(m_left,i + 10 );
dc.LineTo( m_left+4,i + 10);
}
else
{
// 10的倍数
dc.MoveTo( m_left,i + 10);
dc.LineTo( m_left+2,i + 10);
}
}
// 绘制X轴箭头
dc.MoveTo(m_right+15,m_bottom-5);
dc.LineTo(m_right+20,m_bottom);
dc.LineTo(m_right+15,m_bottom+5);
//dc.TextOut(m_right+5,m_bottom-20,_T("t(10e-5s")));
// 绘制Y轴箭头
dc.MoveTo(m_left-5,m_top+25);
dc.LineTo(m_left,m_top+20);
dc.LineTo(m_left+5,m_top+25);
// 不为绘图消息调用 CDialog::OnPaint()
}
曲线可以理解成 很多短的直线,的点间隔越小,曲线越平滑.
MFC怎么画直线? CDC提供了很多方法, 其中就有MoveTo 和LineTo方法
坐标是什么? 直线与文字 直线的画法上面已经说了,文字用CDC的TextOut()方法.
上述的CDC方法都是需要坐标体系的支持,至于怎么把你的数据坐标让它显示在屏幕坐标上,那就得你自己定义了.