RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCanvas.CopyRect Method

Copies part of an image from another canvas into the canvas.

Pascal
procedure CopyRect(const Dest: TRect; Canvas: TCanvas; const Source: TRect);
C++
__fastcall CopyRect(const TRect Dest, TCanvas Canvas, const TRect Source);

Use CopyRect to transfer part of the image on another canvas to the image of the TCanvas object. Dest specifies the rectangle on the canvas where the source image will be copied. The Canvas parameter specifies the canvas with the source image. Source specifies a rectangle bounding the portion of the source canvas that will be copied. 

The portion of the source canvas is copied using the mode specified by CopyMode.  

C++ Examples: 

 

/*
The following code illustrates the differences between 
CopyRect and BrushCopy. The bitmap graphic ‘FACTORY.BMP’ is
loaded into Bitmap and displayed on the Canvas of Form1. 
BrushCopy replaces the color black in the graphic with the 
brush of the canvas, while CopyRect leaves the colors intact.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Graphics::TBitmap *Bitmap;
TRect   MyRect, MyOther;
MyRect = Rect(10,10,100,100);
MyOther = Rect(10,111,100, 201);
Bitmap = new Graphics::TBitmap;
Bitmap->LoadFromFile("c:/Program Files/Common Files/CodeGear Shared/Images/Splash/256color/factory.bmp");
Form1->Canvas->BrushCopy(MyRect, Bitmap, MyRect, clBlack);
Form1->Canvas->CopyRect(MyOther, Bitmap->Canvas, MyRect);
delete Bitmap;
}
/*
The following example uses the CopyMode of an image’s canvas
to blank out the image when the user chooses the "Cut" menu
item.  This example requires a Main menu with a Copy and a
Cut menu item that have their OnClick error handlers populated.
Note: CopyMode and CopyRect are members of the destination
canvas.
*/
void __fastcall TForm1::Copy1Click(TObject *Sender)
{
    TRect SrcRect, DstRect;
    Image2->Canvas->CopyMode = cmSrcCopy;
    DstRect = Rect(0, 0, Image2->Width, Image2->Height);
    SrcRect = Rect(0, 0, Image1->Width, Image1->Height);
    Image2->Canvas->CopyRect(DstRect, Image1->Canvas, SrcRect);
}

void __fastcall TForm1::Cut1Click(TObject *Sender)
{
 TRect r;
 Copy1Click(Sender); // Do the same thing as the copy menu item.
 Image1->Canvas->CopyMode = cmWhiteness;
 r = Rect(0, 0, Image1->Width, Image1->Height);
 Image1->Canvas->CopyRect(r, Image1->Canvas, r);
 Image1->Canvas->CopyMode = cmSrcCopy; // Restore the copy mode.
}

 

Delphi Examples: 

{
The following code illustrates the differences between 
CopyRect and BrushCopy. The bitmap graphic ‘FACTORY.BMP’ is
loaded into Bitmap and displayed on the Canvas of Form1. 
BrushCopy replaces the color black in the graphic with the 
brush of the canvas, while CopyRect leaves the colors intact.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  Bitmap: TBitmap;
  MyRect, MyOther: TRect;
begin
  MyRect := Rect(10,10,150,150);
  MyOther := Rect(10,161,150,301);
  Bitmap := TBitmap.Create;
  Bitmap.LoadFromFile(
    'c:\Program Files\Common Files\CodeGear Shared\Images\Splash\256color\factory.bmp');
  Form1.Canvas.BrushCopy(MyRect, Bitmap, MyRect, clBlack);
  Form1.Canvas.CopyRect(MyOther,Bitmap.Canvas,MyRect);
  Bitmap.Free;
end;
{
The following example uses the CopyMode of an image’s canvas
to blank out the image when the user chooses the "Cut" menu 
item.  This example requires a Main menu with a Copy and a
Cut menu item that have their OnClick error handlers populated.
Note: CopyMode and CopyRect are members of the destination
canvas.
}
procedure TForm1.Copy1Click(Sender: TObject);
var
  DstRect, SrcRect: TRect;
begin
  with Image2.Canvas do
  begin
    CopyMode := cmSrcCopy;
    DstRect := Rect(0, 0, Image2.Width, Image2.Height);
    SrcRect := Rect(0, 0, Image1.Width, Image1.Height);
    CopyRect(DstRect, Image1.Canvas, SrcRect);
  end;
end;

procedure TForm1.Cut1Click(Sender: TObject);
var
  ARect: TRect;
begin
  Copy1Click(Sender); { do the same thing as the copy menu item }
  with Image1.Canvas do
  begin
    CopyMode := cmWhiteness;
    ARect := Rect(0, 0, Image1.Width, Image1.Height);
    CopyRect(ARect, Image1.Canvas, ARect);
    CopyMode := cmSrcCopy; { restore the copy mode }
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!