bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi 新增報表紙張格式的需求
查看: 4892|回復: 0
go

新增報表紙張格式的需求 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2010-5-26 21:14 |只看該作者 |倒序瀏覽 |打印
From http://jasper-dale.spaces.live.c ... E92E2726C!244.entry

如果公司有用到特別紙張的規格,而作業系統本身並沒有的時候,希望藉由系統的執行時建立系統已知會使用到的紙張格式。在這邊會用到Window API中的AddForm, SetForm, DeleteForm, GetForm, DeviceCapabilities等方法,以下提供給大家作參考。
  1. interface

  2. uses Windows, Classes;

  3. type
  4.   TPaperName = array[0..63] of Char;
  5.   TPaperNameArray =
  6.     array[1..(High(Word) div Sizeof(TPaperName))] of TPaperName;
  7.   PPapernameArray = ^TPaperNameArray;

  8. procedure AddPrinterPaper(const vName: string; vWidth, vLength: integer);
  9. procedure SetPrinterPaper(const vName: string; vWidth, vLength: integer);
  10. procedure GetPrinterPaper(const vName: string; var vWidth, vLength: integer);
  11. procedure DelPrinterPaper(const vName: string);
  12. procedure GetPrinterPapers(vStrs: TStrings);
  13. procedure ReplacePrinterPaper(const vName: string; vWidth, vLength: integer);
  14. function PrinterPaperExists(const vName: string): Boolean;

  15. implementation

  16. uses Printers, WinSpool, SysUtils;

  17. procedure AddPrinterPaper(const vName: string; vWidth, vLength: integer);
  18. var
  19.   PrintDevice, PrintDriver, PrintPort: array[0..255] of Char;
  20.   hDMode: THandle;
  21.   hPrinter: THandle;
  22.   FormInfo: TFormInfo1;
  23.   PaperSize: TSize;
  24.   PaperRect: TRect;
  25.   errcode: integer;
  26.   s: string;
  27. begin
  28.   Printer.GetPrinter(PrintDevice, PrintDriver, PrintPort, hDMode);
  29.   OpenPrinter(PrintDevice, hPrinter, nil);
  30.   if (hPrinter = 0) then
  31.   begin
  32.     raise Exception.Create('Failed to open printer!');
  33.     Exit;
  34.   end;
  35.   FormInfo.Flags := FORM_USER;
  36.   FormInfo.pName := PChar(vName);
  37.   PaperSize.cx := vWidth * 100;
  38.   PaperSize.cy := vLength * 100;
  39.   PaperRect.Left := 0;
  40.   PaperRect.Top := 0;
  41.   PaperRect.Right := vWidth * 100;
  42.   PaperRect.Bottom := vLength * 100;
  43.   FormInfo.Size := PaperSize;
  44.   FormInfo.ImageableArea := PaperRect;
  45.   if (not AddForm(hPrinter, 1, @FormInfo)) then
  46.   begin
  47.     errcode := GetLastError;
  48.     if (errcode <> ERROR_FILE_EXISTS) then // Form name exists?
  49.     begin
  50.       case errcode of
  51.         ERROR_ACCESS_DENIED: s := 'Access is denied';
  52.         ERROR_INVALID_HANDLE: s := 'The handle is invalid';
  53.         ERROR_NOT_READY: s := 'The device is not ready';
  54.         ERROR_CALL_NOT_IMPLEMENTED:
  55.           s := 'Function "AddForm" is not supported on this system';
  56.       else
  57.         s := 'Failed to add a Form (paper) name!';
  58.       end;
  59.       raise Exception.Create(s);
  60.     end;
  61.   end;
  62.   ClosePrinter(hPrinter);
  63. end;

  64. procedure SetPrinterPaper(const vName: string; vWidth, vLength: integer);
  65. var
  66.   PrintDevice, PrintDriver, PrintPort: array[0..255] of Char;
  67.   hDMode: THandle;
  68.   hPrinter: THandle;
  69.   FormInfo: TFormInfo1;
  70.   PaperSize: TSize;
  71.   PaperRect: TRect;
  72.   errcode: integer;
  73.   s: string;
  74. begin
  75.   Printer.GetPrinter(PrintDevice, PrintDriver, PrintPort, hDMode);
  76.   OpenPrinter(PrintDevice, hPrinter, nil);
  77.   if (hPrinter = 0) then
  78.   begin
  79.     raise Exception.Create('Failed to open printer!');
  80.     Exit;
  81.   end;
  82.   FormInfo.Flags := FORM_USER;
  83.   FormInfo.pName := PChar(vName);
  84.   PaperSize.cx := vWidth * 100;
  85.   PaperSize.cy := vLength * 100;
  86.   PaperRect.Left := 0;
  87.   PaperRect.Top := 0;
  88.   PaperRect.Right := vWidth * 100;
  89.   PaperRect.Bottom := vLength * 100;
  90.   FormInfo.Size := PaperSize;
  91.   FormInfo.ImageableArea := PaperRect;
  92.   if not SetForm(hPrinter, PChar(vName), 1, @FormInfo) then
  93.   begin
  94.     errcode := GetLastError;
  95.     if (errcode <> ERROR_FILE_EXISTS) then // Form name exists?
  96.     begin
  97.       case errcode of
  98.         ERROR_ACCESS_DENIED: s := 'Access is denied';
  99.         ERROR_INVALID_HANDLE: s := 'The handle is invalid';
  100.         ERROR_NOT_READY: s := 'The device is not ready';
  101.         ERROR_CALL_NOT_IMPLEMENTED:
  102.           s := 'Function "SetForm" is not supported on this system';
  103.       else
  104.         s := 'Failed to set a Form (paper) name!';
  105.       end;
  106.       raise Exception.Create(s);
  107.     end;
  108.   end;
  109.   ClosePrinter(hPrinter);
  110. end;

  111. procedure GetPrinterPaper(const vName: string; var vWidth, vLength: integer);
  112. var
  113.   PrintDevice, PrintDriver, PrintPort: array[0..255] of Char;
  114.   hDMode: THandle;
  115.   hPrinter: THandle;
  116.   FormInfo: TFormInfo1;
  117.   PaperSize: TSize;
  118.   errcode: integer;
  119.   s: string;
  120.   cbBuf: DWORD;
  121.   pcbNeeded: DWORD;
  122. begin
  123.   Printer.GetPrinter(PrintDevice, PrintDriver, PrintPort, hDMode);
  124.   OpenPrinter(PrintDevice, hPrinter, nil);
  125.   if (hPrinter = 0) then
  126.   begin
  127.     raise Exception.Create('Failed to open printer!');
  128.     Exit;
  129.   end;
  130.   cbBuf := sizeof(TPaperName);
  131.   cbBuf := cbBuf + sizeof(TFormInfo1);
  132.   if GetForm(hPrinter, PChar(vName), 1, @FormInfo, cbBuf, pcbNeeded) then
  133.   begin
  134.     PaperSize := FormInfo.Size;
  135.     vWidth := Trunc(PaperSize.cx / 100);
  136.     vLength := Trunc(PaperSize.cy / 100);
  137.   end
  138.   else
  139.   begin
  140.     errcode := GetLastError;
  141.     if (errcode <> ERROR_FILE_EXISTS) then // Form name exists?
  142.     begin
  143.       case errcode of
  144.         ERROR_ACCESS_DENIED: s := 'Access is denied';
  145.         ERROR_INVALID_HANDLE: s := 'The handle is invalid';
  146.         ERROR_NOT_READY: s := 'The device is not ready';
  147.         ERROR_CALL_NOT_IMPLEMENTED:
  148.           s := 'Function "GetForm" is not supported on this system';
  149.         ERROR_INSUFFICIENT_BUFFER:
  150.           s := 'The data area passed to a system call is too small.';
  151.       else
  152.         s := 'Failed to get a Form (paper) name!';
  153.       end;
  154.       raise Exception.Create(s);
  155.     end;
  156.   end;
  157.   ClosePrinter(hPrinter);
  158. end;

  159. procedure DelPrinterPaper(const vName: string);
  160. var
  161.   PrintDevice, PrintDriver, PrintPort: array[0..255] of Char;
  162.   hDMode: THandle;
  163.   hPrinter: THandle;
  164.   FormName: string;
  165.   errcode: integer;
  166.   s: string;
  167. begin
  168.   Printer.GetPrinter(PrintDevice, PrintDriver, PrintPort, hDMode);
  169.   OpenPrinter(PrintDevice, hPrinter, nil);
  170.   if (hPrinter = 0) then
  171.   begin
  172.     raise Exception.Create('Failed to open printer!');
  173.     Exit;
  174.   end; //if...end!!
  175.   if not DeleteForm(hPrinter, PChar(vName)) then
  176.   begin
  177.     errcode := GetLastError;
  178.     if (errcode <> ERROR_FILE_EXISTS) then // Form name exists?
  179.     begin
  180.       case errcode of
  181.         ERROR_ACCESS_DENIED: s := 'Access is denied';
  182.         ERROR_INVALID_HANDLE: s := 'The handle is invalid';
  183.         ERROR_NOT_READY: s := 'The device is not ready';
  184.         ERROR_CALL_NOT_IMPLEMENTED:
  185.           s := 'Function "GetForm" is not supported on this system';
  186.       else
  187.         s := 'Failed to get a Form (paper) name!';
  188.       end; //case...end!!
  189.       raise Exception.Create(s);
  190.     end; //if...end!!
  191.   end;
  192.   ClosePrinter(hPrinter);
  193. end;

  194. procedure GetPrinterPapers(vStrs: TStrings);
  195. var
  196.   Device, Driver, Port: array[0..255] of Char;
  197.   hDevMode: THandle;
  198.   j, numPaperformats: Integer;
  199.   pPaperFormats: PPapernameArray;
  200.   vPrinter: TPrinter;
  201. begin
  202.   vPrinter := TPrinter.Create;
  203.   vPrinter.PrinterIndex := -1;
  204.   vPrinter.GetPrinter(Device, Driver, Port, hDevmode);
  205.   numPaperformats :=
  206.     WinSpool.DeviceCapabilities(Device, Port, DC_PAPERNAMES, nil, nil);
  207.   if numPaperformats > 0 then
  208.   begin
  209.     GetMem(pPaperformats,
  210.       numPaperformats * Sizeof(TPapername));
  211.     vStrs.clear;
  212.     try
  213.       WinSpool.DeviceCapabilities
  214.         (Device, Port, DC_PAPERNAMES,
  215.         Pchar(pPaperFormats), nil);
  216.       for j := 1 to numPaperformats do
  217.         vStrs.add(pPaperformats^[j]);
  218.     finally
  219.       FreeMem(pPaperformats);
  220.     end;
  221.   end;
  222. end;

  223. procedure ReplacePrinterPaper(const vName: string; vWidth, vLength: integer);
  224. begin
  225.   if PrinterPaperExists(vName) then
  226.     SetPrinterPaper(vName, vWidth, vLength)
  227.   else
  228.     AddPrinterPaper(vName, vWidth, vLength)
  229. end;

  230. function PrinterPaperExists(const vName: string): Boolean;
  231. var
  232.   vStrs: TStrings;
  233. begin
  234.   vStrs := TStringList.Create;
  235.   GetPrinterPapers(vStrs);
  236.   Result := (vStrs.IndexOf(vName) >= 0);
  237.   vStrs.Free;
  238. end;
  239. end.
複製代碼
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-5-2 07:19 , Processed in 0.009702 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.