1 // TextProgressCtrl.cpp : implementation file
3 // Written by Chris Maunder (cmaunder@mail.com)
6 // TextProgressCtrl is a drop-in replacement for the standard
7 // CProgressCtrl that displays text in a progress control.
9 // This code may be used in compiled form in any way you desire. This
10 // file may be redistributed by any means PROVIDING it is not sold for
11 // profit without the authors written consent, and providing that this
12 // notice and the authors name is included. If the source code in
13 // this file is used in any commercial application then an email to
14 // the me would be nice.
16 // This file is provided "as is" with no expressed or implied warranty.
17 // The author accepts no liability if it causes any damage to your
18 // computer or anything else vaguely within it's vicinity.
22 // Please use and enjoy. Please let me know of any bugs/mods/improvements
23 // that you have found/implemented and I will fix/incorporate them into this
25 // Modified by Philip Oldaker 2000
28 #include "TextProgressCtrl.h"
33 static char THIS_FILE[] = __FILE__;
36 IMPLEMENT_DYNAMIC(CTextProgressCtrl,CUIODColumnCtrl)
37 /////////////////////////////////////////////////////////////////////////////
40 CTextProgressCtrl::CTextProgressCtrl()
47 m_crBarClr = CLR_DEFAULT;
48 m_crBgClr = CLR_DEFAULT;
49 m_crTextClr = CLR_DEFAULT;
50 m_crSelBarClr = CLR_DEFAULT;
51 m_crSelBgClr = CLR_DEFAULT;
52 m_crSelTextClr = CLR_DEFAULT;
55 CTextProgressCtrl::~CTextProgressCtrl()
59 void CTextProgressCtrl::DoPaint(CDC *PaintDC,CRect rcClient,bool bSelected)
64 bool bInvert=bSelected;
65 COLORREF crBarColour = (m_crBarClr == CLR_DEFAULT)? RGB(10,20,200) : m_crBarClr;
66 COLORREF crBgColour = (m_crBgClr == CLR_DEFAULT)? ::GetSysColor(COLOR_MENU) : m_crBgClr;
67 COLORREF crTextColor = (m_crTextClr == CLR_DEFAULT)? ::GetSysColor(COLOR_BTNTEXT) : m_crTextClr;
70 if (m_crSelBarClr != CLR_DEFAULT)
72 crBarColour = m_crSelBarClr;
75 if (m_crSelBgClr != CLR_DEFAULT)
77 crBgColour = m_crSelBgClr;
80 if (m_crSelTextClr != CLR_DEFAULT)
82 crTextColor = m_crSelTextClr;
86 double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
89 dc.Rectangle(rcClient.left+1,rcClient.top+1,rcClient.right-1,rcClient.bottom-1);
90 rcClient.DeflateRect(2,2);
91 CRect LeftRect(rcClient);
92 CRect RightRect(rcClient);
94 LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
95 RightRect.left = LeftRect.right;
96 dc.FillSolidRect(LeftRect, crBarColour);
97 dc.FillSolidRect(RightRect, crBgColour);
98 // Draw Text if not vertical
102 if (m_strText.GetLength())
105 str.Format(_T("%d%%"), (int)(Fraction*100.0));
106 CSize sz = dc.GetTextExtent(str);
108 if (rcClient.Width() > sz.cx)
109 nTextPos = (rcClient.Width() - sz.cx) / 2;
110 if (!bSelected && LeftRect.Width() >= nTextPos)
112 dc.SetTextColor(RGB(255,255,255));
115 dc.SetTextColor(crTextColor);
116 dc.SetBkMode(TRANSPARENT);
117 DWORD dwTextStyle = DT_CENTER | DT_VCENTER | DT_SINGLELINE;
118 dc.DrawText(str, rcClient, dwTextStyle);
122 dc.InvertRect(rcClient);
126 /////////////////////////////////////////////////////////////////////////////
127 // CTextProgressCtrl operations
129 void CTextProgressCtrl::SetShowText(BOOL bShow)
134 void CTextProgressCtrl::SetRange(int nLower, int nUpper)
140 void CTextProgressCtrl::GetRange(int& nLower, int& nUpper) const
146 int CTextProgressCtrl::SetPos(int nPos)
148 int nOldPos = m_nPos;
153 int CTextProgressCtrl::StepIt()
155 return SetPos(m_nPos + m_nStepSize);
158 int CTextProgressCtrl::OffsetPos(int nPos)
160 return SetPos(m_nPos + nPos);
163 int CTextProgressCtrl::SetStep(int nStep)
165 int nOldStep = m_nStepSize;
170 COLORREF CTextProgressCtrl::SetBarColor(COLORREF crBarClr /*= CLR_DEFAULT*/,COLORREF crSelBarClr /*= CLR_DEFAULT*/)
172 COLORREF crOldBarClr = m_crBarClr;
173 m_crBarClr = crBarClr;
174 m_crSelBarClr = crSelBarClr;
178 COLORREF CTextProgressCtrl::GetBarColor() const
183 COLORREF CTextProgressCtrl::GetSelBarColor() const
185 return m_crSelBarClr;
188 COLORREF CTextProgressCtrl::SetBgColor(COLORREF crBgClr /*= CLR_DEFAULT*/,COLORREF crSelBgClr /*= CLR_DEFAULT*/)
190 COLORREF crOldBgClr = m_crBgClr;
192 m_crSelBgClr = crSelBgClr;
196 COLORREF CTextProgressCtrl::GetBgColor() const
202 COLORREF CTextProgressCtrl::GetSelBgColor() const
207 COLORREF CTextProgressCtrl::SetTextColor(COLORREF crTextClr /*= CLR_DEFAULT*/,COLORREF crSelTextClr /*= CLR_DEFAULT*/)
209 COLORREF crOldTextClr = m_crTextClr;
210 m_crTextClr = crTextClr;
211 m_crSelTextClr = crSelTextClr;
215 COLORREF CTextProgressCtrl::GetTextColor() const
220 COLORREF CTextProgressCtrl::GetSelTextColor() const
222 return m_crSelTextClr;