Can anybody explain how this should work (Delphi XE2 ruing on Windows10)?
In a minimalist application - a form with two buttons - with the following code:
procedure TForm2.Button1Click(Sender: TObject);
begin
Printer.BeginDoc;
Printer.Canvas.TextOut(10,10,'Hello World');
Printer.EndDoc;
end;
procedure TForm2.Button2Click(Sender: TObject);
var
MyPrinter : TPrinter;
begin
MyPrinter := Printer;
MyPrinter.BeginDoc;
MyPrinter.Canvas.TextOut(10,10,'Hello World');
MyPrinter.EndDoc;
MyPrinter.Free;
end;
If I click Button1, the program prints a 'Hello World' at my default printer, and closes normally when I close it (with the close button). If instead, I click Button2, the program prints an identical page but now when I close with the close button I get an Error 217 message.
I can't find clarification in the Delphi documentation regarding exactly how to use the Printer function and TPrinter variables. I am quite happy using a derivative of the Button1 technique to print - if it is confirmed that this is the 'bullet-proof' way to go, but would like to understand why the Button2 method doesn't work. Should I just assume that it is not my responsibility to free any TPrinter object I instantiate, or is there a clearer explanation?
