Delphi控件开发(一)

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.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容