123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- // 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; i<MAX_NUM; i++)
- {
- m_nValue1[i] = 0;
- }
- // 关键代码
- // 初始化关键代码的 C_S 结构
- InitializeCriticalSection ( & g_cs ) ;
- }
- CDlg_RealTimeCurve::~CDlg_RealTimeCurve()
- {
- // 释放关键代码
- DeleteCriticalSection ( & g_cs ) ;
- }
- void CDlg_RealTimeCurve::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //DDX_Control(pDX, IDC_MULTICOLORPLOT_CTRL, m_ctrlMultiColorPlot);
- }
- BEGIN_MESSAGE_MAP(CDlg_RealTimeCurve, CDialog)
- ON_WM_TIMER()
- ON_WM_PAINT()
- END_MESSAGE_MAP()
- // CDlg_RealTimeCurve 消息处理程序
- BOOL CDlg_RealTimeCurve::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: 在此添加额外的初始化
- //m_ctrlMultiColorPlot.LockRang(000.00f,100.00f);
- //m_ctrlMultiColorPlot.SetPlotGranulatrity(4);
- //m_ctrlMultiColorPlot.SetGridResolutionX(15);
- //m_ctrlMultiColorPlot.ShowTitle(4);
- //m_ctrlMultiColorPlot.SetPlotType(LINE);
- m_nLowwer = getMin( m_nLowerLimit );
- m_nUpper = getMax( m_nUpperLimit );
- if( m_nLowerLimit == m_nUpperLimit )
- {
- m_nLowwer = 0;
- m_nUpper = 2;
- }
- float fValue = 0.0;//pVariantsManager->GetAnalogValue(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; i<MAX_NUM-1; i++)
- {
- m_nValue1[i] = m_nValue1[i+1];
- }
-
- float fValue = 0.0;// pVariantsManager->GetAnalogValue(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<m_nUpper; i+=nSpace )
- {
- rate = (1.0*(i-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",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<MAX_NUM; i++)
- {
- CPoint point;
- point.x = rect.left+i*(1.0*rect.Width()/(MAX_NUM-1));
- float rate = (1.0*(data[i]-m_nLowwer)) / (m_nUpper-m_nLowwer);
- point.y = rect.bottom - int(rect.Height()*rate);
- if (point.y > 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 );
- }
|