TMetafileCreate (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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.

Code

#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); // One red circle
  PaintBox1->Canvas->Draw(0, 0, MyMetafile); // One 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); // One red circle and one blue circle
  PaintBox1->Canvas->Draw(0, 0, MyMetafile); // One red circle and one blue circle
}

Uses