Your article is very interesting thanks a lot but i'mn trying to implement your code in VS 2008 but i'm having the following error if you can help: error C2440: 'static_cast'B_: impossible de convertir de 'LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)' en 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' thx in advance
ReplyOriginally posted by: FrAnK
When compiling the code in VC7 the following error occures:
D:\Projects\MinorApp\TextProgressCtrl.cpp(152) : error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl *)(UINT,LPTSTR)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
Even when changing to WPARAM and LPARAM the code will not compile!
It worked great with VC6!
Is there a setting in VC7 I should switch on (or off!) ?
FrAnK.
TextProgressCtrl.h
================
afx_msg LRESULT OnSetText(WPARAM , LPARAM szText);
afx_msg LRESULT OnGetText(WPARAM cchTextMax, LPARAM szText);
TextProgressCtrl.cpp
========================
LRESULT CTextProgressCtrl::OnSetText(WPARAM, LPARAM szText)
{
LRESULT result = Default();
if ( (!szText && m_strText.GetLength()) ||
(szText && (m_strText != (LPCTSTR)szText)) )
{
m_strText = (LPCTSTR)szText;
Invalidate();
}
return result;
}
LRESULT CTextProgressCtrl::OnGetText(WPARAM cchTextMax, LPARAM szText)
{
if (!_tcsncpy((LPTSTR)szText, m_strText, (UINT)cchTextMax))
return 0;
else
return min(cchTextMax, (UINT)m_strText.GetLength());
}
.............
Good Luck...
Reply
Originally posted by: Eric Steimle
if (m_bShowText)
dc.SetBkMode(TRANSPARENT)
Now I can call something like this
and the control will show Processing completed 25% in the progress bar. Call me lazy but I didn't feel like setting the i every time.
Not to mess with other peoples stuff but... I didn't feel like setting an int variable along with calling SetPos and I wanted a little change to the control so I could just add any text before the 25% or x% that the control all ready generates (maybe I'm just lazy). I made the following single line change to the CtextProgressCtrl class in CtextProgressCtrl.cpp file on line 219 in the OnPaint fuction.
{
CString str;
if (m_strText.GetLength())
//str = m_strText;
str.Format(" %s%d%%",m_strText,(int)(Fraction*100.0));//Eric changed this line
else
str.Format(" %d%%", (int)(Fraction*100.0));
m_ProgressBar.SetShowText(true);
m_ProgressBar.SetPos(25);
m_ProgressBar.SetRange(0,100);
CString str;
str.Format("Processing completed");
m_ProgressBar.SetWindowText(str);
Originally posted by: Eric Steimle
if (m_bShowText)
dc.SetBkMode(TRANSPARENT)
Now I can call something like this
and the control will show Processing completed 25% in the progress bar. Call me lazy but I didn't feel like setting the i every time.
Not to mess with other peoples stuff but... I didn't feel like setting an int variable along with calling SetPos and I wanted a little change to the control so I could just add any text before the 25% or x% that the control all ready generates (maybe I'm just lazy). I made the following single line change to the CtextProgressCtrl class in CtextProgressCtrl.cpp file on line 219 in the OnPaint fuction.
{
CString str;
if (m_strText.GetLength())
//str = m_strText;
str.Format(" %s%d%%",m_strText,(int)(Fraction*100.0));//Eric changed this line
else
str.Format(" %d%%", (int)(Fraction*100.0));
m_ProgressBar.SetShowText(true);
m_ProgressBar.SetPos(25);
m_ProgressBar.SetRange(0,100);
CString str;
str.Format("Processing completed");
m_ProgressBar.SetWindowText(str);
Originally posted by: Krazer
BEGIN_MESSAGE_MAP(CTextProgressCtrl, CProgressCtrl)
COLORREF CTextProgressCtrl::SetForeColour(COLORREF col)
color=m_colFore;
COLORREF CTextProgressCtrl::SetBkColour(COLORREF col)
color=m_colFore;
void CTextProgressCtrl::GetRange(int& nLower, int& nUpper)
LRESULT CTextProgressCtrl::OnSetStep(WPARAM wParam, LPARAM lParam)
wLow=m_nMin;
nLow=m_nMin;
Hope this works, wrote it for most of them but I only used StepIT and SetPOS.
//{{AFX_MSG_MAP(CTextProgressCtrl)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_SIZE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETTEXT, OnSetText)
ON_MESSAGE(WM_GETTEXT, OnGetText)
ON_MESSAGE(PBM_SETSTEP, OnSetStep)
ON_MESSAGE(PBM_STEPIT, OnStepIt)
ON_MESSAGE(PBM_GETPOS, OnGetPos)
ON_MESSAGE(PBM_SETPOS, OnSetPos)
ON_MESSAGE(PBM_DELTAPOS, OnDeltaPos)
ON_MESSAGE(PBM_SETBARCOLOR, OnSetBarColor)
ON_MESSAGE(PBM_SETBKCOLOR, OnSetBkColor)
ON_MESSAGE(PBM_GETRANGE, OnGetRange)
ON_MESSAGE(PBM_SETRANGE, OnSetRange)
ON_MESSAGE(PBM_SETRANGE32, OnSetRange32)
END_MESSAGE_MAP()
{
COLORREF color;
m_colFore = col;
return m_colFore;
}
{
COLORREF color;
m_colBk = col;
return color;
}
{
nLower=m_nMin;
nUpper=m_nMax;
}
void CTextProgressCtrl::SetRange(short nLower, short nUpper)
{
m_nMax = nUpper;
m_nMin = nLower;
}
void CTextProgressCtrl::SetRange32(int nLower, int nUpper)
{
m_nMax = nUpper;
m_nMin = nLower;
}
{ return SetStep(wParam);}
LRESULT CTextProgressCtrl::OnStepIt(WPARAM wParam, LPARAM lParam)
{ return StepIt();}
LRESULT CTextProgressCtrl::OnGetPos(WPARAM wParam, LPARAM lParam)
{ return GetPos();}
LRESULT CTextProgressCtrl::OnSetPos(WPARAM wParam, LPARAM lParam)
{ return SetPos(wParam);}
LRESULT CTextProgressCtrl::OnDeltaPos(WPARAM wParam, LPARAM lParam)
{ return OffsetPos(wParam);}
LRESULT CTextProgressCtrl::OnSetBarColor(WPARAM wParam, LPARAM lParam)
{ return SetForeColour((COLORREF)lParam);}
LRESULT CTextProgressCtrl::OnSetBkColor(WPARAM wParam, LPARAM lParam)
{ return SetBkColour((COLORREF)lParam);}
LRESULT CTextProgressCtrl::OnGetRange(WPARAM wParam, LPARAM lParam)
{
int nLower,nUpper;
GetRange(nLower,nUpper);
if(lParam!=NULL)
{
((PBRANGE *)lParam)->iLow=nLower;
((PBRANGE *)lParam)->iHigh=nUpper;
}
if(wParam)
return nLower;
else
return nUpper;
}
LRESULT CTextProgressCtrl::OnSetRange(WPARAM wParam, LPARAM lParam)
{
WORD wLow,wHigh;
wHigh=m_nMax;
SetRange(LOWORD(lParam),HIWORD(lParam));
return MAKELRESULT(wLow,wHigh);
}
LRESULT CTextProgressCtrl::OnSetRange32(WPARAM wParam, LPARAM lParam)
{
int nLow,nHigh;
nHigh=m_nMax;
SetRange32((int)wParam,(int)lParam);
return MAKELRESULT(LOWORD(nLow),LOWORD(nHigh));
}
Originally posted by: Roger Allen
[ccode]
CRect LeftRect, RightRect, ClientRect;
double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
CPaintDC PaintDC(this); // device context for painting
LeftRect = RightRect = ClientRect;
// KK - 980419 Added vertical control check
//RightRect.left = LeftRect.right;
if (m_bShowText)
// need to use our own font
if (GetStyle() & PBS_VERTICAL)
dc.SetBkMode(TRANSPARENT);
CRgn rgn;
if (GetStyle() & PBS_VERTICAL)
I implemented the vertical text for the progress bar. New OnPaint is:
void CTextProgressCtrl::OnPaint()
{
if (m_nMin >= m_nMax)
return;
GetClientRect(ClientRect);
CMemDC dc(&PaintDC);
//CPaintDC dc(this); // device context for painting (if not double buffering)
//
if (GetStyle() & PBS_VERTICAL)
{
LeftRect.top = LeftRect.bottom + (int)((LeftRect.top - LeftRect.bottom)*Fraction);
RightRect.bottom = LeftRect.top;
}
else
{
LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
RightRect.left = LeftRect.right;
}
//LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
dc.FillSolidRect(LeftRect, m_colFore);
dc.FillSolidRect(RightRect, m_colBk);
{
// RIA : Support for rotated text in vertical scroll bar
CFont font ;
LOGFONT lf ;
CFont* pOldFont ;
memset(&lf, 0, sizeof(LOGFONT)) ;
lf.lfHeight = -11 ;
lf.lfWidth = 0 ;
lf.lfWeight = FW_NORMAL ;
strcpy(lf.lfFaceName, "Arial") ;
{
lf.lfOrientation = 900 ; // rotate 90'
lf.lfEscapement = 900 ; // rotate 90'
}
font.CreateFontIndirect(&lf) ;
pOldFont = (CFont*)dc.SelectObject(&font) ;
CString str;
if (m_strText.GetLength())
str = m_strText;
else
str.Format("%d%%", (int)(Fraction*100.0));
rgn.CreateRectRgn(LeftRect.left, LeftRect.top, LeftRect.right, LeftRect.bottom);
dc.SelectClipRgn(&rgn);
dc.SetTextColor(m_colTextBk);
{
// have to use TextOut with a rotated font, else text not positioned in "centre" correctly
CSize size = dc.GetTextExtent(str) ;
CRect rect ;
GetWindowRect(&rect) ;
dc.TextOut((rect.Width() - size.cy) / 2, rect.Height() - (rect.Height() - size.cx) / 2, str) ;
rgn.DeleteObject();
rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right, RightRect.bottom);
dc.SelectClipRgn(&rgn);
dc.SetTextColor(m_colTextFore);
dc.TextOut((rect.Width() - size.cy) / 2, rect.Height() - (rect.Height() - size.cx) / 2, str) ;
}
else
{
dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
rgn.DeleteObject();
rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right, RightRect.bottom);
dc.SelectClipRgn(&rgn);
dc.SetTextColor(m_colTextFore);
dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
dc.SelectObject(pOldFont) ; // restore original DC font
}
}
[/ccode]
Originally posted by: Bruce Sohn
The code gives me lots of help.
I want to use the source to apply to such as 'file open' action.
If I open some file(text file, etc.), the progress bar will start increase in proportion to the file size.
The larger file size, the slower its increasing speed.
The progress bar code with 'file open' action will be needed. Instead of 'file open' action, any time-consuming action will be included.
Go for it!
ReplyOriginally posted by: David Grigsby
Similar to the problem already noted with GetRange(),
GetPos() must be overridden to return the local m_nPos member.
If you don't do this, GetPos() will always return 0.
Originally posted by: Leigh
How can i change the text font size and style?
Reply
Originally posted by: Klaus Kurzawa
In TextProgressCtrl.cpp comment the lines #208 and #211
// KK - 980419 Added vertical control check
// Moved this into horizontal section
I haven't done anything to display vertical text.
Maybe someone else can do this before me?
and add this section instead.
//
if (GetStyle() & PBS_VERTICAL)
{
LeftRect.top = LeftRect.bottom + (int)((LeftRect.top - LeftRect.bottom)*Fraction);
RightRect.bottom = LeftRect.top;
}
else
{
LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
RightRect.left = LeftRect.right;
}
// Moved this into horizontal section
//LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
dc.FillSolidRect(LeftRect, m_colFore);
//RightRect.left = LeftRect.right;
dc.FillSolidRect(RightRect, m_colBk);