vc实时曲线编程求解
vc里面怎么实现实时动态曲线绘制,比如实时显示下位机传回的数据?
核心函数如下:
DrawWave(CDC *pDC)这个函数主要是绘制曲线图。
OnTimer(UINT nIDEvent)这个函数式控制实时绘制的计数器函数。
例子代码如下:
(给你加了点注释希望你能看懂)
void CDrawTest::DrawWave(CDC *pDC)
{
CRect rect;
CString str;
int i;
int m_left,m_top,m_right,m_bottom;
int m_Interval = (m_High - m_Low)/10;
if (m_Interval < 1) m_Interval = 1;
// 获取绘制坐标的文本框
CWnd* pWnd = GetDlgItem(IDC_COORD);
pWnd->GetClientRect(&rect);
pDC->Rectangle(&rect);
m_left = rect.left+10;
m_top = rect.top+10;
m_right = rect.right-10;
m_bottom = rect.bottom-20;
int m_IntervalPan = (m_right - m_left)/11;
if (m_IntervalPan < 1 ) m_IntervalPan =1;
// 创建画笔对象
CPen* pPenRed = new CPen;
// 红色画笔
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
// 创建画笔对象
CPen* pPenBlue = new CPen;
// 蓝色画笔
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255));
// 创建画笔对象
CPen* pPenGreen = new CPen;
// 绿色画笔
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));
// 选中当前红色画笔,并保存以前的画笔
CGdiObject* pOldPen = pDC->SelectObject(pPenRed);
// 绘制坐标轴
pDC->MoveTo(m_left,m_top);
// 垂直轴
pDC->LineTo(m_left,m_bottom);
// 水平轴
pDC->LineTo(m_right,m_bottom);
// 写X轴刻度值
for(i=0;i<10;i++)
{
//str.Format(_T("%d"),m_Min+i*m_Interval);
str.Format(_T("%d"),m_Low+i*m_Interval);
pDC->TextOut(m_left+i*m_IntervalPan,m_bottom+3,str);
}
//str.Format(_T("%d"),m_Max);
str.Format(_T("%d"),m_High);
pDC->TextOut(m_left+10*m_IntervalPan,m_bottom+3,str);
// 绘制X轴刻度
for (i = m_left; i < m_right-20; i += 5)
{
if ((i & 1) == 0)
{
// 10的倍数
pDC->MoveTo(i + 10, m_bottom);
pDC->LineTo(i + 10, m_bottom+4);
}
else
{
// 10的倍数
pDC->MoveTo(i + 10, m_bottom);
pDC->LineTo(i + 10, m_bottom+2);
}
}
// 绘制Y轴箭头
pDC->MoveTo(m_right-5,m_bottom-5);
pDC->LineTo(m_right,m_bottom);
pDC->LineTo(m_right-5,m_bottom+5);
// 绘制X轴箭头
pDC->MoveTo(m_left-5,m_top+5);
pDC->LineTo(m_left,m_top);
pDC->LineTo(m_left+5,m_top+5);
// 绘制Y轴网格 选择绿色画笔
pDC->SelectObject(pPenGreen);
//
int iTemp = (m_bottom - m_top)/5;
for (i = 1 ;i <= 5 ;i++)
{
pDC->MoveTo(m_left,m_bottom - i*iTemp);
pDC->LineTo(m_right,m_bottom - i*iTemp);
}
//数组赋值
if(m_now<1024)
{
m_now++;
}
else
{
m_now = 0;
}
for(i = m_Low;i<m_High;i++)
{
//m_lCount[i]=(rand()%10+rand()%100+rand()%1000)%1024;
m_lCount[i] = (int)(sin(i-m_now)*204);
}
int xTemp;
int yTemp;
// 更改成蓝色画笔
pDC->SelectObject(pPenBlue);
for (i = m_Low; i <= m_High; i=i+3)
{
xTemp = m_left+(i-m_Low)*m_IntervalPan/m_Interval;
yTemp = m_bottom/2 - (int) (m_lCount[i] * m_bottom / 1024);
if (yTemp < m_top) yTemp = m_top;
if((xTemp >=m_left)&&(xTemp <=m_right))
{
pDC->MoveTo(xTemp, m_bottom/2);
pDC->LineTo(xTemp, yTemp);
}
}
// 恢复以前的画笔
pDC->SelectObject(pOldPen);
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
return;
}
void CDrawTest::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
// 获取绘制坐标的文本框
CWnd* pWnd = GetDlgItem(IDC_COORD);
pWnd->GetClientRect(&rect);
// 指针
pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
//pDC->Rectangle(&rect);
//内存绘图
CBitmap memBitmap;
CBitmap* pOldBmp = NULL;
memDC.CreateCompatibleDC(pDC);
memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
pOldBmp = memDC.SelectObject(&memBitmap);
memDC.BitBlt(rect.left,rect.top,rect.right,rect.bottom,pDC,0,0,SRCCOPY);
DrawWave(&memDC);
pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY);
memDC.SelectObject(pOldBmp);
memDC.DeleteDC();
memBitmap.DeleteObject();
CDialog::OnTimer(nIDEvent);
}
注意请及时结贴。