RAD Studio VCL Reference
|
Provides the drawing surface used by the graphic control.
property Canvas: TCanvas;
__property TCanvas Canvas;
Use the properties of the TCanvas object to draw or paint on the surface of the control.
Since a TGraphicControl instance does not have its own Windows screen object, it obtains its device context from its parent control.
C++ Examples:
/* When you do custom drawing, such as shown in the code for the Button's OnClick event handler below, this drawing will not be persistent and will be "erased" the first time the PaintBox component needs to repaint. */ void __fastcall TForm1::Button1Click(TObject *Sender) { PaintBox1->Canvas->Brush->Color = clRed; PaintBox1->Canvas->FillRect(PaintBox1->Canvas->ClipRect); PaintBox1->Canvas->Ellipse(0,0,100,100); } /* In order for the drawing to be persistent, you need to also include an OnPaint event handler that tells the PaintBox how to redraw itself when it needs to. In the code below, an ellipse will be drawn everytime the PaintBox renders itself, but the PaintBox will only paint the client area of the control red after the Button is clicked. This client area will only stay red until it is invalidated, but the ellipse will persist. This behavior is different from the TImage component, because the TImage component maintains an internal bitmap that stores this drawing information for you. */ void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { PaintBox1->Canvas->Brush->Color = clBlue; PaintBox1->Canvas->Ellipse(0,0,100,100); }
/* The following code uses ClientRect to find and draw a line from the top left to the bottom right of the current control. Select a TControl in the ListBox list. This example assumes that the current object is a descendent of TControl and has a public Canvas property. */ { TPoint APoint = Point(X, Y); int Index = ListBox1->ItemAtPos(APoint, True); TLabel *myLabel; TClass ClassRef = ListBox1->Items->Objects[Index]->ClassType(); if (String(ClassRef->ClassName()) == "TLabel") { myLabel = dynamic_cast<TLabel *>(ListBox1->Items->Objects[Index]); // make sure we are using the TControls canvas and not the from's! myLabel->Canvas->MoveTo(myLabel->ClientRect.Left, myLabel->ClientRect.Top); myLabel->Canvas->LineTo(myLabel->ClientRect.Right, myLabel->ClientRect.Bottom); myLabel = NULL; } }
/* The following code draws the graphic in Picture1 in the top-left corner PaintBox1. Place a TPaintBox in the form and a TImage in the form that contains a picture. */ void __fastcall TForm1::Button1Click(TObject *Sender) { PaintBox1->Canvas->Draw(0,0, Image1->Picture->Graphic); }
Delphi Examples:
{ When you do custom drawing, such as shown in the code for the Button's OnClick event handler below, this drawing will not be persistent and will be "erased" the first time the PaintBox component needs to repaint. } procedure TForm1.Button1Click(Sender: TObject); begin PaintBox1.Canvas.Brush.Color := clRed; PaintBox1.Canvas.FillRect(PaintBox1.Canvas.ClipRect); PaintBox1.Canvas.Brush.Color := clBlue; PaintBox1.Canvas.Ellipse(0,0,100,50); end; { In order for the drawing to be persistent, you need to also include an OnPaint event handler that tells the PaintBox how to redraw itself when it needs to. In the code below, an ellipse will be drawn everytime the PaintBox renders itself, but the PaintBox will only paint the client area of the control red after the Button is clicked. This client area will only stay red until it is invalidated, but the ellipse will persist. This behavior is different from the TImage component, because the TImage component maintains an internal bitmap that stores this drawing information for you. } procedure TForm1.PaintBox1Paint(Sender: TObject); begin PaintBox1.Canvas.Brush.Color := clBlue; PaintBox1.Canvas.Ellipse(0,0,100,50); end;
{ The following code uses ClientRect to find and draw a line from the top left to the bottom right of the current control. Select a TControl in the ListBox list. This example assumes that the current object is a descendent of TControl and has a public Canvas property. } if (ListBox1.Items.Objects[Index] is TLabel) then begin myLabel:= (ListBox1.Items.Objects[Index] as TLabel); with myLabel.ClientRect do begin // make sure we are using the TControl's canvas and not the form's! myLabel.Canvas.MoveTo(Left, Top); myLabel.Canvas.LineTo(Right, Bottom); end; myLabel:= nil; end;
{ The following code draws the graphic in Picture1 in the top-left corner PaintBox1. Place a TPaintBox in the form and a TImage in the form that contains a picture. } procedure TForm1.Button1Click(Sender: TObject); begin PaintBox1.Canvas.Draw(0,0, Image1.Picture.Graphic); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|