绘制思路:做一个自定义控件插入界面中
具体步骤:
(1)横轴划分为若干点,使用Moveto+Lineto连线,定时刷新实现动态效果;
(2)在程序中,使用CPaintDC,CClientDc,CDC,CBitMap,CBrush等绘图类;
(3)绘图程序分为几大部分:注册类句柄、刷新机制、设定坐标系、绘图;
部分代码如下:
BOOL CLineChartCtrl::RegisterWndClass(HINSTANCE hInstance)
{
WNDCLASSW wc;
wc.lpszClassName = TEXT("LineChartCtrl"); // matches class name in client
。。。
return (::RegisterClass(&wc) != 0 ); }
void CLineChartCtrl::InvalidateCtrl()
{
CClientDC dc(this);
CRect rcClient;
GetClientRect(rcClient);
if (m_MemDC.GetSafeHdc() == NULL)
{
m_MemDC.CreateCompatibleDC(&dc);
m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
m_MemDC.SelectObject(m_Bitmap);
m_MemDC.SetBkColor(RGB(255,255,255)); //静态背景色
CBrush bkBrush(RGB(160,160,160));
m_MemDC.FillRect(rcClient,&bkBrush);
}
InvalidateRect(rcClient, FALSE);
}
float CLineChartCtrl::SetPos(int nIndex, float nPos)
{
。。。}
void CLineChartCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rcClient;
GetClientRect(rcClient);
// draw scale
if (m_MemDC.GetSafeHdc() != NULL)
dc.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);
}
void CLineChartCtrl::DrawSpike()
{
CRect rcClient;
GetClientRect(rcClient);
if (m_MemDC.GetSafeHdc() != NULL) //竖线间隔宽度
{
m_MemDC.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 20, 0, SRCCOPY);
// draw scale
CRect rcRight = rcClient;
rcRight.left = rcRight.right - 20;
m_MemDC.SetBkColor(RGB(255,255,255)); //运行时背景色
CBrush bkBrush(RGB(160,160,160));
m_MemDC.FillRect(rcRight,&bkBrush);
static BOOL bDrawVerticle = FALSE;
bDrawVerticle = !bDrawVerticle;
if (bDrawVerticle)
{
CPen pen(PS_SOLID, 1, RGB(128,128,128)); //纵轴色
CPen* pOldPen = m_MemDC.SelectObject(&pen);
m_MemDC.MoveTo(CPoint(rcClient.right-2, rcClient.top));
m_MemDC.LineTo(CPoint(rcClient.right-2, rcClient.bottom));
m_MemDC.SelectObject(pOldPen);
}
int nCount = m_items.GetSize();
CLineChartItem* pItem;
CPoint ptOld, ptNew;
for (int i=0; i<nCount; i++)
{
pItem = m_items.GetAt(i);
float nRange = pItem->m_nUpper - pItem->m_nLower;
ptOld.x = rcRight.left-1; // Minus one to make sure to draw inside the area
ptNew.x = rcRight.right-1;
ptOld.y = (int)((((float)(nRange - pItem->m_nOldPos))/(float)nRange)
* (float)rcRight.Height());
ptNew.y = (int)((((float)(nRange - pItem->m_nPos))/(float)nRange)
* (float)rcRight.Height());
CPen pen(PS_SOLID, 1, pItem->m_colorLine);
CPen* pOldPen = m_MemDC.SelectObject(&pen);
m_MemDC.MoveTo(ptOld);
m_MemDC.LineTo(ptNew);
m_MemDC.SelectObject(pOldPen);
}
}
}
BOOL CLineChartCtrl::Add(COLORREF color, float Upper, float Lower)
{
CLineChartItem* pItem = new CLineChartItem;
pItem->m_colorLine = color;
pItem->m_nLower = Lower;
pItem->m_nUpper = Upper;
pItem->m_nPos = 0;
pItem->m_nOldPos = 0;
try
{
m_items.Add(pItem);
InvalidateCtrl();
return TRUE;
}
catch (CMemoryException* e)
{
if (pItem !=NULL)
delete pItem;
e->Delete();
return FALSE;
}
}
void CLineChartCtrl::Go()
{
DrawSpike();
Invalidate(FALSE);
}
注意的几点:(1) Windows CE采用unicode,必须写诸如WNDCLASSW;LoadCursorW;
(2) EVC下的CBrush不能使用两个参数如CBrush(HS_Cross,RGB(0,0,0));
(3)改动stdAfx.h包含的头文件;
(4)在void CVoltagecontrolDlg::DoDataExchange(CDataExchange* pDX)里加入 DDX_Control(pDX, IDC_LineChartCtrl, m_LineChart); 在BOOL CVoltagecontrolDlg::OnInitDialog()里加入 m_LineChart.SubclassDlgItem(IDC_LineChartCtrl, this);
(5)在设置自定义空间属性ID必须为IDC_LineChartCtrl,CLASS为LineChartCtrl,相应程序里的注册句柄名称也设成LineChartCtrl;
(6)程序中改变竖线间隔的是其宽度,而画竖线的时间间隔是2*Timer;
现有的运行效果:
横轴采用五个Edit控件使时间数值能够动态变化;图形绘制从坐标轴右侧开始,横轴随时间移动; 采样时间为1S;达到控制精度(error<0.05)要求时停止采集数据并且弹出提示对话框。
问题:(1)运行程序将出现三个警告:位于Wincore.cpp的line 348,349,4199。选择ignore后,可以运行程序。
(2)纵轴坐标通过实际值标定,且没有画出刻度线。