如何获取打印机里面有什么纸张类型可以选择啊,用代码实现(100分)

  • 主题发起人 主题发起人 jesseyzy
  • 开始时间 开始时间
J

jesseyzy

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获取打印机里面有什么纸张类型可以选择啊,用代码实现
打印机默认有A4,A3,A5,B5等等纸张类型给我们选择,我如何获取这些
有什么纸张呢?
 
1、极易,用下面的过程:
procedure GetPrinterPaperSize(StringList: TStringList);
var
phPrinter, dwNeeded, dwReturned, i: LongWord;
pBuf: PChar;
begin
if not Assigned(StringList) then
Exit;
StringList.Clear;
if OpenPrinter(PChar(Printer.Printers[0]), phPrinter, nil) then
begin
EnumForms(phPrinter, 1, nil, 0, dwNeeded, dwReturned);
pBuf := AllocMem(dwNeeded);
EnumForms(phPrinter, 1, pBuf, dwNeeded, dwNeeded, dwReturned);
for i := 0 to dwReturned - 1do
StringList.Add(PFormInfo1A(pBuf + i*SizeOf(_FORM_INFO_1A)).pName);
FreeMem(pBuf);
end;
end;
2、调用的时候这么用:
procedure TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
GetPrinterPaperSize(sl);
ShowMessage(sl[0]);
sl.Free;
end;
 
unit Unit1;


interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Printers,
Dialogs, FR_Class, StdCtrls, winspool, LMDCustomComboBox,
LMDPrinterComboBox;


type
TForm1 = class(TForm)
frReport1: TfrReport;

Button1: TButton;

Memo1: TMemo;

Memo2: TMemo;

LMDPrinterComboBox1: TLMDPrinterComboBox;

procedure Button1Click(Sender: TObject);

