pchome2000 发表于 2014-9-4 17:31:04

请教高手,delphi 改成au3可以吗?

本帖最后由 pchome2000 于 2014-9-4 19:01 编辑

请教高手,delphi 改成au3可以吗?

uses Printers, WinSpool;

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

   TPaperName = array 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 of TPaperName;
   PPapernameArray = ^TPaperNameArray;
   TPaperArray = array of Word;
   PPaperArray = ^TPaperArray;
var
   Device, Driver, Port: array 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^;
         sl.addObject(pPaperNames^, 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 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), 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.papername, sl, Sizeof(TPapername) - 1);
       infos.paperID := LoWord(Longword(sl.Objects));
       infos.papersize := sizes;
   end;
   finally
   sl.Free;
   end;
end;

//实现过程
var
ADevice,ADriver, APort:array 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, p^, sizeof(TPaperInfo));
   iIndex:=ListBox1.Items.AddObject(infos.papername, Tobject(P));
   if pDMode^.dmPaperSize=infos.paperID then begin //关键就是对比dmPaperSize参数
      ListBox1.ItemIndex := iIndex
   end;
end;
GlobalUnlock(DeviceHandle);
end;

gto250 发表于 2014-9-4 18:19:04

如果你确定你贴的代码是vb的话,我就给你改了

邪恶海盗 发表于 2014-9-5 08:56:11

Delphi为什么要改成AU3,按理说Delphi的效率比AU3更高...

pchome2000 发表于 2014-9-5 11:57:54

想用au3直接操作,

lin6051 发表于 2014-9-5 12:43:53

不懂 啊   懂delphy 还用au3?
页: [1]
查看完整版本: 请教高手,delphi 改成au3可以吗?