开发者

HOw to use local instance of TPrinter

开发者 https://www.devze.com 2022-12-26 20:46 出处:网络
I need to do some printing using TPrinter. The problem is I can not, for various reasons, use Global ob开发者_如何学JAVAject Printer.

I need to do some printing using TPrinter. The problem is I can not, for various reasons, use Global ob开发者_如何学JAVAject Printer.

I want to be able to create my instance of TPrinter and print using that one.

I tried with:

MyPrinter := TPrinter.Create;
MyPrinter.BeginDoc;

but this generates AV.

Any idea what does it take to print something using my instance of TPrinter?

Regards Goran Nagy


If you look at the soucre for printers, the AbortProc uses the global FPrinter object.
You can solve this by call Printer function before caling TPrinter.Create, then it dosn't generate a AccessViolation. It will posibly solve your problem, but MyPrinter.Abort wil NOT propoply not work correcty.

Telling why you can't use the global object, gives other users a posibility to suggest alternative solutions.


The TPrinter object is not really designed to be created locally, but instead is designed to be used from the 'singleton' function Printer in the Printers unit. Generally you would use that.

From the help:

Use TPrinter to manage any printing performed by an application. Obtain an instance of TPrinter by calling the Printer function in the Printers unit.

Edit: Actually, having had a think you could do something like this:

procedure PrintThings;
var
  LMyPrinter: TPrinter;
  LOldPrinter: TPrinter;
begin
  LMyPrinter := TPrinter.Create;
  try
    LOldPrinter := SetPrinter(LMyPrinter);
    try

      // your printing code goes here

    finally
      SetPrinter(LOldPrinter);
    end;
  finally
    LMyPrinter.Free;
  end;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号