中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>打印报表>文章内容

delphi 判断系统是否有打印机连接

来源:站内 关于:bill 发布时间:2007-06-23   [收藏] [推荐]

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^[i].pDocument), pJ^[i].Status, SavePChar(pJ^[i].pStatus)] ));
  finally
    ClosePrinter( hPrinter );
  end;
end;


[浏览: 次]   
上一篇:delphi DBExpress 提高   下一篇:delphi 改变缺省打印机
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi 获取打印机状态
·delphi 打印时改变纸张大小
·delphi 判断打印机能否打印的PostScrip
·delphi 检测存在打印机
·delphi Tmemo,TstringList,Tstrings打
·delphi 如何保存打印机信息到注册表
·delphi 在打印中间改变打印设置
·delphi 使用打印机内置字体打印
·delphi 改变打印机的打印端口
·delphi 获取打印驱动,打印端口名
·delphi 打印LabelsVCL组件
·delphi 如何发二进制字符到打印机
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932