打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何取得当前打印机所用的纸张Form名称?
    

   uses Printers, WinSpool;

type
  TPointWord = packed record
    X: LongWord;
    Y: LongWord;
  end;

  TPaperName = array[0..63] of Char;
  PPaperInfo = ^TPaperInfo;
  TPaperInfo = packed record
    papername: TPapername; { display name of the paper }
    paperID: Smallint; { DMPAPER_* ID }
    papersize: TPoint; { Size in 0.1 mm }
  end;
  TPaperInfos = array of TPaperInfo;
  TPaperSizes = array of TPoint;
  TPrinterResolutions= array of TPointWord;

procedure GetPapernames(sl: TStrings; index: Integer);
type
  TPaperNameArray = array[1..High(Integer) div Sizeof(TPaperName)] of TPaperName;
  PPapernameArray = ^TPaperNameArray;
  TPaperArray = array[1..High(Integer) div Sizeof(Word)] of Word;
  PPaperArray = ^TPaperArray;
var
  Device, Driver, Port: array[0..255] of Char;
  hDevMode: THandle;
  i, numPaperNames, numPapers, temp: Integer;
  pPaperNames: PPapernameArray;
  pPapers: PPaperArray;
begin
  Assert(Assigned(sl));
  Printer.PrinterIndex := index;
  Printer.GetPrinter(Device, Driver, Port, hDevmode);
  numPaperNames := WinSpool.DeviceCapabilities(Device, Port, DC_PAPERNAMES, nil, nil);
  numPapers := WinSpool.DeviceCapabilities(Device, Port, DC_PAPERS, nil, nil);
  if numPapers <> numPaperNames then
  begin
    raise Exception.Create('DeviceCapabilities reports different number of papers and '+ ' paper names!');
  end;
  if numPaperNames > 0 then
  begin
    GetMem(pPaperNames, numPaperNames * Sizeof(TPapername));
    GetMem(pPapers, numPapers * Sizeof(Word));
    try
      WinSpool.DeviceCapabilities(Device, Port, DC_PAPERNAMES, Pchar(pPaperNames), nil);
      WinSpool.DeviceCapabilities(Device, Port, DC_PAPERS, Pchar(pPapers), nil);
      sl.clear;
      for i := 1 to numPaperNames do
      begin
        temp := pPapers^[i];
        sl.addObject(pPaperNames^[i], TObject(temp));
      end;
    finally
      FreeMem(pPaperNames);
      if pPapers <> nil then
        FreeMem(pPapers);
    end;
  end;
end;

procedure GetPapersizes(var sizes: TPaperSizes; index: Integer);
var
  Device, Driver, Port: array[0..255] of Char;
  hDevMode: THandle;
  numPapers: Integer;
begin
  Printer.PrinterIndex := index;
  Printer.GetPrinter(Device, Driver, Port, hDevmode);
  numPapers := WinSpool.DeviceCapabilities(Device, Port, DC_PAPERS, nil, nil);
  SetLength(sizes, numPapers);
  if numPapers > 0 then
    WinSpool.DeviceCapabilities(Device, Port, DC_PAPERSIZE, PChar(@sizes[0]), nil);
end;

procedure GetPaperInfo(var infos: TPaperInfos; index: Integer);
var
  sizes: TPaperSizes;
  sl: TStringlist;
  i: Integer;
begin
  sl := Tstringlist.Create;
  try
    GetPaperNames(sl, index);
    GetPaperSizes(sizes, index);
    Assert(sl.count = Length(sizes));
    SetLength(infos, sl.count);
    for i := 0 to sl.count - 1 do
    begin
      StrPLCopy(infos[i].papername, sl[i], Sizeof(TPapername) - 1);
      infos[i].paperID := LoWord(Longword(sl.Objects[i]));
      infos[i].papersize := sizes[i];
    end;
  finally
    sl.Free;
  end;
end;

//实现过程
var
 ADevice,ADriver, APort:array[0..255] of char;
 infos: TPaperInfos;
 DeviceHandle,hPrinter:THandle;
 PDMode:     PDEVMODE;
 P:Pchar;
 i,iIndex:integer;
begin
 GetPaperInfo(infos, Printer.PrinterIndex);  //获取纸张列表
 Printer.GetPrinter(ADevice,ADriver, APort, DeviceHandle); //取打印机数据
 pDMode := GlobalLock(DeviceHandle);
 ListBox1.Items.Clear;
 for i:=low(infos) to high(infos) do begin
    GetMem(P, sizeof(TPaperInfo));
    move(infos[i], p^, sizeof(TPaperInfo));
    iIndex:=ListBox1.Items.AddObject(infos[i].papername, Tobject(P));
    if pDMode^.dmPaperSize=infos[i].paperID then begin //关键就是对比dmPaperSize参数
      ListBox1.ItemIndex := iIndex
    end;
 end;
 GlobalUnlock(DeviceHandle);
end;


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
delphi使用Socket获取网页源文件
Kendy--从内存中加载并启动一个exe
从内存中加载DLL DELPHI版
[ZT]如何得到其他程序的Richedit中的RTF数据
VF设置打印机若干方法
VB 报表页面设置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服