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.
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.
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 ;
Good luck, Herman
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;