RTDemoView.cpp
上传用户:mgf822
上传日期:2013-10-03
资源大小:133k
文件大小:11k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // RTDemoView.cpp : implementation of the CRTDemoView class
  2. //
  3. #include "stdafx.h"
  4. #include "RTDemo.h"
  5. #include "RTDemoDoc.h"
  6. #include "RTDemoView.h"
  7. #include "RTForm.h"
  8. #include "TimerSpeed.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define SliderTickID 12001
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CRTDemoView
  17. IMPLEMENT_DYNCREATE(CRTDemoView, CView)
  18. BEGIN_MESSAGE_MAP(CRTDemoView, CView)
  19. ON_WM_CONTEXTMENU()
  20. //{{AFX_MSG_MAP(CRTDemoView)
  21. ON_WM_ERASEBKGND()
  22. ON_WM_TIMER()
  23. ON_WM_DESTROY()
  24. ON_WM_SIZE()
  25. ON_COMMAND(ID_SPEED_TIMERSPEED, OnSpeedTimerspeed)
  26. ON_UPDATE_COMMAND_UI(ID_SPEED_TIMERSPEED, OnUpdateSpeedTimerspeed)
  27. //}}AFX_MSG_MAP
  28. // Standard printing commands
  29. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  30. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CRTDemoView construction/destruction
  35. CRTDemoView::CRTDemoView()
  36. {
  37. SpeedLevel  = 6;
  38. MillSeconds  = m_Graph.m_SpeedLevel[SpeedLevel];
  39. CyclesPerSec = 1000.0 /MillSeconds;
  40. m_bRealTime  = FALSE;
  41. if (!m_bRealTime) 
  42. CyclesPerSec = 1;
  43. m_nStartTime = 0;
  44. mSec  = m_nStartTime;
  45. m_nTimeSpan  = 30;
  46. m_sTicks  = (int)(0.5 + m_nTimeSpan * CyclesPerSec);
  47. MaxSeconds   = 120;
  48. CanTicks     = TRUE;
  49. m_bCanSize   = FALSE;
  50. m_bFlag  = TRUE;
  51. m_nTicks  = 0;
  52. Rn.init_random();
  53. m_Graph.SetXNumOfTicks(60);
  54. }
  55. CRTDemoView::~CRTDemoView()
  56. {
  57. }
  58. BOOL CRTDemoView::PreCreateWindow(CREATESTRUCT& cs)
  59. {
  60. // TODO: Modify the Window class or styles here by modifying
  61. //  the CREATESTRUCT cs
  62. return CView::PreCreateWindow(cs);
  63. }
  64. void CRTDemoView::DrawFrame(CRect& rect, COLORREF cr, const char* Title)
  65. {
  66. CRTDemoDoc* pDoc = GetDocument();
  67. m_Graph.RecalcRects(rect);
  68. if (pDoc->m_bBoundary)
  69. m_Graph.DrawBoundary(cr, 2);
  70. if (pDoc->m_bTitle)
  71. {
  72. m_Graph.XAxisTitle("Time");
  73. m_Graph.YAxisTitle("Put Y Axis string here");
  74. //m_Graph.Title(Title);
  75. }
  76. m_Graph.Axes();
  77. if (pDoc->m_bGrid)
  78. m_Graph.Grid();
  79. }
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CRTDemoView drawing
  82. void CRTDemoView::OnDraw(CDC* pDC)
  83. {
  84. CRTDemoDoc* pDoc = GetDocument();
  85. ASSERT_VALID(pDoc);
  86. if (m_Graph.m_bSetingFailed)
  87. return;
  88. CRect rect;
  89. if (pDC->IsPrinting())
  90. {
  91. rect.left = rect.top = 0;
  92. rect.right  = pDC->GetDeviceCaps(HORZRES);
  93. rect.bottom = pDC->GetDeviceCaps(VERTRES);
  94. m_Graph.SetPrintScale(pDC->m_hDC, rect);
  95. }
  96. else
  97. GetClientRect(rect);
  98. m_Graph.BeginDraw(pDC->m_hDC);
  99. DrawFrame(rect, RGB(255, 0, 0), "");
  100. m_Graph.DrawRealTimeLines();
  101. m_Graph.EndDraw(pDC->m_hDC);
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CRTDemoView printing
  105. BOOL CRTDemoView::OnPreparePrinting(CPrintInfo* pInfo)
  106. {
  107. // default preparation
  108. return DoPreparePrinting(pInfo);
  109. }
  110. void CRTDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  111. {
  112. // TODO: add extra initialization before printing
  113. }
  114. void CRTDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  115. {
  116. // TODO: add cleanup after printing
  117. }
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CRTDemoView diagnostics
  120. #ifdef _DEBUG
  121. void CRTDemoView::AssertValid() const
  122. {
  123. CView::AssertValid();
  124. }
  125. void CRTDemoView::Dump(CDumpContext& dc) const
  126. {
  127. CView::Dump(dc);
  128. }
  129. CRTDemoDoc* CRTDemoView::GetDocument() // non-debug version is inline
  130. {
  131. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRTDemoDoc)));
  132. return (CRTDemoDoc*)m_pDocument;
  133. }
  134. #endif //_DEBUG
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CRTDemoView message handlers
  137. void CRTDemoView::OnInitialUpdate() 
  138. {
  139. CView::OnInitialUpdate();
  140. CRTDemoDoc* pDoc = GetDocument();
  141. pDoc->pView = this;
  142. if (!m_Graph.SetRange(0, 0, 100, 100))
  143. {
  144. m_Graph.m_bSetingFailed = true;
  145. MessageBox("Setting Range failed");
  146. return;
  147. }
  148. m_Graph.EnableMemoryDraw(pDoc->m_bMemDraw);
  149. m_Graph.SetBackColor(pDoc->m_nBkColor);
  150. if (!m_Graph.InitialSetting(CyclesPerSec,  m_nStartTime, m_nTimeSpan, MaxSeconds, 6))
  151. {
  152. m_Graph.m_bSetingFailed = true;
  153. return;
  154. }
  155. m_Graph.m_bSetingFailed = false;
  156. RECT rect;
  157. GetClientRect(&rect);
  158. m_Graph.RecalcRects(rect);
  159. m_Graph.AddALine(RGB(255,   0,   0), 55, 95, "芳  烃","重油催化汽油芳烃","Mod%");
  160. m_Graph.AddALine(RGB(  0, 255,   0), 55, 95, "烯  烃","重油催化汽油烯烃","Mod%");
  161. m_Graph.AddALine(RGB(  0,   0, 255), 5, 45, "辛烷值","重油汽油辛 烷 值","C");
  162. // m_Graph.AddALine(RGB(255,   0, 255), 40, 50, "Name 4");
  163. // m_Graph.AddALine(RGB(255, 255,   0), 50, 60, "Name 5");
  164. // m_Graph.AddALine(RGB(  0, 255, 255), 60, 70, "Name 6");
  165. // m_Graph.AddALine(RGB(128, 128, 128), 70, 80, "Name 7");
  166. // m_Graph.AddALine(RGB(  0,   0, 128), 80, 90, "Name 8");
  167. m_bCanSize = TRUE;
  168. m_sSize    = 17;
  169. m_SliderTick.Create(WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS, 
  170. m_Graph.TickBarSize(), this, SliderTickID);
  171. m_SliderTick.SetRange(0, m_sTicks, TRUE);
  172. m_SliderTick.SetPageSize(1);
  173. m_SliderTick.SetPos(0);
  174. m_SliderTick.EnableWindow(FALSE);
  175. SetTimer(1, MillSeconds, NULL);
  176. m_Graph.m_nSysJG=MillSeconds;
  177. m_bTimerOn = TRUE;
  178. }
  179. BOOL CRTDemoView::OnEraseBkgnd(CDC* pDC) 
  180. {
  181. if (!m_Graph.m_bMemoryDraw || m_Graph.m_bSetingFailed)
  182. return CView::OnEraseBkgnd(pDC) ;
  183. return TRUE;
  184. }
  185. void CRTDemoView::SetData(int i)
  186. {
  187. //"Rn.randm()" is much faster than "1.0 * rand() / RAND_MAX";
  188. m_dY = m_Graph.m_LineArray[i].m_dScaleLow + 
  189.   (m_Graph.m_LineArray[i].m_dScaleHigh - 
  190.    m_Graph.m_LineArray[i].m_dScaleLow) * Rn.randm();
  191. m_Graph.AddYValue(i, m_dY);
  192. }
  193. void CRTDemoView::TicksRanges()
  194. {
  195. if (CanTicks && m_Graph.m_nTimes / (m_nTimeSpan + 1) > m_nTicks && 
  196. MaxSeconds >= m_Graph.m_nTimes + 1)
  197. {
  198. m_nTicks = m_Graph.m_nTimes / m_nTimeSpan;
  199. if (m_nTicks >= m_Graph.m_nMaxPages) 
  200. CanTicks = FALSE;
  201. else
  202. {
  203. ResetSlider(m_nTicks + 1);
  204. m_Graph.m_nPage = m_nTicks + 1;
  205. }
  206. }
  207. }
  208. void CRTDemoView::OnTimer(UINT nIDEvent) 
  209. {
  210. n = m_Graph.m_LineArray.GetSize();
  211. for(i=0; i<n; i++)
  212. SetData(i);
  213. m_Graph.UpdateTimeRange(mSec);
  214. mSec += (UINT)(0.5 + 1000 / CyclesPerSec);
  215. GetLocalTime(&lpSystemTime);
  216. DWORD mSecc=(lpSystemTime.wHour*3600+lpSystemTime.wMinute*60+lpSystemTime.wSecond)*1000+lpSystemTime.wMilliseconds;
  217. m_Graph.m_nSysTime=mSecc;
  218. m_Graph.SetPrintTime(mSec - m_nStartTime, m_Graph.MaxTime); // Sim time : max time
  219. n = m_Graph.GetCursorTimeAndIndex(CurrentTime);
  220. // m_Graph.SetPrintTime(CurrentTime - m_nStartTime, m_Graph.CursorTime); // cursor time
  221. m_Graph.SetPrintTime(mSecc, m_Graph.CursorTime); // cursor time
  222. GetYValues(n);
  223. TicksRanges();
  224. if (m_bFlag)
  225. {
  226. if (m_Graph.m_bIsForwardDrawing)
  227. {
  228. if (m_Graph.m_nTick < m_Graph.m_nSTicks)
  229. {
  230. m_SliderTick.SetPos(m_Graph.m_nTick);
  231. m_Graph.m_nTick ++;
  232. }
  233. else
  234. {
  235. m_SliderTick.SetPos(m_Graph.m_nTick);
  236. m_SliderTick.EnableWindow(TRUE);
  237. m_bFlag = FALSE;
  238. }
  239. }
  240. else
  241. {
  242. m_SliderTick.SetPos(m_Graph.m_nSTicks);
  243. m_Graph.m_nTick ++;
  244. if (m_Graph.m_nTick == m_Graph.m_nSTicks)
  245. {
  246. m_SliderTick.EnableWindow(TRUE);
  247. m_bFlag = FALSE;
  248. }
  249. }
  250. }
  251. m_Graph.Redraw(GetSafeHwnd());
  252. CView::OnTimer(nIDEvent);
  253. }
  254. void CRTDemoView::OnDestroy() 
  255. {
  256. if (!m_Graph.m_bSetingFailed)
  257. KillTimer(1);
  258. CView::OnDestroy();
  259. }
  260. void CRTDemoView::GetYValues(int Index)
  261. {
  262. m = m_Graph.m_LineArray.GetSize();
  263. CRTForm* pForm = (CRTForm*)GetRTForm();
  264. for(i = 0; i < m; i ++)
  265. {
  266. m_dValue = m_Graph.m_LineArray[i].m_pValues[Index].YValue;
  267. m_dValue = m_Graph.m_LineArray[i].m_dMin + (m_dValue - m_Graph.m_LineArray[i].m_dScaleLow) *
  268.    (m_Graph.m_LineArray[i].m_dMax - m_Graph.m_LineArray[i].m_dMin) / 
  269.    (m_Graph.m_LineArray[i].m_dScaleHigh - m_Graph.m_LineArray[i].m_dScaleLow);
  270. m_sString.Format("%.4f", m_dValue);
  271. pForm->m_RTGrid.SetTextMatrix(i + 1, 5, m_sString);
  272. }
  273. }
  274. void CRTDemoView::RefreshGraph()
  275. {
  276. m = m_Graph.GetCursorTimeAndIndex(CurrentTime);
  277. GetYValues(m);
  278. m_Graph.Redraw(GetSafeHwnd());
  279. }
  280. void CRTDemoView::OnSize(UINT nType, int cx, int cy) 
  281. {
  282. CView::OnSize(nType, cx, cy);
  283. if(m_bCanSize)
  284. {
  285. RECT rect;
  286. GetClientRect(&rect);
  287. m_Graph.RecalcRects(rect);
  288. m_SliderTick.MoveWindow(&m_Graph.TickBarSize());
  289. }
  290. }
  291. RECT& CRTDemoView::GetGraphRect()
  292. {
  293. if (::IsWindow(m_hWnd))
  294. return m_Graph.m_PlotRect;
  295. else
  296. {
  297. static RECT rt;
  298. rt.left   = 0;
  299. rt.top    = 0;
  300. rt.right  = 0;
  301. rt.bottom = 0;
  302. return rt;
  303. }
  304. }
  305. CWnd* CRTDemoView::GetRTForm()
  306. {
  307. CSplitterWnd* pWnd = (CSplitterWnd*)GetParent();
  308. return pWnd->GetPane(1, 0);
  309. }
  310. void CRTDemoView::ResetSlider(int Ticks)
  311. {
  312. CRTForm* pForm = (CRTForm*)GetRTForm();
  313. pForm->InitialSlider(0, Ticks - 1, 1, Ticks);
  314. }
  315. void CRTDemoView::RTKillTimer()
  316. {
  317. KillTimer(1);
  318. m_bTimerOn = FALSE;
  319. }
  320. void CRTDemoView::RTRestoreTimer()
  321. {
  322. SetTimer(1, MillSeconds, NULL);
  323. m_Graph.m_nSysJG=MillSeconds;
  324. m_bTimerOn = TRUE;
  325. }
  326. void CRTDemoView::RemoveALine(int Index)
  327. {
  328. if (!m_Graph.RemoveALine(Index))
  329. return;
  330. CRTForm* pForm = (CRTForm*)GetRTForm();
  331. pForm->RedrawGrid();
  332. }
  333. BOOL CRTDemoView::InsertALine(int index, COLORREF color, double low, double high, 
  334.   const char* name, const char* desc, const char* unit, 
  335.   double min, double max, int style, int width)
  336. {
  337. if (m_Graph.InsertALine(index, color, low, high, name, desc, 
  338. unit, min, max, style, width))
  339. {
  340. CRTForm* pForm = (CRTForm*)GetRTForm();
  341. pForm->RedrawGrid();
  342. return TRUE;
  343. }
  344. else
  345. return FALSE;
  346. }
  347. void CRTDemoView::SaveAModifiedLine()
  348. {
  349. RefreshGraph();
  350. CRTForm* pForm = (CRTForm*)GetRTForm();
  351. pForm->RedrawGrid();
  352. }
  353. void CRTDemoView::GetColorAndName(int index, COLORREF& cr, CString& name)
  354. {
  355. cr   = m_Graph.m_LineArray[index].m_nColor;
  356. name = m_Graph.m_LineArray[index].m_sName.GetChar();
  357. }
  358. void CRTDemoView::OnContextMenu(CWnd*, CPoint point)
  359. {
  360. // CG: This block was added by the Pop-up Menu component { if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(CG_IDR_POPUP_RTDEMO_VIEW)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
  361. }
  362. void CRTDemoView::OnSpeedTimerspeed() 
  363. {
  364. CTimerSpeed TSpeed(this);
  365. TSpeed.m_nTimerSpeed = SpeedLevel;
  366. if (TSpeed.DoModal() == IDOK)
  367. {
  368. if (SpeedLevel != TSpeed.m_nTimerSpeed)
  369. {
  370. RTKillTimer();
  371. SpeedLevel  = TSpeed.m_nTimerSpeed;
  372. MillSeconds = m_Graph.m_SpeedLevel[SpeedLevel];
  373. RTRestoreTimer();
  374. }
  375. }
  376. }
  377. void CRTDemoView::OnUpdateSpeedTimerspeed(CCmdUI* pCmdUI) 
  378. {
  379. pCmdUI->Enable(!m_bRealTime);
  380. }
  381. void CRTDemoView::Redraw()
  382. {
  383. RECT rect;
  384. GetClientRect(&rect);
  385. m_Graph.RecalcRects(rect);
  386. Invalidate();
  387. }