procedure LMDPrinterComboBox1Change(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
Papers: Array[0..255] of Word;

PaperSizes: Array[0..255] of TPoint;

procedure GetPaperName;

end;

Type
TPName = array[0..63] of char;

TPNames = array[0..0] of TPName;

var
Form1: TForm1;

ADevice, ADriver, APort: PChar;

DeviceMode: THandle;

FMode: PDeviceMode;

implementation

{$R *.dfm}
procedure TForm1.GetPaperName;

var
i,j:integer;

PaperName: Pointer;

begin

i:= DeviceCapabilities(ADevice, APort, DC_PAPERNAMES, nil,nil);

if i>1 then

GetMem(PaperName, i*64)
else
exit;

DeviceCapabilities(ADevice, APort, DC_PAPERNAMES, PaperName, nil);

FillChar(Papers, SizeOf(Papers), #0);

DeviceCapabilities(ADevice, APort, DC_PAPERS, @PaperSizes, FMode);

DeviceCapabilities(ADevice, APort, DC_PAPERS, @Papers, FMode);

Memo2.Lines.Clear;

for j:=0 to i-1 do

begin

Memo2.Lines.Add(StrPas(TPNames(PaperName^)[j])+' ==> '+IntToStr(Papers[j]));

end;

FreeMem(PaperName, i*64);

end;


procedure TForm1.Button1Click(Sender: TObject);

begin

GetMem(ADevice, 128);

GetMem(ADriver, 128);

GetMem(APort, 128);

FillChar(ADevice^, 128, 0);

FillChar(ADriver^, 128, 0);

FillChar(APort^, 128, 0);

Printer.GetPrinter (ADevice, ADriver, APort, DeviceMode );

FMode := GlobalLock(DeviceMode);

Memo1.Lines.Assign(Printer.Printers);

Memo1.Lines.Add('===================================');

Memo1.Lines.Add('打 印 机:' + Printer.Printers[Printer.PrinterIndex]);

Memo1.Lines.Add('纸张的物理尺寸:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALWIDTH))+' X '+IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT)));

Memo1.Lines.Add('可打印区域:' + IntToStr(GetDeviceCaps(Printer.Handle, HORZRES))+' X '+IntToStr(GetDeviceCaps(Printer.Handle, VERTRES)));

Memo1.Lines.Add('横向偏移量:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX)));

Memo1.Lines.Add('纵向偏移量:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY)));

GetPaperName;

FreeMem(ADevice, 128);

FreeMem(ADriver, 128);

FreeMem(APort, 128);

end;


procedure TForm1.LMDPrinterComboBox1Change(Sender: TObject);

begin

Printer.PrinterIndex := LMDPrinterComboBox1.ItemIndex;

end;


end.
 
能用就结帖,不能用就说一声。
凡是不结帖的我那有个列表,想上榜的留个名:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3911835
 
EnumForms(phPrinter, 1, nil, 0, dwNeeded, dwReturned);
这个是什么啊,没有定义啊?
 
szhcracker,
你写的,有太多的控件是你自己用了,一用就编译不了啊,能否写个最简单的,
 
修改了一下:
unit Unit1;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Printers,
Dialogs, StdCtrls, WinSpool;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
public
Papers: array[0..255] of Word;
PaperSizes: array[0..255] of TPoint;
procedure GetPaperName;
end;
Type
TPName = array[0..63] of Char;
TPNames = array[0..0] of TPName;
var
Form1: TForm1;
ADevice, ADriver, APort: PChar;
DeviceMode: THandle;
FMode: PDeviceMode;
implementation

{$R *.dfm}
procedure TForm1.GetPaperName;
var
i, j: Integer;
PaperName: Pointer;
begin

i := DeviceCapabilities(ADevice, APort, DC_PAPERNAMES, nil, nil);
if i > 1 then
GetMem(PaperName, i * 64)
else
Exit;
DeviceCapabilities(ADevice, APort, DC_PAPERNAMES, PaperName, nil);
FillChar(Papers, SizeOf(Papers), #0);
DeviceCapabilities(ADevice, APort, DC_PAPERS, @PaperSizes, FMode);
DeviceCapabilities(ADevice, APort, DC_PAPERS, @Papers, FMode);
Memo2.Lines.Clear;
for j := 0 to i - 1do
begin
Memo2.Lines.Add(StrPas(TPNames(PaperName^)[j]) + ' ==> ' + IntToStr(Papers[j]));
end;
FreeMem(PaperName, i * 64);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin

GetMem(ADevice, 128);
GetMem(ADriver, 128);
GetMem(APort, 128);
FillChar(ADevice^, 128, 0);
FillChar(ADriver^, 128, 0);
FillChar(APort^, 128, 0);
Printer.GetPrinter(ADevice, ADriver, APort, DeviceMode);
FMode := GlobalLock(DeviceMode);
Memo1.Lines.Assign(Printer.Printers);
Memo1.Lines.Add('===================================');
Memo1.Lines.Add('打 印 机:' + Printer.Printers[Printer.PrinterIndex]);
Memo1.Lines.Add('纸张的物理尺寸:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALWIDTH))+' X '+IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALHEIGHT)));
Memo1.Lines.Add('可打印区域:' + IntToStr(GetDeviceCaps(Printer.Handle, HORZRES))+' X '+IntToStr(GetDeviceCaps(Printer.Handle, VERTRES)));
Memo1.Lines.Add('横向偏移量:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX)));
Memo1.Lines.Add('纵向偏移量:' + IntToStr(GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY)));
GetPaperName;
FreeMem(ADevice, 128);
FreeMem(ADriver, 128);
FreeMem(APort, 128);
end;

end.
 
To jesseyzy:
EnumForms 是 API函数,我忘了一句话,不好意思:
uses WinSpool, Printers;
 
接受答案了.
 
楼主啊,请到这领分:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3912684
 

Similar threads

I
回复
0
查看
675
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
2K
import
I
后退
顶部