TextProgressCtrl.cpp
上传用户:nhyuejuan
上传日期:2013-12-02
资源大小:171k
文件大小:9k
源码类别:

状态条

开发平台:

Visual C++

  1. //以下是CPP文件
  2. // TextProgressCtrl.cpp : implementation file
  3. //
  4. // Written by Chris Maunder (chrismaunder@codeguru.com)
  5. // Copyright 1998.
  6. //
  7. // Modified : 26/05/98 Jeremy Davis, jmd@jvf.co.uk
  8. //                Added colour routines
  9. //
  10. // TextProgressCtrl is a drop-in replacement for the standard 
  11. // CProgressCtrl that displays text in a progress control.
  12. //
  13. // This code may be used in compiled form in any way you desire. This
  14. // file may be redistributed by any means PROVIDING it is not sold for
  15. // profit without the authors written consent, and providing that this
  16. // notice and the authors name is included. If the source code in 
  17. // this file is used in any commercial application then an email to
  18. // the me would be nice.
  19. //
  20. // This file is provided "as is" with no expressed or implied warranty.
  21. // The author accepts no liability if it causes any damage to your
  22. // computer, causes your pet cat to fall ill, increases baldness or
  23. // makes you car start emitting strange noises when you start it up.
  24. //
  25. // Expect bugs.
  26. // 
  27. // Please use and enjoy. Please let me know of any bugs/mods/improvements 
  28. // that you have found/implemented and I will fix/incorporate them into this
  29. // file. 
  30. #include "stdafx.h"
  31. #include "TextProgressCtrl.h"
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37. #ifndef _MEMDC_H_
  38. //////////////////////////////////////////////////
  39. // CMemDC - memory DC
  40. //
  41. // Author: Keith Rule
  42. // Email:  keithr@europa.com
  43. // Copyright 1996-1997, Keith Rule
  44. //
  45.  // You may freely use or modify this code provided this
  46. // Copyright is included in all derived versions.
  47. //
  48. // History - 10/3/97 Fixed scrolling bug.
  49. //                   Added print support.
  50. //
  51. // This class implements a memory Device Context
  52. class CMemDC : public CDC
  53. {
  54. public:
  55.  // constructor sets up the memory DC
  56.     CMemDC(CDC* pDC) : CDC()
  57.     {
  58.         ASSERT(pDC != NULL);
  59.         m_pDC = pDC;
  60.         m_pOldBitmap = NULL;
  61.         m_bMemDC = !pDC->IsPrinting();
  62.               
  63.         if (m_bMemDC)    // Create a Memory DC
  64.         {
  65.             pDC->GetClipBox(&m_rect);
  66.     CreateCompatibleDC(pDC);
  67.             m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  68.             m_pOldBitmap = SelectObject(&m_bitmap);
  69.             SetWindowOrg(m_rect.left, m_rect.top);
  70.         }
  71.         else        // Make a copy of the relevent parts of the current DC for printing
  72.         {
  73.             m_bPrinting = pDC->m_bPrinting;
  74.             m_hDC       = pDC->m_hDC;
  75.             m_hAttribDC = pDC->m_hAttribDC;
  76.         }
  77.     }
  78.  // Destructor copies the contents of the mem DC to the original DC
  79.     ~CMemDC()
  80.     {
  81.         if (m_bMemDC) 
  82.         {    
  83.             // Copy the offscreen bitmap onto the screen.
  84.             m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  85.                           this, m_rect.left, m_rect.top, SRCCOPY);
  86.             //Swap back the original bitmap.
  87.             SelectObject(m_pOldBitmap);
  88.          } else {
  89.             // All we need to do is replace the DC with an illegal value,
  90.             // this keeps us from accidently deleting the handles associated with
  91.             // the CDC that was passed to the constructor.
  92.             m_hDC = m_hAttribDC = NULL;
  93.         }
  94.     }
  95.     // Allow usage as a pointer
  96.     CMemDC* operator->() {return this;}
  97.  // Allow usage as a pointer
  98.     operator CMemDC*() {return this;}
  99. private:
  100.     CBitmap  m_bitmap;      // Offscreen bitmap
  101.     CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
  102.     CDC*     m_pDC;         // Saves CDC passed in constructor
  103.     CRect    m_rect;        // Rectangle of drawing area.
  104.     BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
  105. };
  106. #endif
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CTextProgressCtrl
  109. CTextProgressCtrl::CTextProgressCtrl()
  110. {
  111.     m_nPos            = 0;
  112.     m_nStepSize        = 1;
  113.     m_nMax            = 100;
  114.     m_nMin            = 0;
  115.     m_bShowText        = TRUE;
  116.     m_strText.Empty();
  117.     m_colFore        = ::GetSysColor(COLOR_HIGHLIGHT);
  118.     m_colBk            = ::GetSysColor(COLOR_WINDOW);
  119.     m_colTextFore    = ::GetSysColor(COLOR_HIGHLIGHT);
  120.     m_colTextBk        = ::GetSysColor(COLOR_WINDOW);
  121.     m_nBarWidth = -1;
  122. }
  123. CTextProgressCtrl::~CTextProgressCtrl()
  124. {
  125. }
  126. BEGIN_MESSAGE_MAP(CTextProgressCtrl, CProgressCtrl)
  127.     //{{AFX_MSG_MAP(CTextProgressCtrl)
  128.     ON_WM_ERASEBKGND()
  129.     ON_WM_PAINT()
  130.     ON_WM_SIZE()
  131.     //}}AFX_MSG_MAP
  132.     ON_MESSAGE(WM_SETTEXT, OnSetText)
  133.     ON_MESSAGE(WM_GETTEXT, OnGetText)
  134. END_MESSAGE_MAP()
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CTextProgressCtrl message handlers
  137. LRESULT CTextProgressCtrl::OnSetText(UINT, LPCTSTR szText)
  138. {
  139.     LRESULT result = Default();
  140.     if ( (!szText && m_strText.GetLength()) ||
  141.          (szText && (m_strText != szText))   )
  142.     {
  143.         m_strText = szText;
  144.         Invalidate();
  145.     }
  146.     return result;
  147. }
  148. LRESULT CTextProgressCtrl::OnGetText(UINT cchTextMax, LPTSTR szText)
  149. {
  150.     if (!_tcsncpy(szText, m_strText, cchTextMax))
  151.         return 0;
  152.     else 
  153.         return min(cchTextMax, (UINT) m_strText.GetLength());
  154. }
  155. BOOL CTextProgressCtrl::OnEraseBkgnd(CDC* /*pDC*/) 
  156. {    
  157.      return TRUE;
  158. }
  159. void CTextProgressCtrl::OnSize(UINT nType, int cx, int cy) 
  160. {
  161.     CProgressCtrl::OnSize(nType, cx, cy);
  162.     
  163.     m_nBarWidth    = -1;   // Force update if SetPos called
  164. }
  165. void CTextProgressCtrl::OnPaint() 
  166. {
  167.     if (m_nMin >= m_nMax) 
  168.         return;
  169.     CRect LeftRect, RightRect, ClientRect;
  170.     GetClientRect(ClientRect);
  171.     double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
  172.     CPaintDC PaintDC(this); // device context for painting
  173.     CMemDC dc(&PaintDC);
  174.     //CPaintDC dc(this);    // device context for painting (if not double buffering)
  175.     LeftRect = RightRect = ClientRect;
  176.     LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
  177.     dc.FillSolidRect(LeftRect, m_colFore);
  178.     RightRect.left = LeftRect.right;
  179.     dc.FillSolidRect(RightRect, m_colBk);
  180.     if (m_bShowText)
  181.     {
  182.         CString str;
  183.         if (m_strText.GetLength())
  184.             str = m_strText;
  185.         else
  186.             str.Format("%d%%", (int)(Fraction*100.0));
  187.         dc.SetBkMode(TRANSPARENT);
  188.         CRgn rgn;
  189.         rgn.CreateRectRgn(LeftRect.left, LeftRect.top, LeftRect.right, LeftRect.bottom);
  190.         dc.SelectClipRgn(&rgn);
  191.         dc.SetTextColor(m_colTextBk);
  192.         dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  193.         rgn.DeleteObject();
  194.         rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right, RightRect.bottom);
  195.         dc.SelectClipRgn(&rgn);
  196.         dc.SetTextColor(m_colTextFore);
  197.         dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  198.     }
  199. }
  200. void CTextProgressCtrl::SetForeColour(COLORREF col)
  201. {
  202.     m_colFore = col;
  203. }
  204. void CTextProgressCtrl::SetBkColour(COLORREF col)
  205. {
  206.     m_colBk = col;
  207. }
  208. void CTextProgressCtrl::SetTextForeColour(COLORREF col)
  209. {
  210.     m_colTextFore = col;
  211. }
  212. void CTextProgressCtrl::SetTextBkColour(COLORREF col)
  213. {
  214.     m_colTextBk = col;
  215. }
  216. COLORREF CTextProgressCtrl::GetForeColour()
  217. {
  218.     return m_colFore;
  219. }
  220. COLORREF CTextProgressCtrl::GetBkColour()
  221. {
  222.     return m_colBk;
  223. }
  224. COLORREF CTextProgressCtrl::GetTextForeColour()
  225. {
  226.     return m_colTextFore;
  227. }
  228. COLORREF CTextProgressCtrl::GetTextBkColour()
  229. {
  230.     return m_colTextBk;
  231. }
  232. /////////////////////////////////////////////////////////////////////////////
  233. // CTextProgressCtrl message handlers
  234. void CTextProgressCtrl::SetShowText(BOOL bShow)
  235.     if (::IsWindow(m_hWnd) && m_bShowText != bShow)
  236.         Invalidate();
  237.     m_bShowText = bShow;
  238. }
  239. void CTextProgressCtrl::SetRange(int nLower, int nUpper)
  240. {
  241.     m_nMax = nUpper;
  242.     m_nMin = nLower;
  243. }
  244. int CTextProgressCtrl::SetPos(int nPos) 
  245. {    
  246.     if (!::IsWindow(m_hWnd))
  247.         return -1;
  248.     int nOldPos = m_nPos;
  249.     m_nPos = nPos;
  250.     CRect rect;
  251.     GetClientRect(rect);
  252.     double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
  253.     int nBarWidth = (int) (Fraction * rect.Width());
  254.     if (nBarWidth != m_nBarWidth)
  255.     {
  256.         m_nBarWidth = nBarWidth;
  257.         RedrawWindow();
  258.     }
  259.     return nOldPos;
  260. }
  261. int CTextProgressCtrl::StepIt() 
  262. {    
  263.    return SetPos(m_nPos + m_nStepSize);
  264. }
  265. int CTextProgressCtrl::OffsetPos(int nPos)
  266. {
  267.     return SetPos(m_nPos + nPos);
  268. }
  269. int CTextProgressCtrl::SetStep(int nStep)
  270. {
  271.     int nOldStep = nStep;
  272.     m_nStepSize = nStep;
  273.     return nOldStep;