// Dlg_RealTimeCurve.cpp : 实现文件 // #include "stdafx.h" #include "Dlg_RealTimeCurve.h" // CDlg_RealTimeCurve 对话框 IMPLEMENT_DYNAMIC(CDlg_RealTimeCurve, CDialog) CDlg_RealTimeCurve::CDlg_RealTimeCurve(CWnd* pParent /*=NULL*/) : CDialog(CDlg_RealTimeCurve::IDD, pParent) { m_nLowwer = 0; m_nUpper = 200; m_nLowerLimit = 0; m_nUpperLimit = 0; for (int i=0; iGetAnalogValue(m_sVarName); m_nValue1[MAX_NUM-1] = (int)fValue; m_sCurData.Format( "%s【%0.1f】",m_sVarDes,fValue ); SetTimer( 1,1000,NULL ); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } int CDlg_RealTimeCurve::getMin( int iLowwer ) { int nRet=0; nRet = iLowwer-20; if( nRet<0 ) nRet = 0; int nMod = nRet%10; if( nMod!=0 ) nRet -= nMod; return nRet; } int CDlg_RealTimeCurve::getMax( int iUpper ) { int nRet=0; nRet = iUpper+20; int nMod = nRet%10; if( nMod!=0 ) nRet += ( 10-nMod ); return nRet; } void CDlg_RealTimeCurve::OnTimer(UINT nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 // 用关键代码同步 EnterCriticalSection ( & g_cs ) ; for(int i=0; iGetAnalogValue(m_sVarName); m_nValue1[MAX_NUM-1] = (int)fValue; m_sCurData.Format( "%s【%0.1f】",m_sVarDes,fValue ); Invalidate(false); //Draw(); //m_ctrlMultiColorPlot.SetData( ( float ) m_nValue1[MAX_NUM-1] ) ; // 离开关键代码 LeaveCriticalSection ( & g_cs ) ; CDialog::OnTimer(nIDEvent); } void CDlg_RealTimeCurve::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialog::OnPaint() Draw(); } void CDlg_RealTimeCurve::Draw() { // 用关键代码同步 EnterCriticalSection ( & g_cs ) ; CString sText; CDC *pDC=this->GetDC(); CRect rect; this->GetClientRect( rect ); //rect.NormalizeRect(); pDC->SetBkColor( RGB( 64,128,128 ) ); CBrush m_brush; m_brush.CreateSolidBrush( RGB( 64,128,128 ) ); CBrush* pOldBrush=(CBrush*)pDC->SelectObject(&m_brush); pDC->Rectangle(rect); DeleteObject( m_brush ); // 边框 pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT); CRect inner; inner.left = rect.left+40; inner.right = rect.right-10; inner.top = rect.top+10; inner.bottom = rect.bottom-30; // 网格 CPen penGrid(PS_DOT, 1, RGB(192, 192, 192)); CPen* pOldPen = pDC->SelectObject(&penGrid); pDC->SetTextColor( RGB(0,0,0) ); pDC->SetTextAlign( TA_RIGHT ); int Y=0; float rate = 0;//(1.0*(m_nLowerLimit-m_nLowwer)) / (m_nUpper-m_nLowwer); //Y = inner.bottom - int(inner.Height()*rate); //if (Y > inner.bottom) // Y = inner.bottom; //if (Y < inner.top) // Y = inner.top; //pDC->MoveTo(inner.left, Y); //pDC->LineTo(inner.right, Y); //sText.Format( "%d",m_nLowerLimit ); //pDC->TextOut( 40-5,Y-7,sText ); // //rate = (1.0*(m_nUpperLimit-m_nLowwer)) / (m_nUpper-m_nLowwer); //Y = inner.bottom - int(inner.Height()*rate); //if (Y > inner.bottom) // Y = inner.bottom; //if (Y < inner.top) // Y = inner.top; //pDC->MoveTo(inner.left, Y); //pDC->LineTo(inner.right, Y); //sText.Format( "%d",m_nUpperLimit ); //pDC->TextOut( 40-5,Y-7,sText ); int nSpace = (m_nUpper-m_nLowwer)/10; if( nSpace%10!=0 ) nSpace += ( 10-nSpace%10 ); if( m_nUpper==2 && m_nLowwer==0 ) nSpace = 1; for (int i=m_nLowwer+nSpace; i inner.bottom) Y = inner.bottom; if (Y < inner.top) Y = inner.top; pDC->MoveTo(inner.left, Y); pDC->LineTo(inner.right, Y); sText.Format( "%d",i ); pDC->TextOut( 40-5,Y-7,sText ); } DeleteObject( penGrid ); //for (int i=1; i<6; i++) //{ // pDC->MoveTo(inner.left + inner.Width()*i/6, inner.top); // pDC->LineTo(inner.left + inner.Width()*i/6, inner.bottom); //} // 显示60个点,1s一个点 DrawCurve(pDC, inner, m_nValue1, RGB( 0,0,255 ) ); CPen penFrame(PS_SOLID, 1, RGB(0, 0, 0)); pDC->SelectObject(&penFrame); pDC->MoveTo(inner.left, inner.top); pDC->LineTo(inner.right, inner.top); pDC->LineTo(inner.right, inner.bottom); DeleteObject( penFrame ); CRect leftKedu = inner; leftKedu.right = leftKedu.left + 5; DrawVKedu(pDC, leftKedu, 50, RGB( 0,0,0 ) ); CRect bottomKedu = inner; bottomKedu.top = leftKedu.bottom - 5; DrawHKedu(pDC, bottomKedu, MAX_NUM, RGB( 0,0,0 )); pDC->SetTextColor( RGB(0,0,255) ); pDC->SetTextAlign( TA_CENTER ); pDC->TextOut( rect.Width()/2,rect.bottom-20,m_sCurData ); ReleaseDC( pDC ); // 离开关键代码 LeaveCriticalSection ( & g_cs ) ; } void CDlg_RealTimeCurve::DrawHKedu(CDC* pDC, CRect rect, int nKedu, COLORREF clrKedu) { CPen pen(PS_SOLID,1,clrKedu); CPen *pOldPen = pDC->SelectObject(&pen); pDC->MoveTo(rect.right, rect.bottom); pDC->LineTo(rect.left, rect.bottom); for(int i=0;i<=nKedu;i++) { int nX = rect.left + i*(rect.Width()*1.0/nKedu); pDC->MoveTo(nX, rect.top); pDC->LineTo(nX, rect.bottom); } pDC->SelectObject(pOldPen); DeleteObject( pen ); } void CDlg_RealTimeCurve::DrawVKedu(CDC* pDC, CRect rect, int nKedu, COLORREF clrKedu) { CPen pen(PS_SOLID,1,clrKedu); CPen *pOldPen = pDC->SelectObject(&pen); pDC->MoveTo(rect.TopLeft()); pDC->LineTo(rect.left, rect.bottom); //for(int i=0;i<=nKedu;i++) //{ // int nY = rect.top + i*(rect.Height()*1.0/nKedu); // if (i == nKedu ) // nY = rect.bottom; // pDC->MoveTo(rect.left, nY); // pDC->LineTo(rect.right, nY); //} pDC->SelectObject(pOldPen); DeleteObject( pen ); } void CDlg_RealTimeCurve::DrawCurve(CDC* pDC, CRect rect, int* data, COLORREF color) { rect.NormalizeRect(); CPen PenBackGrid(PS_SOLID, 2, color); CPen* pOldPen = pDC->SelectObject(&PenBackGrid); bool f = false; for (int i=0; i rect.bottom) point.y = rect.bottom; if (point.y < rect.top) point.y = rect.top; if (point.x < rect.left) point.x = rect.left; if (point.x > rect.right) point.x = rect.right; if ( !f && point.y!=rect.bottom ) { f = true; pDC->MoveTo(point); } else if ( f ) { pDC->LineTo(point); } } pDC->SelectObject(pOldPen); DeleteObject( PenBackGrid ); }