unit U_CanvasBar;
interface
uses Graphics,Types,SysUtils,Classes,Windows;
type TCanvasbar =
class(TComponent)
private
fStep,FPercent,fPosition:integer;
fMin,fMax:integer;
fLeft,fTop,fWidth,fHeight:integer;
fParent:TCanvas;
fBorderCol,fColor,fGaugeCol:TColor;
fFont:TFont;
procedure SetPosition(value:integer);
public
constructor Create(AOwner:TComponent);
override;
destructor Destroy;
override;
procedure Paint;
//kein override, sollte da eins sein?
procedure StepIt;
published
property Parent : TCanvas
read fParent
write fParent;
property Step : integer
read fStep
write fStep ;
//stored 1;
property Position: integer
read fPosition
write SetPosition;
//stored 0;
property Percent : integer
read fPercent ;
//stored 0;
property Left : integer
read fLeft
write fLeft ;
//stored 0;
property Top : integer
read fTop
write fTop ;
//stored 0;
property Height : integer
read fHeight
write fHeight ;
//stored 0;
property Width : integer
read fWidth
write fWidth ;
//stored 0;
property BorderColor: TColor
read fBorderCol
write fBorderCol;
//stored clblack;
property Color: TColor
read fColor
write FColor ;
//stored clgray;
property FGaugeColor:TColor
read fGaugeCol
write fGaugeCol ;
//stored clmedgray;
property Font: TFont
read fFont
write fFont;
property Min: integer
read fMin
write fMin;
property Max: integer
read fmax
write fMax;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('
Custom',[TCanvasBar]);
end;
constructor TCanvasBar.Create(AOwner:TComponent);
begin
inherited;
fBorderCol := clBlack;
//hier werden die angesprochenen standardwerte gesetzt, die ich im OI nicht überschrieben kann.
fGaugeCol := clSilver;
//im OI erscheint das hier übrigens auch als clblack....
fColor := clGray;
//...ebenso wie das hier.
fFont:=TFont.Create;
fMin:=0;
fMax:=100;
fPosition:=0;
fPercent:=0;
fStep:=1;
fWidth:=160;
fHeight:=16;
end;
destructor TCanvasBar.Destroy;
begin
inherited;
fFont.Free;
end;
procedure TCanvasBar.Paint;
var storpen:TPen;
storbrush:TBrush;
storfont:TFont;
gaugerect:TRect;
textwidth, textHeight, textLeft,textTop:integer;
begin
storpen:=TPen.Create;
storbrush:=TBrush.Create;
storfont:=TFont.Create;
storpen.Assign(fparent.pen);
storbrush.Assign(fparent.Brush);
storfont.Assign(fparent.Font);
fparent.Pen.Color:=fBorderCol;
fparent.Brush.Color:=fColor;
fparent.Rectangle(fleft,ftop,fleft+fwidth,ftop+fheight);
if fPercent > 0
then
begin
GaugeRect.Left:=fLeft+1;
GaugeRect.Top:=fTop+1;
GaugeRect.Bottom:=fTop + fHeight - 1;
GaugeRect.Right:= fLeft + round( (fwidth-2) * ( fPosition / fMax ));
fparent.Pen.Color:=fGaugeCol;
fparent.Brush.Color:=fGaugeCol;
fparent.Rectangle(GaugeRect);
end;
fparent.Font:= fFont;
textwidth := fparent.TextWidth(inttostr(fPercent)+'
%');
textHeight := fparent.TextHeight(inttostr(fPercent)+'
%');
textLeft := round(fLeft + fWidth / 2 - textWidth / 2);
textTop := round(fTop + fHeight / 2 - textHeight / 2);
SetBKMode(parent.Handle,TRANSPARENT);
//das geht eleganter, ich weiss.
fparent.TextOut(textLeft,textTop,inttostr(fPercent)+'
%');
SetBKMode(parent.Handle,OPAQUE);
fparent.Brush.Assign(storbrush);
fparent.Pen.Assign(storpen);
fparent.Font.Assign(storfont);
storpen.Free;
storbrush.Free;
storfont.Free;
end;
procedure TCanvasBar.StepIt;
begin
fPosition := fPosition+fStep;
self.SetPosition(fPosition);
Paint;
end;
procedure TCanvasbar.SetPosition(value:integer);
begin
fPosition := value;
if fMin <> fMax
then
fPercent := Round(Abs((fPosition - fMin) / (fMax - fMin)) * 100)
else fPercent:=0;
end;
end.