Mam wrazenie, ze programisci staja sie coraz bardziej leniwi i potrzebuja do wszystkiego komponentow (ale kto bedzie pisal komponenty ?!)
3 minuty "wytezonej" pracy - mozna by uogolnic TShape (troche wiecej pracy)
unit Trojkat;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TTriangle = class(TGraphicControl)
private
FPen: TPen;
FBrush: TBrush;
procedure SetBrush(Value: TBrush);
procedure SetPen(Value: TPen);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
procedure StyleChanged(Sender: TObject);
property Align;
property Brush: TBrush read FBrush write SetBrush;
property DragCursor;
property DragMode;
property Enabled;
property ParentShowHint;
property Pen: TPen read FPen write SetPen;
property ShowHint;
property Visible;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;
procedure Register;
implementation
constructor TTriangle.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
Width := 65;
Height := 65;
FPen := TPen.Create;
FPen.OnChange := StyleChanged;
FBrush := TBrush.Create;
FBrush.OnChange := StyleChanged;
end;
destructor TTriangle.Destroy;
begin
FPen.Free;
FBrush.Free;
inherited Destroy;
end;
procedure TTriangle.Paint;
var
S: Integer;
PP:array[0..3] of TPoint;
x,y,w,h:integer;
begin
with Canvas do
begin
Pen := FPen;
Brush := FBrush;
X := Pen.Width div 2;
Y := X;
W := Width - Pen.Width + 1;
H := Height - Pen.Width;
if Pen.Width = 0 then
begin
Dec(W);
Dec(H);
end;
if W < H then S := W else S := H;
PP[0] := POINT(x+ w div 2,y);
PP[1] := POINT(x+w,y+h);
PP[2] := POINT(x,y+h);
PP[3] := PP[0];
Canvas.Polygon(PP);
end;
end;
procedure TTriangle.StyleChanged(Sender: TObject);
begin
Invalidate;
end;
procedure TTriangle.SetBrush(Value: TBrush);
begin
FBrush.Assign(Value);
end;
procedure TTriangle.SetPen(Value: TPen);
begin
FPen.Assign(Value);
end;
procedure Register;
begin
RegisterComponents('Samples', [TTriangle]);
end;
end.
PS. do TShape nie mozna stosowac regionow bo to kontrolka graficzna (nie ma uchwytu okna - ona dziedziczy canvas po obiekcie na ktorym jest)
PSII Xor ze w delphi - nie zauwazylem c++ (z RSS) ale to juz nie problem jak cos to poswiece kolejne 3 minuty ! :)