Dlg_RealTimeCurve.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Dlg_RealTimeCurve.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Dlg_RealTimeCurve.h"
  5. #include ".\dlg_realtimecurve.h"
  6. // CDlg_RealTimeCurve 对话框
  7. IMPLEMENT_DYNAMIC(CDlg_RealTimeCurve, CDialog)
  8. CDlg_RealTimeCurve::CDlg_RealTimeCurve(CWnd* pParent /*=NULL*/)
  9. : CDialog(CDlg_RealTimeCurve::IDD, pParent)
  10. {
  11. m_nLowwer = 0;
  12. m_nUpper = 200;
  13. m_nLowerLimit = 0;
  14. m_nUpperLimit = 0;
  15. for (int i=0; i<MAX_NUM; i++)
  16. {
  17. m_nValue1[i] = 0;
  18. }
  19. // 关键代码
  20. // 初始化关键代码的 C_S 结构
  21. InitializeCriticalSection ( & g_cs ) ;
  22. }
  23. CDlg_RealTimeCurve::~CDlg_RealTimeCurve()
  24. {
  25. // 释放关键代码
  26. DeleteCriticalSection ( & g_cs ) ;
  27. }
  28. void CDlg_RealTimeCurve::DoDataExchange(CDataExchange* pDX)
  29. {
  30. CDialog::DoDataExchange(pDX);
  31. //DDX_Control(pDX, IDC_MULTICOLORPLOT_CTRL, m_ctrlMultiColorPlot);
  32. }
  33. BEGIN_MESSAGE_MAP(CDlg_RealTimeCurve, CDialog)
  34. ON_WM_TIMER()
  35. ON_WM_PAINT()
  36. END_MESSAGE_MAP()
  37. // CDlg_RealTimeCurve 消息处理程序
  38. BOOL CDlg_RealTimeCurve::OnInitDialog()
  39. {
  40. CDialog::OnInitDialog();
  41. // TODO: 在此添加额外的初始化
  42. //m_ctrlMultiColorPlot.LockRang(000.00f,100.00f);
  43. //m_ctrlMultiColorPlot.SetPlotGranulatrity(4);
  44. //m_ctrlMultiColorPlot.SetGridResolutionX(15);
  45. //m_ctrlMultiColorPlot.ShowTitle(4);
  46. //m_ctrlMultiColorPlot.SetPlotType(LINE);
  47. pVariantsManager->GetAnalogValue(m_sVarName,m_sVarDes);
  48. pVariantsManager->GetAnalogValue(m_sVarName,m_nLowerLimit,m_nUpperLimit);
  49. m_nLowwer = getMin( m_nLowerLimit );
  50. m_nUpper = getMax( m_nUpperLimit );
  51. if( m_nLowerLimit == m_nUpperLimit )
  52. {
  53. m_nLowwer = 0;
  54. m_nUpper = 2;
  55. }
  56. float fValue = pVariantsManager->GetAnalogValue(m_sVarName);
  57. m_nValue1[MAX_NUM-1] = (int)fValue;
  58. m_sCurData.Format( "%s【%0.1f】",m_sVarDes,fValue );
  59. SetTimer( 1,1000,NULL );
  60. return TRUE; // return TRUE unless you set the focus to a control
  61. // 异常: OCX 属性页应返回 FALSE
  62. }
  63. int CDlg_RealTimeCurve::getMin( int iLowwer )
  64. {
  65. int nRet=0;
  66. nRet = iLowwer-20;
  67. if( nRet<0 )
  68. nRet = 0;
  69. int nMod = nRet%10;
  70. if( nMod!=0 )
  71. nRet -= nMod;
  72. return nRet;
  73. }
  74. int CDlg_RealTimeCurve::getMax( int iUpper )
  75. {
  76. int nRet=0;
  77. nRet = iUpper+20;
  78. int nMod = nRet%10;
  79. if( nMod!=0 )
  80. nRet += ( 10-nMod );
  81. return nRet;
  82. }
  83. void CDlg_RealTimeCurve::OnTimer(UINT nIDEvent)
  84. {
  85. // TODO: 在此添加消息处理程序代码和/或调用默认值
  86. // 用关键代码同步
  87. EnterCriticalSection ( & g_cs ) ;
  88. for(int i=0; i<MAX_NUM-1; i++)
  89. {
  90. m_nValue1[i] = m_nValue1[i+1];
  91. }
  92. float fValue = pVariantsManager->GetAnalogValue(m_sVarName);
  93. m_nValue1[MAX_NUM-1] = (int)fValue;
  94. m_sCurData.Format( "%s【%0.1f】",m_sVarDes,fValue );
  95. Invalidate(false);
  96. //Draw();
  97. //m_ctrlMultiColorPlot.SetData( ( float ) m_nValue1[MAX_NUM-1] ) ;
  98. // 离开关键代码
  99. LeaveCriticalSection ( & g_cs ) ;
  100. CDialog::OnTimer(nIDEvent);
  101. }
  102. void CDlg_RealTimeCurve::OnPaint()
  103. {
  104. CPaintDC dc(this); // device context for painting
  105. // TODO: 在此处添加消息处理程序代码
  106. // 不为绘图消息调用 CDialog::OnPaint()
  107. Draw();
  108. }
  109. void CDlg_RealTimeCurve::Draw()
  110. {
  111. // 用关键代码同步
  112. EnterCriticalSection ( & g_cs ) ;
  113. CString sText;
  114. CDC *pDC=this->GetDC();
  115. CRect rect;
  116. this->GetClientRect( rect );
  117. //rect.NormalizeRect();
  118. pDC->SetBkColor( RGB( 64,128,128 ) );
  119. CBrush m_brush;
  120. m_brush.CreateSolidBrush( RGB( 64,128,128 ) );
  121. CBrush* pOldBrush=(CBrush*)pDC->SelectObject(&m_brush);
  122. pDC->Rectangle(rect);
  123. DeleteObject( m_brush );
  124. // 边框
  125. pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT);
  126. CRect inner;
  127. inner.left = rect.left+40;
  128. inner.right = rect.right-10;
  129. inner.top = rect.top+10;
  130. inner.bottom = rect.bottom-30;
  131. // 网格
  132. CPen penGrid(PS_DOT, 1, RGB(192, 192, 192));
  133. CPen* pOldPen = pDC->SelectObject(&penGrid);
  134. pDC->SetTextColor( RGB(0,0,0) );
  135. pDC->SetTextAlign( TA_RIGHT );
  136. int Y=0;
  137. float rate = 0;//(1.0*(m_nLowerLimit-m_nLowwer)) / (m_nUpper-m_nLowwer);
  138. //Y = inner.bottom - int(inner.Height()*rate);
  139. //if (Y > inner.bottom)
  140. // Y = inner.bottom;
  141. //if (Y < inner.top)
  142. // Y = inner.top;
  143. //pDC->MoveTo(inner.left, Y);
  144. //pDC->LineTo(inner.right, Y);
  145. //sText.Format( "%d",m_nLowerLimit );
  146. //pDC->TextOut( 40-5,Y-7,sText );
  147. //
  148. //rate = (1.0*(m_nUpperLimit-m_nLowwer)) / (m_nUpper-m_nLowwer);
  149. //Y = inner.bottom - int(inner.Height()*rate);
  150. //if (Y > inner.bottom)
  151. // Y = inner.bottom;
  152. //if (Y < inner.top)
  153. // Y = inner.top;
  154. //pDC->MoveTo(inner.left, Y);
  155. //pDC->LineTo(inner.right, Y);
  156. //sText.Format( "%d",m_nUpperLimit );
  157. //pDC->TextOut( 40-5,Y-7,sText );
  158. int nSpace = (m_nUpper-m_nLowwer)/10;
  159. if( nSpace%10!=0 )
  160. nSpace += ( 10-nSpace%10 );
  161. if( m_nUpper==2 && m_nLowwer==0 )
  162. nSpace = 1;
  163. for (int i=m_nLowwer+nSpace; i<m_nUpper; i+=nSpace )
  164. {
  165. rate = (1.0*(i-m_nLowwer)) / (m_nUpper-m_nLowwer);
  166. Y = inner.bottom - int(inner.Height()*rate);
  167. if (Y > inner.bottom)
  168. Y = inner.bottom;
  169. if (Y < inner.top)
  170. Y = inner.top;
  171. pDC->MoveTo(inner.left, Y);
  172. pDC->LineTo(inner.right, Y);
  173. sText.Format( "%d",i );
  174. pDC->TextOut( 40-5,Y-7,sText );
  175. }
  176. DeleteObject( penGrid );
  177. //for (int i=1; i<6; i++)
  178. //{
  179. // pDC->MoveTo(inner.left + inner.Width()*i/6, inner.top);
  180. // pDC->LineTo(inner.left + inner.Width()*i/6, inner.bottom);
  181. //}
  182. // 显示60个点,1s一个点
  183. DrawCurve(pDC, inner, m_nValue1, RGB( 0,0,255 ) );
  184. CPen penFrame(PS_SOLID, 1, RGB(0, 0, 0));
  185. pDC->SelectObject(&penFrame);
  186. pDC->MoveTo(inner.left, inner.top);
  187. pDC->LineTo(inner.right, inner.top);
  188. pDC->LineTo(inner.right, inner.bottom);
  189. DeleteObject( penFrame );
  190. CRect leftKedu = inner;
  191. leftKedu.right = leftKedu.left + 5;
  192. DrawVKedu(pDC, leftKedu, 50, RGB( 0,0,0 ) );
  193. CRect bottomKedu = inner;
  194. bottomKedu.top = leftKedu.bottom - 5;
  195. DrawHKedu(pDC, bottomKedu, MAX_NUM, RGB( 0,0,0 ));
  196. pDC->SetTextColor( RGB(0,0,255) );
  197. pDC->SetTextAlign( TA_CENTER );
  198. pDC->TextOut( rect.Width()/2,rect.bottom-20,m_sCurData );
  199. ReleaseDC( pDC );
  200. // 离开关键代码
  201. LeaveCriticalSection ( & g_cs ) ;
  202. }
  203. void CDlg_RealTimeCurve::DrawHKedu(CDC* pDC, CRect rect, int nKedu, COLORREF clrKedu)
  204. {
  205. CPen pen(PS_SOLID,1,clrKedu);
  206. CPen *pOldPen = pDC->SelectObject(&pen);
  207. pDC->MoveTo(rect.right, rect.bottom);
  208. pDC->LineTo(rect.left, rect.bottom);
  209. for(int i=0;i<=nKedu;i++)
  210. {
  211. int nX = rect.left + i*(rect.Width()*1.0/nKedu);
  212. pDC->MoveTo(nX, rect.top);
  213. pDC->LineTo(nX, rect.bottom);
  214. }
  215. pDC->SelectObject(pOldPen);
  216. DeleteObject( pen );
  217. }
  218. void CDlg_RealTimeCurve::DrawVKedu(CDC* pDC, CRect rect, int nKedu, COLORREF clrKedu)
  219. {
  220. CPen pen(PS_SOLID,1,clrKedu);
  221. CPen *pOldPen = pDC->SelectObject(&pen);
  222. pDC->MoveTo(rect.TopLeft());
  223. pDC->LineTo(rect.left, rect.bottom);
  224. //for(int i=0;i<=nKedu;i++)
  225. //{
  226. // int nY = rect.top + i*(rect.Height()*1.0/nKedu);
  227. // if (i == nKedu )
  228. // nY = rect.bottom;
  229. // pDC->MoveTo(rect.left, nY);
  230. // pDC->LineTo(rect.right, nY);
  231. //}
  232. pDC->SelectObject(pOldPen);
  233. DeleteObject( pen );
  234. }
  235. void CDlg_RealTimeCurve::DrawCurve(CDC* pDC, CRect rect, int* data, COLORREF color)
  236. {
  237. rect.NormalizeRect();
  238. CPen PenBackGrid(PS_SOLID, 2, color);
  239. CPen* pOldPen = pDC->SelectObject(&PenBackGrid);
  240. bool f = false;
  241. for (int i=0; i<MAX_NUM; i++)
  242. {
  243. CPoint point;
  244. point.x = rect.left+i*(1.0*rect.Width()/(MAX_NUM-1));
  245. float rate = (1.0*(data[i]-m_nLowwer)) / (m_nUpper-m_nLowwer);
  246. point.y = rect.bottom - int(rect.Height()*rate);
  247. if (point.y > rect.bottom)
  248. point.y = rect.bottom;
  249. if (point.y < rect.top)
  250. point.y = rect.top;
  251. if (point.x < rect.left)
  252. point.x = rect.left;
  253. if (point.x > rect.right)
  254. point.x = rect.right;
  255. if ( !f && point.y!=rect.bottom )
  256. {
  257. f = true;
  258. pDC->MoveTo(point);
  259. }
  260. else if ( f )
  261. {
  262. pDC->LineTo(point);
  263. }
  264. }
  265. pDC->SelectObject(pOldPen);
  266. DeleteObject( PenBackGrid );
  267. }