Delphi 2010 - 如何复制和清除TShape

时间:2012-08-21 18:13:19

标签: delphi canvas delphi-2010 shape

好的,在使用TShape之后,我需要从Lines和Text中清除我的“Shape1”。

还有如何将“Shape1”中的所有内容复制到“Shape2”中?

谢谢B4 ^ o ^

    type
      TShape = class(ExtCtrls.TShape); //interposer class

      TForm1 = class(TForm)
        Shape1: TShape;
        Shape2: TShape;
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
      public
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
//        Draw some text on Shape1 := TShape 
        Shape1.Canvas.Font.Name :='Arial';// set the font 
        Shape1.Canvas.Font.Size  :=20;//set the size of the font
        Shape1.Canvas.Font.Color:=clBlue;//set the color of the text
        Shape1.Canvas.TextOut(10,10,'1999');
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
//        Copy everything from Shape1 to Shape2 (make a duplication)
//        How to do it ? 
        showmessage('copy Shape1 into Shape2');    
    end;

    End.

1 个答案:

答案 0 :(得分:4)

以下伪代码会将SourceShape画布内容的副本复制到TargetShape画布,但只有在TargetShape刷新之前:

procedure TForm1.Button1Click(Sender: TObject);
begin
  TargetShape.Canvas.CopyRect(Rect(0, 0, TargetShape.ClientWidth,
    TargetShape.ClientHeight), SourceShape.Canvas, Rect(0, 0,
    SourceShape.ClientWidth, SourceShape.ClientHeight));
end;

清除以前复制的内容,您可以使用以下内容:

procedure TForm1.Button2Click(Sender: TObject);
begin
  TargetShape.Invalidate;
end;

要保持绘图的持久性,您需要实现自己的OnPaint事件,无论何时触发,都会使用上面显示的CopyRect方法将当前画布内容从源复制到目标。 / p>

但问题是,为什么要使用TShape控件呢?最好使用TPaintBox并自己绘制你的东西,包括由TShape控件绘制的形状。