Re:Stack overflow
Hi all, I just tried to write my first graphical component, so please don't
tease.... ;) I wrote my own progressbar as I need it to include text other
than just the percent. It seems to work ok until I try to set the text of
the component in the Paint method. I get a stack overflow in runtime but not
in designtime.... any help would be greatly appreciated as I really do not
know what I am doing wrong.... oops, I do not know what I am doing at all.
... here is my code so far...
class PACKAGE TCDSProgressBar : public TGraphicControl
{
private:
long FMin;
long FMax;
long FProgress;
AnsiString FPText;
TColor FBackgroundColor;
TColor FForegroundColor;
bool FShowText;
void __fastcall SetProgress(long value);
void __fastcall SetPText(AnsiString value);
void __fastcall SetShowText(bool value);
void __fastcall SetBackgroundColor(TColor);
void __fastcall SetForegroundColor(TColor);
void __fastcall SetTextColor(TColor);
protected:
virtual void __fastcall Paint(void);
public:
__fastcall TCDSProgressBar(TComponent* Owner);
__published:
__property long Max = { read=FMax, write=FMax };
__property long Progress = { read=FProgress, write=SetProgress };
__property AnsiString PText = { read=FPText, write=SetPText };
__property TColor BackgroundColor = { read=FBackgroundColor,
write=SetBackgroundColor };
__property TColor ForegroundColor = { read=FForegroundColor,
write=SetForegroundColor };
__property bool ShowText = { read=FShowText, write=SetShowText };
};
//--------------------------------------------------------------------------
-
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TCDSProgressBar *)
{
new TCDSProgressBar(NULL);
}
//--------------------------------------------------------------------------
-
__fastcall TCDSProgressBar::TCDSProgressBar(TComponent* Owner)
: TGraphicControl(Owner)
{
FMin = 0;
FMax = 100;
FProgress = 0;
}
//--------------------------------------------------------------------------
-
namespace Cdsprogressbar
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TCDSProgressBar)};
RegisterComponents("CompuData", classes, 0);
}
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::SetProgress(long value)
{
//TODO: Add your source code here
FProgress = value;
Paint();
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::Paint(void)
{ // This is the guts of it right here
// Fetch the current progress in pixels
long ForegroundWidth = Width>= FMax?(Width / FMax) * FProgress:1;
long BackgroundWidth = Width;
// Now calculate both rectangles
TRect ProgressRect(0,0,ForegroundWidth,Height);
TRect
BackgroundRect(ForegroundWidth,0,BackgroundWidth+ForegroundWidth,Height);
// Ok, lets draw them now
Canvas->Brush->Color = FForegroundColor;
Canvas->FillRect(ProgressRect);
Canvas->Brush->Color = FBackgroundColor;
Canvas->FillRect(BackgroundRect);
// Moving right along to the Text property
AnsiString T = PText;
// if we are to show the percent as well, then lets check this out
if(FShowText && T.Length()) {
long OurProgress;
if(FProgress>= 100) OurProgress = (FProgress / 100);
else if(FProgress == 0) OurProgress = 0;
else OurProgress = 1;
T = T + AnsiString(" ") + IntToStr(OurProgress);
}
Canvas->TextRect(TRect(Left + 2,Top + 2,Left + (Width-2),Top +
(Height-2)),2,2,T);
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::SetPText(AnsiString value)
{
PText = value;
Paint();
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::SetForegroundColor(TColor value)
{
FForegroundColor = value;
Paint();
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::SetBackgroundColor(TColor value)
{
FBackgroundColor = value;
Paint();
}
//--------------------------------------------------------------------------
-
void __fastcall TCDSProgressBar::SetShowText(bool value)
{
FShowText = value;
Paint();
}
//--------------------------------------------------------------------------
-