美文网首页
Delphi控件开发(一)

Delphi控件开发(一)

作者: 风语007 | 来源:发表于2016-11-18 19:39 被阅读0次
    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控件开发(一)

          本文链接:https://www.haomeiwen.com/subject/zkqjpttx.html