首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么取得当前打印机所用的纸张Form名称

2012-03-09 
如何取得当前打印机所用的纸张Form名称?如下可取得印表机所有纸张格式名称:procedure GetPrinterPapers(vS

如何取得当前打印机所用的纸张Form名称?
如下可取得印表机所有纸张格式名称:

procedure GetPrinterPapers(vStrs: TStrings);
var
  Device, Driver, Port: array[0..255] of Char;
  hDevMode: THandle;
  j, numPaperformats: Integer;
  pPaperFormats: PPapernameArray;
  vPrinter: TPrinter;
begin
  vPrinter := TPrinter.Create;
  vPrinter.PrinterIndex := -1;
  vPrinter.GetPrinter(Device, Driver, Port, hDevmode);
  numPaperformats :=
  WinSpool.DeviceCapabilities(Device, Port, DC_PAPERNAMES, nil, nil);
  if numPaperformats > 0 then
  begin
  GetMem(pPaperformats,
  numPaperformats * Sizeof(TPapername));
  vStrs.clear;
  try
  WinSpool.DeviceCapabilities
  (Device, Port, DC_PAPERNAMES,
  Pchar(pPaperFormats), nil);
  for j := 1 to numPaperformats do
  vStrs.add(pPaperformats^[j]);
  finally
  FreeMem(pPaperformats);
  end;
  end;
end;


但我该如何取得当前印表机选的纸张格式名称是A4、B5、自订或其它的自订名例如"测试" ?
目前只有想到遍历每一种纸张取得其大小,如果和当前印表机纸张大小符合即可能是当前选中项目,
但这有一个问题就是假使有2种自定义纸张叫"自定义1"、"自定义2",且它们的纸张大小都设成相同,
此时便无法判断当前选择的是哪一个了?
是否有其他方式或API可以直接取得的呢?谢谢

[解决办法]
首先,“假使有2种自定义纸张叫"自定义1"、"自定义2",且它们的纸张大小都设成相同”,既然相同了,选哪一个都无所谓!
其次,关于打印方面的api函数,有位仁兄的博客中,列出十分详尽:http://www.02t.cn/article.asp?id=191
[解决办法]

Delphi(Pascal) code
var  DeviceMode: PDeviceMode;  ADeviceMode: THandle;  ADevice, ADriver, APort: PAnsiChar;begin  ADevice := StrAlloc(255);  ADriver := StrAlloc(255);  APort := StrAlloc(255);  Printer.GetPrinter(ADevice, ADriver, APort, ADeviceMode);  if ADeviceMode <> 0 then    DeviceMode := GlobalLock(ADeviceMode); //DeviceMode.dmFormName就是纸张名称end;//如果以上方法获取不到DeviceMode也可以用下面方法var    HPrt:THandle;    dwNeeded:Cardinal;    PrtInfo2:PPrinterInfo2;begin    OpenPrinter(pchar(Printer.Printers[Printer.PrinterIndex]), HPrt, nil);//第一个参数是打印机名称    GetPrinter(HPrt, 2, PrtInfo2, 0, @dwNeeded); //获取打印机    ReAllocMem(PrtInfo2, dwNeeded);    GetPrinter(HPrt, 2, PrtInfo2, dwNeeded, @dwNeeded);    //PrtInfo2.pDevMode.dmFormName:纸张名称end;
[解决办法]
//打印首选项对话框
var
ADevice,ADriver, APort:Array[0..255] of char;
PDMode: PDEVMODE;
DeviceHandle,hPrinter:THandle;
begin
Printer.GetPrinter(ADevice,ADriver, APort, DeviceHandle);
pDMode := GlobalLock(DeviceHandle);
WinSpool.OpenPrinter(ADevice,hPrinter,nil);
DocumentProperties(Self.Handle,hPrinter,ADevice,pDMode^,pDMode^,DM_IN_PROMPT or DM_OUT_BUFFER or DM_IN_BUFFER);
ClosePrinter(hPrinter);
end;

热点排行