RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TMetafileCanvas.Create Constructor

Creates an instance of TMetafileCanvas and acquires a handle to a metafile device context.

Pascal
constructor Create(AMetafile: TMetafile; ReferenceDevice: HDC);
C++
__fastcall TMetafileCanvas(TMetafile AMetafile, HDC ReferenceDevice);

Call Create to create an instantiate a TMetafileCanvas object. Pass in the associated metafile whose image is about to be drawn as the AMetafile parameter. Pass in a handle to a device context on which to base the metafile's device context as the ReferenceDevice parameter. In most cases, this is the device context for a window on which the image is normally drawn.  

Create sets the size of the TMetafile object from ReferenceDevice if it does not already have the MMHeight and MMWidth properties set. Create then creates a metafile device context, and sets the Handle property to its handle. All subsequent drawing methods will draw to the metafile device context. 

When the TMetafileCanvas is destroyed, The CreatedBy and Description strings will be used to set the properties of the same name in the TMetafile object.  

C++ Examples: 

 

/*
This example shows how to create or augment a metafile using
a metafile canvas object.  This metafile can then be used to
draw on the canvas of another object such as a paintbox or a
printer.
*/
TMetafile *MyMetafile;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
MyMetafile = new TMetafile;
TMetafileCanvas *canvas = new TMetafileCanvas(MyMetafile, 0);
canvas->Brush->Color = clRed;
canvas->Ellipse(0, 0, 100, 200);
// ...
delete canvas;
Form1->Canvas->Draw(0, -50, MyMetafile); //1 red circle
PaintBox1->Canvas->Draw(0, 0, MyMetafile); //1 red circle
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  if (MyMetafile == NULL) return;   // click Button1 first!
  TMetafileCanvas *canvas = new TMetafileCanvas(MyMetafile, 0);
  canvas->Draw(-50, 0, MyMetafile);
  canvas->Brush->Color = clBlue;
  canvas->Ellipse(100, 100, 300, 200);
  // ...
  delete canvas;
  Form1->Canvas->Draw(0, 0, MyMetafile); // 1 red circle and 1 blue circle
  PaintBox1->Canvas->Draw(0, 0, MyMetafile); // 1 red circle and 1 blue circle
}

 

Delphi Examples: 

{
This example shows how to create or augment a metafile using
a metafile canvas object.  This metafile can then be used to
draw on the canvas of another object such as a paintbox or a
printer.
}

var
  MyMetafile: TMetafile;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyMetafile := TMetafile.Create;
  with TMetafileCanvas.Create(MyMetafile, 0) do
  try
    Brush.Color := clRed;
    Ellipse(0, 0, 100, 200);
    //  ...
  finally
    Free;
  end;
  Form1.Canvas.Draw(0, 0, MyMetafile); {1 red circle }
  PaintBox1.Canvas.Draw(0, -50, MyMetafile); {1 red circle }
end;

{
To add to an existing metafile image, create a metafile
canvas and play the source metafile into the metafile canvas:
}
procedure TForm1.Button2Click(Sender: TObject);
begin
  if (MyMetafile = nil) then exit;  { click Button1 first! }
  
  with TMetafileCanvas.Create(MyMetafile, 0) do
  try
    Draw(-50, 0, MyMetafile);
    Brush.Color := clBlue;
    Ellipse(100, 100, 300, 200);
// ...
  finally
    Free;
  end;
  Form1.Canvas.Draw(0, 0, MyMetafile); {1 red circle and 1 blue circle }
  PaintBox1.Canvas.Draw(0, 0, MyMetafile); {1 red circle and 1 blue circle }
end;

 

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