RTDemoView.cpp
上传用户:oy0519
上传日期:2008-01-20
资源大小:124k
文件大小: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. CyclesPerSec = 1000.0 / m_Graph.m_SpeedLevel[SpeedLevel];
  39. MillSeconds  = m_Graph.m_SpeedLevel[SpeedLevel];
  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), 10, 20, "Name 1");
  160. m_Graph.AddALine(RGB(  0, 255,   0), 20, 30, "Name 2");
  161. m_Graph.AddALine(RGB(  0,   0, 255), 30, 40, "Name 3");
  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_bTimerOn = TRUE;
  177. }
  178. BOOL CRTDemoView::OnEraseBkgnd(CDC* pDC) 
  179. {
  180. if (!m_Graph.m_bMemoryDraw || m_Graph.m_bSetingFailed)
  181. return CView::OnEraseBkgnd(pDC) ;
  182. return TRUE;
  183. }
  184. void CRTDemoView::SetData(int i)
  185. {
  186. //"Rn.randm()" is much faster than "1.0 * rand() / RAND_MAX";
  187. m_dY = m_Graph.m_LineArray[i].m_dScaleLow + 
  188.   (m_Graph.m_LineArray[i].m_dScaleHigh - 
  189.    m_Graph.m_LineArray[i].m_dScaleLow) * Rn.randm();
  190. m_Graph.AddYValue(i, m_dY);
  191. }
  192. void CRTDemoView::TicksRanges()
  193. {
  194. if (CanTicks && m_Graph.m_nTimes / (m_nTimeSpan + 1) > m_nTicks && 
  195. MaxSeconds >= m_Graph.m_nTimes + 1)
  196. {
  197. m_nTicks = m_Graph.m_nTimes / m_nTimeSpan;
  198. if (m_nTicks >= m_Graph.m_nMaxPages) 
  199. CanTicks = FALSE;
  200. else
  201. {
  202. ResetSlider(m_nTicks + 1);
  203. m_Graph.m_nPage = m_nTicks + 1;
  204. }
  205. }
  206. }
  207. void CRTDemoView::OnTimer(UINT nIDEvent) 
  208. {
  209. n = m_Graph.m_LineArray.GetSize();
  210. for(i=0; i<n; i++)
  211. SetData(i);
  212. m_Graph.UpdateTimeRange(mSec);
  213. mSec += (UINT)(0.5 + 1000 / CyclesPerSec);
  214. m_Graph.SetPrintTime(mSec - m_nStartTime, m_Graph.MaxTime); // Sim time : max time
  215. n = m_Graph.GetCursorTimeAndIndex(CurrentTime);
  216. m_Graph.SetPrintTime(CurrentTime - m_nStartTime, m_Graph.CursorTime); // cursor time
  217. GetYValues(n);
  218. TicksRanges();
  219. if (m_bFlag)
  220. {
  221. if (m_Graph.m_bIsForwardDrawing)
  222. {
  223. if (m_Graph.m_nTick < m_Graph.m_nSTicks)
  224. {
  225. m_SliderTick.SetPos(m_Graph.m_nTick);
  226. m_Graph.m_nTick ++;
  227. }
  228. else
  229. {
  230. m_SliderTick.SetPos(m_Graph.m_nTick);
  231. m_SliderTick.EnableWindow(TRUE);
  232. m_bFlag = FALSE;
  233. }
  234. }
  235. else
  236. {
  237. m_SliderTick.SetPos(m_Graph.m_nSTicks);
  238. m_Graph.m_nTick ++;
  239. if (m_Graph.m_nTick == m_Graph.m_nSTicks)
  240. {
  241. m_SliderTick.EnableWindow(TRUE);
  242. m_bFlag = FALSE;
  243. }
  244. }
  245. }
  246. m_Graph.Redraw(GetSafeHwnd());
  247. CView::OnTimer(nIDEvent);
  248. }
  249. void CRTDemoView::OnDestroy() 
  250. {
  251. if (!m_Graph.m_bSetingFailed)
  252. KillTimer(1);
  253. CView::OnDestroy();
  254. }
  255. void CRTDemoView::GetYValues(int Index)
  256. {
  257. m = m_Graph.m_LineArray.GetSize();
  258. CRTForm* pForm = (CRTForm*)GetRTForm();
  259. for(i = 0; i < m; i ++)
  260. {
  261. m_dValue = m_Graph.m_LineArray[i].m_pValues[Index].YValue;
  262. m_dValue = m_Graph.m_LineArray[i].m_dMin + (m_dValue - m_Graph.m_LineArray[i].m_dScaleLow) *
  263.    (m_Graph.m_LineArray[i].m_dMax - m_Graph.m_LineArray[i].m_dMin) / 
  264.    (m_Graph.m_LineArray[i].m_dScaleHigh - m_Graph.m_LineArray[i].m_dScaleLow);
  265. m_sString.Format("%.4f", m_dValue);
  266. pForm->m_RTGrid.SetTextMatrix(i + 1, 5, m_sString);
  267. }
  268. }
  269. void CRTDemoView::RefreshGraph()
  270. {
  271. m = m_Graph.GetCursorTimeAndIndex(CurrentTime);
  272. GetYValues(m);
  273. m_Graph.Redraw(GetSafeHwnd());
  274. }
  275. void CRTDemoView::OnSize(UINT nType, int cx, int cy) 
  276. {
  277. CView::OnSize(nType, cx, cy);
  278. if(m_bCanSize)
  279. {
  280. RECT rect;
  281. GetClientRect(&rect);
  282. m_Graph.RecalcRects(rect);
  283. m_SliderTick.MoveWindow(&m_Graph.TickBarSize());
  284. }
  285. }
  286. RECT& CRTDemoView::GetGraphRect()
  287. {
  288. if (::IsWindow(m_hWnd))
  289. return m_Graph.m_PlotRect;
  290. else
  291. {
  292. static RECT rt;
  293. rt.left   = 0;
  294. rt.top    = 0;
  295. rt.right  = 0;
  296. rt.bottom = 0;
  297. return rt;
  298. }
  299. }
  300. CWnd* CRTDemoView::GetRTForm()
  301. {
  302. CSplitterWnd* pWnd = (CSplitterWnd*)GetParent();
  303. return pWnd->GetPane(1, 0);
  304. }
  305. void CRTDemoView::ResetSlider(int Ticks)
  306. {
  307. CRTForm* pForm = (CRTForm*)GetRTForm();
  308. pForm->InitialSlider(0, Ticks - 1, 1, Ticks);
  309. }
  310. void CRTDemoView::RTKillTimer()
  311. {
  312. KillTimer(1);
  313. m_bTimerOn = FALSE;
  314. }
  315. void CRTDemoView::RTRestoreTimer()
  316. {
  317. SetTimer(1, MillSeconds, NULL);
  318. m_bTimerOn = TRUE;
  319. }
  320. void CRTDemoView::RemoveALine(int Index)
  321. {
  322. if (!m_Graph.RemoveALine(Index))
  323. return;
  324. CRTForm* pForm = (CRTForm*)GetRTForm();
  325. pForm->RedrawGrid();
  326. }
  327. BOOL CRTDemoView::InsertALine(int index, COLORREF color, double low, double high, 
  328.   const char* name, const char* desc, const char* unit, 
  329.   double min, double max, int style, int width)
  330. {
  331. if (m_Graph.InsertALine(index, color, low, high, name, desc, 
  332. unit, min, max, style, width))
  333. {
  334. CRTForm* pForm = (CRTForm*)GetRTForm();
  335. pForm->RedrawGrid();
  336. return TRUE;
  337. }
  338. else
  339. return FALSE;
  340. }
  341. void CRTDemoView::SaveAModifiedLine()
  342. {
  343. RefreshGraph();
  344. CRTForm* pForm = (CRTForm*)GetRTForm();
  345. pForm->RedrawGrid();
  346. }
  347. void CRTDemoView::GetColorAndName(int index, COLORREF& cr, CString& name)
  348. {
  349. cr   = m_Graph.m_LineArray[index].m_nColor;
  350. name = m_Graph.m_LineArray[index].m_sName.GetChar();
  351. }
  352. void CRTDemoView::OnContextMenu(CWnd*, CPoint point)
  353. {
  354. // 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); }
  355. }
  356. void CRTDemoView::OnSpeedTimerspeed() 
  357. {
  358. CTimerSpeed TSpeed(this);
  359. TSpeed.m_nTimerSpeed = SpeedLevel;
  360. if (TSpeed.DoModal() == IDOK)
  361. {
  362. if (SpeedLevel != TSpeed.m_nTimerSpeed)
  363. {
  364. RTKillTimer();
  365. SpeedLevel  = TSpeed.m_nTimerSpeed;
  366. MillSeconds = m_Graph.m_SpeedLevel[SpeedLevel];
  367. RTRestoreTimer();
  368. }
  369. }
  370. }
  371. void CRTDemoView::OnUpdateSpeedTimerspeed(CCmdUI* pCmdUI) 
  372. {
  373. pCmdUI->Enable(!m_bRealTime);
  374. }
  375. void CRTDemoView::Redraw()
  376. {
  377. RECT rect;
  378. GetClientRect(&rect);
  379. m_Graph.RecalcRects(rect);
  380. Invalidate();
  381. }