home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 93win / data1.cab / DLL_Toolkit / Source / HTBLineChart / LineChartCtrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-02  |  5.1 KB  |  208 lines

  1. // LineChartCtrl.cpp : implementation file
  2. //
  3. // Written by Yuheng Zhao (yuheng@ministars.com) 
  4. // http://www.ministars.com
  5. // The original idea and part of the code from Ken C. Len's CHistogramCtrl
  6. // http://www.codeguru.com/controls/histogram_control.shtml
  7. //
  8. // Copyright (c) 1998.
  9. //
  10. // This code may be used in compiled form in any way you desire. This
  11. // file may be redistributed unmodified by any means PROVIDING it is 
  12. // not sold for profit without the authors written consent, and 
  13. // providing that this notice and the authors name is included. If 
  14. // the source code in  this file is used in any commercial application 
  15. // then a simple email would be nice.
  16. //
  17. // This file is provided "as is" with no expressed or implied warranty.
  18. // The author accepts no liability if it causes any damage whatsoever.
  19. // It's free - so you get what you pay for.
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "LineChartCtrl.h"
  24.  
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CLineChartCtrl
  33.  
  34. CLineChartCtrl::CLineChartCtrl()
  35. {
  36.     CLineChartCtrl::RegisterWndClass(AfxGetInstanceHandle());
  37. }
  38.  
  39. CLineChartCtrl::~CLineChartCtrl()
  40. {
  41.     int nCount = m_items.GetSize();
  42.     
  43.     for (int i = 0; i < nCount; i++)
  44.         delete m_items.GetAt(i);
  45.     m_items.RemoveAll();
  46. }
  47.  
  48.  
  49. BEGIN_MESSAGE_MAP(CLineChartCtrl, CWnd)
  50.     //{{AFX_MSG_MAP(CLineChartCtrl)
  51.     ON_WM_PAINT()
  52.     //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54.  
  55. BOOL CLineChartCtrl::RegisterWndClass(HINSTANCE hInstance)
  56. {
  57.     WNDCLASS wc;
  58.     wc.lpszClassName = "LINE_CHART_CTRL"; // matches class name in client
  59.     wc.hInstance = hInstance;
  60.     wc.lpfnWndProc = ::DefWindowProc;
  61.     wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  62.     wc.hIcon = 0;
  63.     wc.lpszMenuName = NULL;
  64.     wc.hbrBackground = (HBRUSH) ::GetStockObject(LTGRAY_BRUSH);
  65.     wc.style = CS_GLOBALCLASS; // To be modified
  66.     wc.cbClsExtra = 0;
  67.     wc.cbWndExtra = 0;
  68.  
  69.     return (::RegisterClass(&wc) != 0);
  70. }
  71.  
  72. void CLineChartCtrl::InvalidateCtrl()
  73. {
  74.     CClientDC dc(this);
  75.     CRect rcClient;
  76.     GetClientRect(rcClient);
  77.  
  78.     if (m_MemDC.GetSafeHdc() == NULL)
  79.     {
  80.         m_MemDC.CreateCompatibleDC(&dc);
  81.         m_Bitmap.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());
  82.         m_MemDC.SelectObject(m_Bitmap);
  83.         
  84.         // draw scale
  85.         m_MemDC.SetBkColor(RGB(0,0,0));
  86.         CBrush bkBrush(HS_CROSS,RGB(0,128,0));
  87.         m_MemDC.FillRect(rcClient,&bkBrush);
  88.     }
  89.  
  90.     InvalidateRect(rcClient, FALSE);
  91. }
  92.  
  93. UINT CLineChartCtrl::SetPos(int nIndex, UINT nPos)
  94. {
  95.     if (nIndex >= m_items.GetSize())
  96.         return 0;
  97.  
  98.     CLineChartItem* pItem = m_items.GetAt(nIndex);
  99.  
  100.     if (nPos > pItem->m_nUpper)
  101.         nPos = pItem->m_nUpper;
  102.     if (nPos < pItem->m_nLower)
  103.         nPos = pItem->m_nLower;
  104.  
  105.     pItem->m_nOldPos = pItem->m_nPos;
  106.     pItem->m_nPos = nPos;
  107.  
  108.     return pItem->m_nOldPos;
  109. }
  110.  
  111. void CLineChartCtrl::OnPaint() 
  112. {
  113.     CPaintDC dc(this); // device context for painting
  114.     
  115.     CRect rcClient;
  116.     GetClientRect(rcClient);
  117.  
  118.     // draw scale
  119.     if (m_MemDC.GetSafeHdc() != NULL)
  120.         dc.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 0, 0, SRCCOPY);
  121. }
  122.  
  123. void CLineChartCtrl::DrawSpike()
  124. {
  125.     CRect rcClient;
  126.     GetClientRect(rcClient);
  127.  
  128.     if (m_MemDC.GetSafeHdc() != NULL)
  129.     {
  130.         m_MemDC.BitBlt(0, 0, rcClient.Width(), rcClient.Height(), &m_MemDC, 4, 0, SRCCOPY);
  131.  
  132.         // draw scale
  133.         CRect rcRight = rcClient;
  134.         rcRight.left = rcRight.right - 4;
  135.         m_MemDC.SetBkColor(RGB(0,0,0));
  136.  
  137.         CBrush bkBrush(HS_HORIZONTAL,RGB(0,128,0));  
  138.         m_MemDC.FillRect(rcRight,&bkBrush);
  139.  
  140.         static BOOL bDrawVerticle = FALSE;
  141.         bDrawVerticle = !bDrawVerticle;
  142.  
  143.         // Draw Verticle lines only every two steps
  144.         if (bDrawVerticle)
  145.         {
  146.             CPen pen(PS_SOLID, 1, RGB(0,128,0));
  147.             CPen* pOldPen = m_MemDC.SelectObject(&pen);
  148.             m_MemDC.MoveTo(CPoint(rcClient.right-2, rcClient.top));
  149.             m_MemDC.LineTo(CPoint(rcClient.right-2, rcClient.bottom));
  150.             m_MemDC.SelectObject(pOldPen);
  151.         }
  152.  
  153.         int nCount = m_items.GetSize();
  154.         CLineChartItem* pItem;
  155.         CPoint ptOld, ptNew;
  156.         for (int i=0; i<nCount; i++)
  157.         {
  158.             pItem = m_items.GetAt(i);
  159.  
  160.             UINT  nRange = pItem->m_nUpper - pItem->m_nLower;
  161.             ptOld.x = rcRight.left-1; // Minus one to make sure to draw inside the area
  162.             ptNew.x = rcRight.right-1;
  163.             ptOld.y = (int)((((float)(nRange - pItem->m_nOldPos))/(float)nRange)
  164.                 * (float)rcRight.Height());
  165.             ptNew.y = (int)((((float)(nRange - pItem->m_nPos))/(float)nRange)
  166.                 * (float)rcRight.Height());
  167.  
  168.             CPen pen(PS_SOLID, 1, pItem->m_colorLine);
  169.             CPen* pOldPen = m_MemDC.SelectObject(&pen);
  170.             m_MemDC.MoveTo(ptOld);
  171.             m_MemDC.LineTo(ptNew);
  172.             m_MemDC.SelectObject(pOldPen);
  173.         }
  174.     }
  175. }
  176.  
  177. BOOL CLineChartCtrl::Add(COLORREF color, UINT Upper, UINT Lower)
  178. {
  179.     CLineChartItem* pItem = new CLineChartItem;
  180.     pItem->m_colorLine = color;
  181.     pItem->m_nLower = Lower;
  182.     pItem->m_nUpper = Upper;
  183.     pItem->m_nPos = 0;
  184.     pItem->m_nOldPos = 0;
  185.  
  186.     try 
  187.     {
  188.         m_items.Add(pItem);
  189.  
  190.         InvalidateCtrl();
  191.         return TRUE;
  192.     }
  193.     catch (CMemoryException* e)
  194.     {
  195.         if (pItem !=NULL) 
  196.             delete pItem;
  197.         e->Delete();
  198.         return FALSE;
  199.     }
  200. }
  201.  
  202. void CLineChartCtrl::Go()
  203. {
  204.     DrawSpike();
  205.  
  206.     Invalidate(FALSE);
  207. }
  208.