如何知道系统是否安装有打印机?(50分)

  • 主题发起人 microwave
  • 开始时间
M

microwave

Unregistered / Unconfirmed
GUEST, unregistred user!
如果没有打印机我的程序会出错
 
摘自Hubdog的葵花宝典。
1、检测存在打印机
Printers是专门用来控制打印机的,可是在没有安装打印机时,却会提示I/O错误,所以必须有一个检测是否存在打印机的方法,我试过很多方法,可是I/O错误总是比我的判断早出现,所以采用以下的烂招来检测打印机。首先在uses增加Printers,再准备一个列表框ComboBox1,其属性Visible设为FALSE,然后在打印之前执行下列语句,那么就可以检测到是否存在打印机了:
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;
 
2、判断系统是否有打印机连接
Add a message handler for it to your main form, private section:
procedure WMSpoolerstatus( var msg: TWMSpoolerstatus );
message WM_SPOOLERSTATUS;

Hit Shift-Ctrl-C to make the IDE add a implementation for the method.
Add an inherited statement. msg.JobStatus will be 0 if the spooler thinks
it is healthy and < 0 if it has an error. From the list of error codes
found in Windows.PAS (above PR_JOBSTATUS) a missing network printer may not
be seen as an error, so look at msg.JobsLeft. If this never decreases in
the messages you see the spooler cannot get rid of the jobs. Of course it is
hard to tell when enough time has passed so you might become suspicious.
You can try to use the WinSpool.EnumJobs API to query the spooler directly.
Example below. See the diverse JOB_STATUS* codes given in the help topic for
JOB_INFO_1 in win32.hlp. Note that the Status field is a bitset, it can contain
more than one of the status codes. Use expressions like
if (Status and JOB_STATUS_OFFLINE) <> 0 then
... printer offline
uses Winspool, Printers;
function GetCurrentPrinterHandle: THandle;
var
Device, Driver, Port : array[0..255] of char;
hDeviceMode: THandle;
begin
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
if not OpenPrinter(@Device, Result, nil) then
RaiseLastWin32Error;
end;

Function SavePChar( p: PChar ): PChar;
const error: PChar = 'Nil';
begin
if not assigned( p ) then
result := error
else
result := p;
end;

procedure TForm1.Button2Click(Sender: TObject);
type
TJobs = Array [0..1000] of JOB_INFO_1;
PJobs = ^TJobs;
var
hPrinter : THandle;
bytesNeeded, numJobs, i: Cardinal;
pJ: PJobs;
begin
hPrinter:= GetCurrentPrinterHandle;
try
EnumJobs( hPrinter, 0, 1000, 1, Nil, 0, bytesNeeded,
numJobs );
pJ := AllocMem( bytesNeeded );
If not EnumJobs( hPrinter, 0, 1000, 1, pJ, bytesNeeded,
bytesNeeded, numJobs )
then
RaiseLastWin32Error;
memo1.clear;
if numJobs = 0 then
memo1.lines.add('No jobs in queue')
else
For i:= 0 to Pred(numJobs)do
memo1.lines.add( Format(
'Job %s, Status (%d): %s',
[SavePChar(pJ^.pDocument), pJ^.Status, SavePChar(pJ^.pStatus)] ));
finally
ClosePrinter( hPrinter );
end;
end;

 
>>如果没有打印机我的程序会出错
内容好像有点变。。。
不过没有打印机程序出错,估计你是2000下用quickreport了。
 
顶部