home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cdgtst / digitst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-10  |  5.0 KB  |  266 lines

  1. // DigitST.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DigitST.h"
  6.  
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CDigitST
  15.  
  16. CDigitST::CDigitST()
  17. {
  18.     // Default number of digits
  19.     m_nPrecision = 2;
  20.  
  21.     // Default value
  22.     m_nValue = 0;
  23.  
  24.     // Don't display zeroes
  25.     m_bZeroPadding = FALSE;
  26.  
  27.     // Default resize is RIGHT-BOTTOM
  28.     m_dwResize = ST_RIGHT | ST_BOTTOM;
  29.  
  30.     m_nWidth = 0;
  31.     m_nHeight = 0;
  32. }
  33.  
  34.  
  35. CDigitST::~CDigitST()
  36. {
  37. }
  38.  
  39.  
  40. BEGIN_MESSAGE_MAP(CDigitST, CStatic)
  41.     //{{AFX_MSG_MAP(CDigitST)
  42.     ON_WM_PAINT()
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CDigitST message handlers
  48.  
  49.  
  50. void CDigitST::OnPaint() 
  51. {
  52.     PAINTSTRUCT lpPaint;
  53.  
  54.     BeginPaint(&lpPaint);
  55.  
  56.     CWindowDC dc(this);
  57.  
  58.     // If there is a bitmap loaded
  59.     if (m_bmDigit.m_hObject) DrawDigits(&dc);
  60.  
  61.     EndPaint(&lpPaint);
  62. } // End of OnPaint
  63.  
  64.  
  65. BOOL CDigitST::SetStyle(UINT nBitmapId, int nPrecision)
  66. {
  67.     BITMAP bm;
  68.     BOOL bRet;
  69.  
  70.     // Detach any previuos bitmap
  71.     m_bmDigit.Detach();
  72.     // Load new bitmap
  73.     bRet = m_bmDigit.LoadBitmap(nBitmapId);
  74.     // If all ok
  75.     if (bRet == TRUE)
  76.     {
  77.         // Get dimension
  78.         m_bmDigit.GetBitmap(&bm);
  79.         // Width of a SINGLE digit in a 12 digits bitmap
  80.         m_nWidth = (int)bm.bmWidth / 12;
  81.         // Height of the digits
  82.         m_nHeight = bm.bmHeight;
  83.  
  84.         SetPrecision(nPrecision);
  85.     }
  86.  
  87.     return bRet;
  88. } // End of SetStyle
  89.  
  90.  
  91. void CDigitST::SetValue(int nValue, BOOL bRepaint)
  92. {
  93.     // Set new value
  94.     m_nValue = nValue;
  95.  
  96.     // Repaint control
  97.     if (bRepaint == TRUE) Invalidate();
  98. } // End of SetValue
  99.  
  100.  
  101. int CDigitST::GetValue()
  102. {
  103.     return m_nValue;
  104. } // End of GetValue
  105.  
  106.  
  107. void CDigitST::SetPrecision(int nPrecision, BOOL bResize)
  108. {
  109.     // Set number of digits
  110.     // Some security
  111.     if (nPrecision >= ST_MIN_PRECISION && nPrecision <= ST_MAX_PRECISION)
  112.         m_nPrecision = nPrecision;
  113.  
  114.     // Resize control
  115.     if (bResize == TRUE) Resize();
  116. } // End of SetPrecision
  117.  
  118.  
  119. int CDigitST::GetPrecision()
  120. {
  121.     return m_nPrecision;
  122. } // End of GetPrecision
  123.  
  124.  
  125. void CDigitST::SetResize(DWORD dwResize, BOOL bResize)
  126. {
  127.     m_dwResize = dwResize;
  128.  
  129.     // Resize control
  130.     if (bResize == TRUE) Resize();
  131. } // End of SetResize
  132.  
  133.  
  134. DWORD CDigitST::GetResize()
  135. {
  136.     return m_dwResize;
  137. } // End of GetResize
  138.  
  139.  
  140. void CDigitST::SetZeroPadding(BOOL bPad, BOOL bRepaint)
  141. {
  142.     // Set new padding style
  143.     m_bZeroPadding = bPad;
  144.  
  145.     // Repaint control
  146.     if (bRepaint == TRUE) Invalidate();
  147. } // End of SetZeroPadding
  148.  
  149.  
  150. BOOL CDigitST::GetZeroPadding()
  151. {
  152.     return m_bZeroPadding;
  153. } // End of GetZeroPadding
  154.  
  155.  
  156. void CDigitST::Inc(BOOL bRepaint)
  157. {
  158.     SetValue(GetValue() + 1, bRepaint);
  159. } // End of Inc
  160.  
  161.  
  162. void CDigitST::Dec(BOOL bRepaint)
  163. {
  164.     SetValue(GetValue() - 1, bRepaint);
  165. } // End of Dec
  166.  
  167.  
  168. const char* CDigitST::GetVersionC()
  169. {
  170.   return "1.0";
  171. } // End of GetVersionC
  172.  
  173.  
  174. const short CDigitST::GetVersionI()
  175. {
  176.   return 10; // Divide by 10 to get actual version
  177. } // End of GetVersionI
  178.  
  179.  
  180. void CDigitST::DrawDigits(CDC* pDC)
  181. {
  182.     char szValue[ST_MAX_PRECISION+1];
  183.  
  184.     int nLoop;
  185.     int destX;
  186.     CRect rectCtrl;
  187.     int nAsciiChar;
  188.     CBitmap* pOldBitmap;
  189.  
  190.     CDC memDC;
  191.     memDC.CreateCompatibleDC(pDC);
  192.  
  193.     // Select bitmap
  194.     pOldBitmap = memDC.SelectObject(&m_bmDigit);
  195.     
  196.     GetClientRect(rectCtrl);
  197.  
  198.     // Start from more significative digit
  199.     destX = BORDER_SPACE;
  200.  
  201.     PrepareString(szValue);
  202.  
  203.     for (nLoop = 0; nLoop < m_nPrecision; nLoop++)
  204.     {
  205.         if (m_bZeroPadding == TRUE && m_nValue >= 0)
  206.             nAsciiChar = 0;
  207.         else
  208.             nAsciiChar = 10;
  209.  
  210.         // If included in '0'..'9'
  211.         if (szValue[nLoop] >= '0' && szValue[nLoop] <= '9')
  212.             nAsciiChar = szValue[nLoop] - 48;
  213.  
  214.         // If signed
  215.         if (szValue[nLoop] == '-') nAsciiChar = 11;
  216.  
  217.         pDC->BitBlt(destX, BORDER_SPACE, m_nWidth, m_nHeight, &memDC, 0+(m_nWidth*nAsciiChar), 0, SRCCOPY);
  218.         destX += m_nWidth;
  219.     }
  220.  
  221.     // Restore old selected bitmap
  222.     memDC.SelectObject(pOldBitmap);
  223. } // End of DrawDigits
  224.  
  225.  
  226. void CDigitST::Resize()
  227. {
  228.     CRect rectCtrl;
  229.  
  230.     Invalidate();
  231.  
  232.     // Modify control rect to fit all digits (specified by m_nPrecision)
  233.     GetWindowRect(rectCtrl);
  234.     GetParent()->ScreenToClient(rectCtrl);
  235.  
  236.     // Resize to RIGHT
  237.     if (m_dwResize & ST_RIGHT)
  238.         rectCtrl.right = rectCtrl.left + (m_nWidth * m_nPrecision)+(BORDER_SPACE*2);
  239.     else
  240.     // Resize to LEFT
  241.         rectCtrl.left = rectCtrl.right - (m_nWidth * m_nPrecision)-(BORDER_SPACE*2);
  242.  
  243.     // Resize to BOTTOM
  244.     if (m_dwResize & ST_BOTTOM)
  245.         rectCtrl.bottom = rectCtrl.top + m_nHeight+(BORDER_SPACE*2);
  246.     else
  247.     // Resize to TOP
  248.         rectCtrl.top = rectCtrl.bottom - m_nHeight-(BORDER_SPACE*2);
  249.  
  250.     MoveWindow(rectCtrl);
  251. } // End of Resize
  252.  
  253.  
  254. void CDigitST::PrepareString(char* szDest)
  255. {
  256.     char szStr[ST_MAX_PRECISION+1];    
  257.  
  258.     sprintf(szStr, "%*d", m_nPrecision,m_nValue);
  259.     sprintf(szDest, "%*s", m_nPrecision, &szStr[strlen(szStr)-m_nPrecision]);
  260. } // End of PrepareString
  261.  
  262.  
  263. #undef ST_MIN_PRECISION
  264. #undef ST_MAX_PRECISION
  265. #undef BORDER_SPACE
  266.