Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How use TPrinter.Setprinter in Delphi?

4,260 views
Skip to first unread message

David

unread,
Dec 26, 1997, 3:00:00 AM12/26/97
to

I would like to set which printer receives the output, using something
like this:

var
Pr : Tprinter;
H : hdc;
begin
Pr := TPrinter.Create;
H := Pr.Handle;
Pr.Setprinter('EPSON Stylus COLOR 600','EPS600','LPT1:',H);
end;

but it produces an access violation. Does anyone knows why?

David.

Dmitriy Mikhelzon

unread,
Dec 26, 1997, 3:00:00 AM12/26/97
to David

David wrote:

David,

1. Do not call TPrinter.Create directly. Use insted Printer variable. In
order to use it, add Printers to uses clause.
2. Also, you probably do not need call SetPrinter method directly. You
could use following two properties of Printer variable: Printer.Printers
and Printer.PrinterIndex.
In this case your code would be like this:

var
PrinterToUse : integer;

PrinterToUse := Printer.Printers.IndexOf('EPSON Stylus COLOR 600');
if PrinterToUse > -1 then
Printer.PrinterIndex := PrinterToUse;

But before I would make shure that description of desired printer in
Printers list is - 'EPSON Stylus COLOR 600'.

Regards,

Dmitriy.


lhirsch

unread,
Dec 27, 1997, 3:00:00 AM12/27/97
to

David wrote:
>
> I would like to set which printer receives the output, using something
> like this:
>
> var
> Pr : Tprinter;
> H : hdc;
> begin
> Pr := TPrinter.Create;
> H := Pr.Handle;
> Pr.Setprinter('EPSON Stylus COLOR 600','EPS600','LPT1:',H);
> end;
>
> but it produces an access violation. Does anyone knows why?
>
> David.

This (below) is what works for me in Delphi 2.01. Your problem may have
resulted from passing Strings instead of PChar or an invalid handle
(H). Actually, If you weren't getting compiler warnings/errors on
passings strings where PChars were expected, then it shouldn't matter.
The handle is created in set printer I think, I've never used it.

I've deleted some code from my actual procedure, so the code below may
not actually compile. I'm not sure why I have to call SetPrinter twice,
but I do for it to consistently work. I think you only have to do that
with NT, not Win 95.

Good Luck.


procedure SetCurrentPrinter;
var MyPrinter : TPrinter;
ADevice, ADriver, APort: array[0..255] of char ;
ADeviceMode: THandle;
begin
StrPCopy(ADevice, 'EPSON Stylus COLOR 600') ;
StrPCopy(ADriver, 'EPS600') ;
StrPCopy(APort, 'LPT1:') ;
MyPrinter.SetPrinter(ADevice, ADriver, APort, 0);
MyPrinter.GetPrinter(ADevice, ADriver, APort, ADeviceMode);
MyPrinter.SetPrinter(ADevice, ADriver, APort, 0);
end ;

Herman Steigstra

unread,
Dec 29, 1997, 3:00:00 AM12/29/97
to

You can save yourself a lot of headacks with trying to do the
programming yourself by having a look at my printer selection combobox.
This component can be dropped on your form and displays as a combobox,
containing all available printers. Additional properties are present for
selecting the correct papersize, papersource orientation etc.
You can find a demo at my homepage at www.skzl.nl/~hsteigstra

Good luck, Herman

Gary Markowski

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

The main problem here is that TPrinter instantiates itself.

Therfore, to use TPrinter, simply include Printers in your uses
clause, and begin using its methods. It will automatically create
itself the first time you use it.

Also, another hint is to use the Canvas property to set the font and
write out text.

Example:

procedure TMain.PrintPage;
var
Prt : TextFile; { Printer port }
begin
AssignPrn(Prt);
try
Rewrite(Prt);
Printer.Canvas.Font.Size := 14; { The Printer object is
auto-instantiated }
Printer.Canvas.Font.Style := [fsBold];
PrintCentered('My Heading Line');
Printer.Canvas.Font.Size := 12;
Printer.Canvas.Font.Style := [];
WriteLn(Prt, 'Line one...');
WriteLn(Prt, 'Line two...');
finally
Printer.Canvas.Font.Style := []; { Reset font style }
System.CloseFile(Prt);
end;
end;

procedure TMain.PrintCentered(Data: string);
var XPos : Integer;
begin
with Printer.Canvas do
begin
{ This example assumes a page 8 inches wide }
XPos := (Trunc(8 * Font.PixelsPerInch) - TextWidth(Data)) Div
2;
if XPos < 0 then
XPos := 0;
Printer.Canvas.TextOut(XPos, PenPos.Y, Data);
MoveTo(0, PenPos.Y + TextHeight(Data));
end;

WriteLn(Prt, ''); { Writes out line feed }
end;

0 new messages