Win98下如何得到打印机的状态?(50分)

  • 主题发起人 唐纳王
  • 开始时间

唐纳王

Unregistered / Unconfirmed
GUEST, unregistred user!
请教:在Win98下如何得到打印机的状态,即是否联机以及打印正常与否?
 
From Hotdog2.5<br>var<br>&nbsp; pinfo: pPRINTERINFO2;<br>&nbsp; pprt,a: Cardinal;<br>&nbsp; b:pDWORD;<br>begin<br>&nbsp; b:=allocmem(256);<br>&nbsp; pinfo:=AllocMem(1000);<br>&nbsp; if OpenPrinter('HP DJ 200',pprt,nil) then<br>&nbsp; begin<br>&nbsp; &nbsp; a:=1000;<br>&nbsp; &nbsp; if getprinter(pprt,2,pinfo,a,b) then<br>&nbsp; &nbsp; &nbsp; showmessage(pinfo.pPrinterName+' ststus:'+inttostr(pinfo.Status));<br>&nbsp; ClosePrinter(pprt);<br>&nbsp; end;<br>&nbsp; <br>&nbsp; freemem(b);<br>&nbsp; freemem(pinfo);<br>end;<br><br>PRINTER_INFO_2.Status :<br>&nbsp;Windows NT: <br>&nbsp; &nbsp;PRINTER_STATUS_PAUSED<br>&nbsp; &nbsp;PRINTER_STATUS_PENDING_DELETION <br>&nbsp;Windows 95: <br>&nbsp; &nbsp;PRINTER_STATUS_BUSY<br>&nbsp; &nbsp;PRINTER_STATUS_DOOR_OPEN<br>&nbsp; &nbsp;PRINTER_STATUS_ERROR<br>&nbsp; &nbsp;PRINTER_STATUS_INITIALIZING<br>&nbsp; &nbsp;PRINTER_STATUS_IO_ACTIVE<br>&nbsp; &nbsp;PRINTER_STATUS_MANUAL_FEED<br>&nbsp; &nbsp;PRINTER_STATUS_NO_TONER<br>&nbsp; &nbsp;PRINTER_STATUS_NOT_AVAILABLE<br>&nbsp; &nbsp;PRINTER_STATUS_OFFLINE<br>&nbsp; &nbsp;PRINTER_STATUS_OUT_OF_MEMORY<br>&nbsp; &nbsp;PRINTER_STATUS_OUTPUT_BIN_FULL<br>&nbsp; &nbsp;PRINTER_STATUS_PAGE_PUNT<br>&nbsp; &nbsp;PRINTER_STATUS_PAPER_JAM<br>&nbsp; &nbsp;PRINTER_STATUS_PAPER_OUT<br>&nbsp; &nbsp;PRINTER_STATUS_PAPER_PROBLEM<br>&nbsp; &nbsp;PRINTER_STATUS_PAUSED<br>&nbsp; &nbsp;PRINTER_STATUS_PENDING_DELETION<br>&nbsp; &nbsp;PRINTER_STATUS_PRINTING<br>&nbsp; &nbsp;PRINTER_STATUS_PROCESSING<br>&nbsp; &nbsp;PRINTER_STATUS_TONER_LOW<br>&nbsp; &nbsp;PRINTER_STATUS_UNAVAILABLE<br>&nbsp; &nbsp;PRINTER_STATUS_USER_INTERVENTION<br>&nbsp; &nbsp;PRINTER_STATUS_WAITING<br>&nbsp; &nbsp;PRINTER_STATUS_WARMING_UP <br>//////////////////////////////////////////////////////////////////<br>unit PrinterStatus;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, WinProcs, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br><br>// TPrinterstatus<br>// Komponente zum Abfragen des aktuellen Druckerportstatus<br>// Programmiert 2000 von K. Otto<br>// funktioniert unter Win 9x, jedoch nicht unter Win NT<br>// ---------------------------------<br>// Status: Freeware incl. Sourcecode<br>// ---------------------------------<br>// Diese Komponente beruht auf einem Beitrag von Robert Vivrette<br>// f黵 "The unofficial Newsletter of Delphi Users"<br>// http://www.undu.com/articles/990228a.html<br><br>type<br>&nbsp; TPrinterStatus = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; { Private-Deklarationen }<br>&nbsp; &nbsp; fStatus : Byte;<br>&nbsp; &nbsp; fLPT &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp; Function GetTimeOut : Boolean;<br>&nbsp; &nbsp; Function GetIOError : Boolean;<br>&nbsp; &nbsp; Function GetPrinterSelected : Boolean;<br>&nbsp; &nbsp; Function GetOutOfPaper : Boolean;<br>&nbsp; &nbsp; Function GetAcknowledgement : Boolean;<br>&nbsp; &nbsp; Function GetPrinterBusy : Boolean;<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected-Deklarationen }<br>&nbsp; public<br>&nbsp; &nbsp; { Public-Deklarationen }<br>&nbsp; &nbsp; Procedure CheckPrinterStatus; // Liest den Druckerstatus der angegeben LPT-Schnittstelle<br>&nbsp; &nbsp; Constructor Create(AOwner : TComponent); Override;<br>&nbsp; &nbsp; Property TimeOut : Boolean Read GetTimeOut;<br>&nbsp; &nbsp; Property IOError &nbsp;: Boolean Read GetIOError;<br>&nbsp; &nbsp; Property PrinterSelected : Boolean Read GetPrinterSelected;<br>&nbsp; &nbsp; Property OutOfPaper : Boolean Read GetOutOfPaper;<br>&nbsp; &nbsp; Property Acknowledgement : Boolean Read GetAcknowledgement;<br>&nbsp; &nbsp; Property Busy : Boolean Read GetPrinterBusy;<br>&nbsp; published<br>&nbsp; &nbsp; { Published-Deklarationen }<br>&nbsp; &nbsp; Property LPT : Integer Read fLPT Write fLPT;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Eigene', [TPrinterStatus]);<br>end;<br><br>Function TPrinterStatus.GetTimeOut : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=(fStatus and $01)=$01;<br>End;<br><br>Function TPrinterStatus.GetIOError : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=(fStatus and $08)=$08;<br>End;<br><br>Function TPrinterStatus.GetPrinterSelected : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=(fStatus and $10)=$10;<br>End;<br><br>Function TPrinterStatus.GetOutOfPaper : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=(fStatus and $20)=$20;<br>End;<br><br>Function TPrinterStatus.GetAcknowledgement : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=(fStatus and $40)=$40;<br>End;<br><br>Function TPrinterStatus.GetPrinterBusy : Boolean;<br>Begin<br>&nbsp; &nbsp; &nbsp;Result:=not ((fStatus and $80)=$80);<br>End;<br><br>Procedure TPrinterStatus.CheckPrinterStatus;<br>Var<br>&nbsp; &nbsp;Status : Byte;<br>&nbsp; &nbsp;CheckLPT : Word;<br>Begin<br>&nbsp; &nbsp; &nbsp;Status:=0;<br>&nbsp; &nbsp; &nbsp;If (fLPT&gt;=1) and (fLPT&lt;=3) Then<br>&nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckLPT:=fLPT-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asm<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov dx,CheckLPT;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov al,0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov ah,2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int 17h;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov &amp;Status, ah;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp;End;<br>&nbsp; &nbsp; &nbsp;fStatus:=Status;<br>End;<br><br>Constructor TPrinterStatus.Create(AOwner : TComponent);<br>Begin<br>&nbsp; &nbsp; &nbsp;Inherited Create(AOwner);<br>&nbsp; &nbsp; &nbsp;fLPT:=1;<br>&nbsp; &nbsp; &nbsp;fStatus:=0;<br>End;<br><br>end.<br>//////////////////////////////////////////////////////<br>用法:<br><br>&nbsp;if not PrinterStatus1.PrinterReady(0) then &nbsp;//0 = current printerport<br>&nbsp; ShowMessage(PrinterStatus1.StatusMsg) else {print print print} ;<br><br>unit PrinterStatus;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br>&nbsp; TPrinterStatus = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; FPort : Word;<br>&nbsp; &nbsp; FStatusStr : string;<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; function PrinterReady(LPT: Word): boolean;<br>&nbsp; published<br>&nbsp; &nbsp; { Published declarations }<br>&nbsp; &nbsp; property StatusMsg: string read FStatusStr;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br>uses Printers;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Win95', [TPrinterStatus]);<br>end;<br><br>const<br>&nbsp; PrnReady = $90;<br>&nbsp; OffLine = $00;<br>&nbsp; OffLine2 = $10; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {NEW LINE}<br>&nbsp; PaperOut = $20;<br>&nbsp; PaperOut2 = $30; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{NEW LINE}<br>&nbsp; HookedButOff = $80; &nbsp; &nbsp; &nbsp; &nbsp; {NEW LINE}<br>&nbsp; NoConnect = $B0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{MODIFIED LINE}<br><br>&nbsp; {NOCONNECT = $30 FOR SOME COMPUTERS BY STU}<br><br>function TPrinterStatus.PrinterReady(LPT: Word): boolean;<br>var<br>&nbsp; ErrorCode, C : BYTE;<br>&nbsp; code, x : integer;<br>&nbsp; s : string;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function GetPrinterStatus (LPT: Word): Byte;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{Pass 1 in LPT for LPT1}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;asm<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mov ah,2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mov dx,LPT<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dec dx<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int $17<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mov @Result,ah<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; &nbsp;{GetPrinterStatus}<br><br><br>begin<br>&nbsp;result := false; &nbsp;//assume not<br><br>&nbsp;FPort := LPT;<br>&nbsp;if FPort = 0 then begin &nbsp;{if no port specified then try to set port to current<br>printer port}<br>&nbsp; &nbsp;{printer name}<br>&nbsp; &nbsp;s := Printer.Printers[Printer.PrinterIndex];<br>&nbsp; &nbsp;if Pos('FPort',s) &lt;&gt; 0 then begin<br>&nbsp; &nbsp; &nbsp;s := Copy(s, Pos('FPort',s) +3, 1);<br>&nbsp; &nbsp; &nbsp;Val(s,x,code);<br>&nbsp; &nbsp; &nbsp;if code &lt;&gt; 0 then FPort := 1 else FPort := x;<br>&nbsp; &nbsp;end else FPort := 1; &nbsp;{default to LPT1}<br>&nbsp;end;<br><br>&nbsp;{valid LPT is 1..4}<br>&nbsp;if (FPort &gt; 4) or (FPort &lt; 1) then begin<br>&nbsp; &nbsp;raise ERangeError.CreateFmt(<br>&nbsp; &nbsp; &nbsp;'LPT%d is not within the valid range of %d..%d',<br>&nbsp; &nbsp; &nbsp;[FPort, 1, 4]);<br>&nbsp; &nbsp;exit;<br>&nbsp;end;<br><br><br>&nbsp;ErrorCode := GetPrinterStatus(FPort);<br><br>&nbsp;ErrorCode := ErrorCode and $B0; &nbsp; &nbsp; &nbsp; {NEW LINE}<br><br>&nbsp;C := ERRORCODE shl 6; &nbsp; {ALWAYS MEANS NOTHING CONNECTED}<br><br>&nbsp;if C &gt; 0 then ERRORCODE := $B0; {ELEMINATES NO LPT3 AND NOTHING CONNECTED}<br><br>&nbsp;case ErrorCode of<br>&nbsp; PrnReady &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: begin FStatusStr := 'Printer Ready'; result := true;<br>end;<br>&nbsp; NoConnect &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : FStatusStr := 'Printer not connected';<br>&nbsp; Offline,OffLine2 &nbsp; &nbsp;: FStatusStr := 'Printer off line'; &nbsp; &nbsp; {Modified}<br>&nbsp; PaperOut,PaperOut2 &nbsp;: FStatusStr := 'Printer out of paper'; {Modified}<br>&nbsp; HookedButOff &nbsp; &nbsp; &nbsp; &nbsp;: FStatusStr := 'Printer connected but turned off'; {New}<br>&nbsp;else<br>&nbsp; FStatusStr := 'Printer error code: ' + IntToStr(ErrorCode);<br>&nbsp;end;<br><br>end;<br><br>
 
