Dlg_RealTimeCurve.cpp 7.2 KB

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