demoView.cpp
上传用户:apnc006
上传日期:2013-01-26
资源大小:178k
文件大小:5k
源码类别:

绘图程序

开发平台:

Visual C++

  1. //*******************************************************************************************************/
  2. //* FileName: demoView.cpp
  3. //*
  4. //* Contents: demo implementation of clPlot.
  5. //*
  6. //* Note: Most of the stuff in this file is standard MFC implementation stuff not related to 
  7. //* the plot.
  8. //*
  9. //* The implementation of the plot in the demo are done as following:
  10. //*
  11. //* 1. OnInitialUpdate creates the plot and initialize it. It also activates a timer.
  12. //*
  13. //* 2. OnTimer adds data points to the plot every second.
  14. //*
  15. //* 3. OnSize re-sizes the plot to fit the entire view as needed.
  16. //*
  17. //* 4. OnEraseBkgnd is overrided to do nothing. This is needed for flicker free drawing.
  18. //*
  19. //* Author: Jan Vidar Berger
  20. //*******************************************************************************************************/
  21. #include "stdafx.h"
  22. #include "Demo.h"
  23. #include "DemoDoc.h"
  24. #include "DemoView.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CDemoView
  32. IMPLEMENT_DYNCREATE(CDemoView, CView)
  33. BEGIN_MESSAGE_MAP(CDemoView, CView)
  34. //{{AFX_MSG_MAP(CDemoView)
  35. ON_WM_TIMER()
  36. ON_WM_SIZE()
  37. ON_WM_ERASEBKGND()
  38. //}}AFX_MSG_MAP
  39. // Standard printing commands
  40. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  41. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  42. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CDemoView construction/destruction
  46. CDemoView::CDemoView()
  47. {
  48. // TODO: add construction code here
  49. canSize=FALSE;
  50. }
  51. CDemoView::~CDemoView()
  52. {
  53. }
  54. BOOL CDemoView::PreCreateWindow(CREATESTRUCT& cs)
  55. {
  56. // TODO: Modify the Window class or styles here by modifying
  57. //  the CREATESTRUCT cs
  58. return CView::PreCreateWindow(cs);
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CDemoView drawing
  62. void CDemoView::OnDraw(CDC* pDC)
  63. {
  64. CDemoDoc* pDoc = GetDocument();
  65. ASSERT_VALID(pDoc);
  66. // TODO: add draw code for native data here
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CDemoView printing
  70. BOOL CDemoView::OnPreparePrinting(CPrintInfo* pInfo)
  71. {
  72. // default preparation
  73. return DoPreparePrinting(pInfo);
  74. }
  75. void CDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  76. {
  77. // TODO: add extra initialization before printing
  78. }
  79. void CDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81. // TODO: add cleanup after printing
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CDemoView diagnostics
  85. #ifdef _DEBUG
  86. void CDemoView::AssertValid() const
  87. {
  88. CView::AssertValid();
  89. }
  90. void CDemoView::Dump(CDumpContext& dc) const
  91. {
  92. CView::Dump(dc);
  93. }
  94. CDemoDoc* CDemoView::GetDocument() // non-debug version is inline
  95. {
  96. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDemoDoc)));
  97. return (CDemoDoc*)m_pDocument;
  98. }
  99. #endif //_DEBUG
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CDemoView message handlers
  102. void CDemoView::OnInitialUpdate() 
  103. {
  104. CView::OnInitialUpdate();
  105. CRect Rect;
  106. GetClientRect(Rect);
  107. m_Plot.Create(WS_CHILD|WS_VISIBLE,Rect,this,12000);
  108. m_Plot.SetSerie(0, PS_SOLID, RGB(255,0,0), 0.0, 2000.0, "Pressure");
  109. m_Plot.SetSerie(1, PS_DOT, RGB(0,255,0), 0.0, 2000.0, "Pressure");
  110. m_Plot.SetSerie(2, PS_DASH, RGB(0,0,255), 0.0, 2000.0, "Pressure");
  111. m_Plot.SetSerie(3, PS_SOLID, RGB(255,255,0), 0.0, 2000.0, "Pressure");
  112. m_Plot.SetLegend(0, PS_SOLID, RGB(255,0,0), "Temperature");
  113. m_Plot.SetLegend(1, PS_DOT, RGB(0,255,0), "Pressure");
  114. m_Plot.SetLegend(2, PS_DASH, RGB(0,0,255), "Pressure");
  115. m_Plot.SetLegend(3, PS_SOLID, RGB(255,255,0), "Pressure");
  116. m_Plot.m_bAutoScrollX=TRUE;
  117. SetTimer(1,1000,NULL);
  118. canSize=TRUE;
  119. }
  120. void CDemoView::OnTimer(UINT nIDEvent) 
  121. {
  122. static BOOL pros={FALSE};
  123. if(!pros){
  124. pros=TRUE;
  125. {
  126. double y =(double)(abs(rand())%2000);
  127. m_Plot.AddPoint(0,  CTime::GetCurrentTime(), y);
  128. }
  129. {
  130. double y =(double)(abs(rand())%2000);
  131. m_Plot.AddPoint(1,  CTime::GetCurrentTime(), y);
  132. }
  133. {
  134. double y =(double)(abs(rand())%2000);
  135. m_Plot.AddPoint(2,  CTime::GetCurrentTime(), y);
  136. }
  137. {
  138. //double y =(double)(abs(rand())%2000);
  139. double y=500.0;
  140. m_Plot.AddPoint(3,  CTime::GetCurrentTime(), y);
  141. }
  142. Invalidate();
  143. pros=FALSE;
  144. }
  145. CView::OnTimer(nIDEvent);
  146. }
  147. void CDemoView::OnSize(UINT nType, int cx, int cy) 
  148. {
  149. CView::OnSize(nType, cx, cy);
  150. if(canSize){
  151. CRect Rect;
  152. GetClientRect(Rect);
  153. m_Plot.MoveWindow(Rect);
  154. }
  155. }
  156. BOOL CDemoView::OnEraseBkgnd(CDC* pDC) 
  157. {
  158. // TODO: Add your message handler code here and/or call default
  159. return FALSE;
  160. }