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?
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;