var
pinfo: pPRINTERINFO2;
pprt,a: Cardinal;
b:PDWORD;
begin
b:=allocmem(256);
pinfo:=AllocMem(1000);
if OpenPrinter('HP DJ 200',pprt,nil) then
begin
a:=1000;
if getprinter(pprt,2,pinfo,a,b) then
showmessage(pinfo.pPrinterName+' ststus:'+inttostr(pinfo.Status));
ClosePrinter(pprt);
end;
// TPrinterstatus
// Komponente zum Abfragen des aktuellen Druckerportstatus
// Programmiert 2000 von K. Otto
// funktioniert unter Win 9x, jedoch nicht unter Win NT
// ---------------------------------
// Status: Freeware incl. Sourcecode
// ---------------------------------
// Diese Komponente beruht auf einem Beitrag von Robert Vivrette
// f黵 "The unofficial Newsletter of Delphi Users"
// http://www.undu.com/articles/990228a.html
type
TPrinterStatus = class(TComponent)
private
{ Private-Deklarationen }
fStatus : Byte;
fLPT : Integer;
Function GetTimeOut : Boolean;
Function GetIOError : Boolean;
Function GetPrinterSelected : Boolean;
Function GetOutOfPaper : Boolean;
Function GetAcknowledgement : Boolean;
Function GetPrinterBusy : Boolean;
protected
{ Protected-Deklarationen }
public
{ Public-Deklarationen }
Procedure CheckPrinterStatus; // Liest den Druckerstatus der angegeben LPT-Schnittstelle
Constructor Create(AOwner : TComponent); Override;
Property TimeOut : Boolean Read GetTimeOut;
Property IOError : Boolean Read GetIOError;
Property PrinterSelected : Boolean Read GetPrinterSelected;
Property OutOfPaper : Boolean Read GetOutOfPaper;
Property Acknowledgement : Boolean Read GetAcknowledgement;
Property Busy : Boolean Read GetPrinterBusy;
published
{ Published-Deklarationen }
Property LPT : Integer Read fLPT Write fLPT;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Eigene', [TPrinterStatus]);
end;
Function TPrinterStatus.GetTimeOut : Boolean;
Begin
Result:=(fStatus and $01)=$01;
End;
Function TPrinterStatus.GetIOError : Boolean;
Begin
Result:=(fStatus and $08)=$08;
End;
Function TPrinterStatus.GetPrinterSelected : Boolean;
Begin
Result:=(fStatus and $10)=$10;
End;
Function TPrinterStatus.GetOutOfPaper : Boolean;
Begin
Result:=(fStatus and $20)=$20;
End;
Function TPrinterStatus.GetAcknowledgement : Boolean;
Begin
Result:=(fStatus and $40)=$40;
End;
Function TPrinterStatus.GetPrinterBusy : Boolean;
Begin
Result:=not ((fStatus and $80)=$80);
End;
Procedure TPrinterStatus.CheckPrinterStatus;
Var
Status : Byte;
CheckLPT : Word;
Begin
Status:=0;
If (fLPT>=1) and (fLPT<=3) Then
Begin
CheckLPT:=fLPT-1;
asm
mov dx,CheckLPT;
mov al,0;
mov ah,2;
int 17h;
mov &Status, ah;
End;
End;
fStatus:=Status;
End;
Constructor TPrinterStatus.Create(AOwner : TComponent);
Begin
Inherited Create(AOwner);
fLPT:=1;
fStatus:=0;
End;
function TPrinterStatus.PrinterReady(LPT: Word): boolean;
var
ErrorCode, C : BYTE;
code, x : integer;
s : string;
function GetPrinterStatus (LPT: Word): Byte;
{Pass 1 in LPT for LPT1}
begin
asm
mov ah,2
mov dx,LPT
dec dx
int $17
mov @Result,ah
end;
end; {GetPrinterStatus}
begin
result := false; //assume not
FPort := LPT;
if FPort = 0 then begin {if no port specified then try to set port to current
printer port}
{printer name}
s := Printer.Printers[Printer.PrinterIndex];
if Pos('FPort',s) <> 0 then begin
s := Copy(s, Pos('FPort',s) +3, 1);
Val(s,x,code);
if code <> 0 then FPort := 1 else FPort := x;
end else FPort := 1; {default to LPT1}
end;
{valid LPT is 1..4}
if (FPort > 4) or (FPort < 1) then begin
raise ERangeError.CreateFmt(
'LPT%d is not within the valid range of %d..%d',
[FPort, 1, 4]);
exit;
end;
ErrorCode := GetPrinterStatus(FPort);
ErrorCode := ErrorCode and $B0; {NEW LINE}
C := ERRORCODE shl 6; {ALWAYS MEANS NOTHING CONNECTED}
if C > 0 then ERRORCODE := $B0; {ELEMINATES NO LPT3 AND NOTHING CONNECTED}
case ErrorCode of
PrnReady : begin FStatusStr := 'Printer Ready'; result := true;
end;
NoConnect : FStatusStr := 'Printer not connected';
Offline,OffLine2 : FStatusStr := 'Printer off line'; {Modified}
PaperOut,PaperOut2 : FStatusStr := 'Printer out of paper'; {Modified}
HookedButOff : FStatusStr := 'Printer connected but turned off'; {New}
else
FStatusStr := 'Printer error code: ' + IntToStr(ErrorCode);
end;
procedure TForm1.ButtonClick(Sender: Tobject);
begin
ComboBox1.Clear;
ComboBox1.Items.Assign(Printer.Printers);
if ComboBox1.Items.CommaText='' then
Messagedlg('你需要安装打印机才能打印!',mtError,[mbOk],0);
else
Form1.Print;
end;
如果你想要改变Windows的缺省打印机,你必须在Windows的WIN.INI中改变设备键。
发送一条 WM_WININICHANGE 消息:
var
strIni: array[0..MAX_PATH] of Char;
arrWindows: array[0..64] of Char;
begin
GetWindowsDirectory(strIni, SizeOf(strIni));
StrCat(strIni, '\win.ini');
with TIniFile.Create(strIni) do
try
WriteString('windows', 'device', 'HP LaserJet 4 Plus,HPPCL5MS,LPT1:');
finally
Free;
end;
Use TPrinter to manage any printing performed by an application. Obtain an instance of TPrinter by calling the Printer function in the Printers unit.
To determine how the printed image of the form appears, use the PrintScale property of a TForm component.
A print job is started by a call to BeginDoc. The application sends commands by rendering through a Text variable or the printer’s canvas. You can move to a new page by calling the NewPage method. The job stays open until the application calls EndDoc. If a problem occurs and you need to terminate a print job that was not sent to the printer successfully, call the Abort method.
Use the TPrinter properties to configure the print job. For example, the title displayed in the Print Manager (and on network header pages) is determined by the Title property. Copies determines the number of copies to print, and Orientation lets you specify whether to print in portrait or landscape mode.
TPrinter includes several read-only properties as well, that let you determine which page is currently being printed, the fonts available on the printer, the paper size, and so on.