unit MyShape;
interface
uses
SysUtils, Classes, Controls, Graphics;
type
TSample = (Rou,ran);
TMyShape = class(TGraphicControl)
private
{ Private declarations }
FShape: TSample;
FPen: TPen;
FBrush: TBrush;
procedure SetShape(const Value: TSample);
procedure SetPen(const Value: TPen);
procedure StyleChange(obj: TObject);
procedure SetBrush(const Value: TBrush);
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Width;
property Height;
property Canvas;
property Shape: TSample read FShape write SetShape;
property Pen: TPen read FPen write SetPen;
property Brush: TBrush read FBrush write SetBrush;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyShape]);
end;
{ TMyShape }
constructor TMyShape.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShape := ran;
FPen := TPen.Create;
FPen.OnChange := StyleChange;
Canvas.Pen := FPen;
FBrush := TBrush.Create;
FBrush.OnChange := StyleChange;
Canvas.Brush := FBrush;
Width := 400;
Height := 400;
Invalidate;
end;
destructor TMyShape.Destroy;
begin
FPen.Free;
FBrush.Free;
inherited Destroy;
end;
procedure TMyShape.Paint;
begin
inherited;
if FShape = rou then
begin
Canvas.RoundRect(0,0,Width,Height,50,50);
end
else
begin
Canvas.Rectangle(0,0,Width,Height);
end;
end;
procedure TMyShape.StyleChange(obj: TObject);
begin
//必须这么写,不写assign这一句,不好使
Canvas.Pen.Assign(FPen);
Canvas.Brush.Assign(FBrush);
Invalidate;
end;
procedure TMyShape.SetBrush(const Value: TBrush);
begin
Canvas.Brush.Assign(FBrush);
Invalidate;
end;
procedure TMyShape.SetPen(const Value: TPen);
begin
FPen.Assign(Value);
Invalidate;
end;
procedure TMyShape.SetShape(const Value: TSample);
begin
FShape := Value;
Invalidate;
end;
end.
Delphi控件开发(一)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Xamarin XAML语言教程构建ControlTemplate控件模板 控件模板ControlTemplate...
- 这篇文章主要介绍了iOS开发中UILabel设置字体的相关技巧总结,代码基于传统的Objective-C,需要的朋...
- 评分控件在APP中非常常用,一般常见的形状为星型或者心型(❤)。一般我们比较常用的方法是一个一个图片或者按钮去堆砌...