打印本文 打印本文 关闭窗口 关闭窗口
组件制作之三(图形控件)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1960  更新时间:2009/4/23 18:34:08  文章录入:mintao  责任编辑:mintao
;

    property Brush: TBrush read FBrush write SetBrush;

    property DragCursor;

    property DragKind;

    property DragMode;

    property Enabled;

    property Constraints;

    property ParentShowHint;

    property Pen: TPen read FPen write SetPen;

    property Shape: TShapeType read FShape write SetShape default stRectangle;

    property ShowHint;

    property Visible;

    property OnContextPopup;

    property OnDragDrop;

    property OnDragOver;

    property OnEndDock;

    property OnEndDrag;

    property OnMouseDown;

    property OnMouseMove;

    property OnMouseUp;

    property OnStartDock;

    property OnStartDrag;

  end;

 

implementation

 

//构造函数,ControlStyle确定控件的特征,这里是加上csReplicatable

//使该控件可以用PaintTo方法把它画到一个任意的画布中

//另外还指定Pen和Brush对象的OnChange事件的方法指针给StyleChanged;

constructor TShapeEx.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 TShapeEx.Destroy;

begin

  FPen.Free;

  FBrush.Free;

  inherited Destroy;

end;

//最重要函数,控件就是以这个函数画出来的。

procedure TShapeEx.Paint;

var

//X:左上角X, Y:左上角Y, W:宽, Y:高  S:宽高中大的值

  X, Y, W, H, S: Integer;

begin

//Canvas是父类TGraphicControl的属性,用于画控件

//在画图之前,要进行一些坐标点的确定,

  with Canvas do

  begin

  //如果以控件的四个角来画图形,当Pen太大时,图形的边线

  //将比Pen的宽要小一些,所以要进行点的重定位。

    Pen := FPen;

    Brush := FBrush;

    X := Pen.Width div 2;

    Y := X;

    W := Width - Pen.Width + 1;

    H := Height - Pen.Width + 1;

    //Pen.width的值为0和为1时是一样的等于一个象素

    //如果上面Pen.width等于1,则图形的宽高刚好等于控件的宽高

    //如果为0,则图形宽高要大于控件宽高一个象素了,所以有了下面的语句

    if Pen.Width = 0 then

    begin

      Dec(W);

      Dec(H);

    end;

    if W < H then S := W else S := H;

    //当图形为正方类型的图形时,需要对图形的位置进行一些调整

    if FShape in [stSquare, stRoundSquare, stCircle] then

    begin

      Inc(X, (W - S) div 2);

      Inc(Y, (H - S) div 2);

      W := S;

    

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口