0

I intend to use TMetafileCanvas so I have started to find example. On Embarcadero side I have find the following example:

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;

I have created a new project and put on the Form, Button and PaintBox, then I have copy upper example, but nothing is happened when the code is executed and the form stay the same!

Evidently I'm doing something wrong! What I have to do that example should work correct?

GJ.
  • 10,810
  • 2
  • 45
  • 62
  • @David Heffernan: this is a copy of Embarcadero example, nothing more or less. Check my link to an example! – GJ. Mar 18 '14 at 17:04
  • 1
    Just uncomment Free. This example is not quite correct, because TMetafileCanvas destructor executes important tasks before destroying. – MBo Mar 18 '14 at 17:10
  • The code you linked to what written by somebody with no idea what they are doing. It's appalling the Emba put their name to tripe like that. – David Heffernan Mar 18 '14 at 17:15

1 Answers1

4

The MetaFile doesn't update itself until you free the MetaFileCanvas. (The code you posted actually shows that, but the call to Free has been commented out.)

Embarcadero's example is wrong in another sense, too. All painting to the form should be done in the OnPaint event, not from anywhere else. (I blame that on much of the documentation sample code having been contributed by users, and it's only reviewed by the documentation team and not the development team, AFAICT.)

procedure TForm1.FormPaint(Sender: TObject);
var
  MetaFile: TMetafile;
  MFCanvas: TMetafileCanvas;
begin
  MetaFile := TMetafile.Create;
  try
    MetaFile.SetSize(200, 200);
    try
      MFCanvas := TMetafileCanvas.Create(MetaFile, Canvas.Handle);
      MFCanvas.Brush.Color := clRed;
      MFCanvas.FloodFill(0, 0, clRed, fsBorder);
      MFCanvas.Rectangle(10, 10, 190, 190);
    finally
      MFCanvas.Free;
    end;
    Self.Canvas.StretchDraw(Rect(0, 0, 200, 200), MetaFile);
  finally
    MetaFile.Free;
  end;
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444