RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TMetafile.Create Constructor

Instantiates a metafile object.

Pascal
constructor Create; override;
C++
virtual __fastcall TMetafile();

Call Create to instantiate a metafile at runtime. 

Create allocates memory for a metafile object, and calls the inherited Create. Then sets the Enhanced and Transparent properties to true. 

To create a metafile image from scratch, draw the image in a metafile canvas. When the metafile canvas is destroyed, it transfers the image into the metafile object provided to the metafile canvas constructor. After the image is drawn on the canvas and the canvas is destroyed, the image is 'playable' in the metafile 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.
*/
#include <memory>       //for STL auto_ptr class

TMetafile *MyMetafile;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  static std::auto_ptr<TMetafile> _MyMetafileCleaner(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; // TMetaFile canvas paints in the destructor.
  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) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!