Delphi控件开发(一)

2016-11-18  本文已影响0人  风语007
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.
上一篇 下一篇

猜你喜欢

热点阅读