下面这段代码可以查看默认打印机并更改。<br>unit MainFrm;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; cbPrinters: TComboBox;<br>&nbsp; &nbsp; lblPrinter: TLabel;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure cbPrintersChange(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br>uses IniFiles, Printers;<br><br>{$R *.DFM}<br><br>procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br>&nbsp; { Copy the printer names to the combobox and set the combobox to<br>&nbsp; &nbsp; show the currently selected default printer }<br>&nbsp; cbPrinters.Items.Assign(Printer.Printers);<br>&nbsp; cbPrinters.Text := Printer.Printers[Printer.PrinterIndex];<br>&nbsp; // Update the label to reflect the default printer <br>&nbsp; lblPrinter.Caption := Printer.Printers[Printer.PrinterIndex];<br>end;<br><br>procedure TMainForm.cbPrintersChange(Sender: TObject);<br>var<br>&nbsp; IniFile: TIniFile;<br>&nbsp; TempStr1, TempStr2: String;<br>&nbsp; S: array[0..64] of char;<br>begin<br>&nbsp; with Printer do<br>&nbsp; begin<br>&nbsp; &nbsp; // Set the new printer based on the ComboBox's selected printer<br>&nbsp; &nbsp; PrinterIndex := cbPrinters.ItemIndex;<br>&nbsp; &nbsp; // Store the printer name into a temporary string<br>&nbsp; &nbsp; TempStr1 := Printers[PrinterIndex];<br>&nbsp; &nbsp; // Delete the unnecessary portion of the printer name<br>&nbsp; &nbsp; System.Delete(TempStr1, Pos(' on ', TempStr1), Length(TempStr1));<br>&nbsp; &nbsp; // Create a TIniFile class<br>&nbsp; &nbsp; IniFile := TIniFile.Create('WIN.INI');<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; // Retrieve the device name of the selected printer<br>&nbsp; &nbsp; &nbsp; TempStr2 := IniFile.ReadString('Devices', TempStr1, '');<br>&nbsp; &nbsp; &nbsp; // Change the default printer to that chosen by the user<br>&nbsp; &nbsp; &nbsp; IniFile.WriteString('windows', 'device', TempStr1 + ',' + TempStr2);<br>&nbsp; &nbsp; &nbsp; // Tell all windows that the default printer changed. <br>&nbsp; &nbsp; &nbsp; StrCopy(S, 'windows');<br>&nbsp; &nbsp; &nbsp; SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@S));<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; IniFile.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; // Update the label to reflect the new printer selection <br>&nbsp; lblPrinter.Caption := Printer.Printers[Printer.PrinterIndex];<br>end;<br><br>end.
 

Similar threads

回复
0
查看
734
不得闲
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
769
DelphiTeacher的专栏
D
顶部