如何取得打印机所支持的打印纸张名称的列表以及这些纸张的尺寸.(100分)

  • 主题发起人 主题发起人 delphiform
  • 开始时间 开始时间
D

delphiform

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取得打印机所支持的打印纸张名称的列表以及这些纸张的尺寸.
 
我这里有一个单元,就取是取得打印纸张的相关数据的.
nit Unit1;
interface
uses
WinSpool,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
{function DeviceCapabilities(pDeviceName, pPort: PChar;
iIndex: Integer;
pOutput: PChar;
DevMode: PDeviceMode): Integer;
stdcall;}
implementation
Uses Printers,Registry;
{$R *.dfm}
//function DeviceCapabilities;
external 'winspool.drv' name 'DeviceCapabilitiesA';
procedure TForm1.Button1Click(Sender: TObject);
Type TPaperName=Array[0..63] of Char;
var Rslt,i:integer;
PaperNames:Array of TPaperName;
PaperSizes:Array of TPoint;
PaperCodes:Array of Word;
szDriver,szDeviceName,szPort:String;//Array[0..255] of char;
DM:THandle;
begin
SetLength(szDeviceName,256);
SetLength(szDriver,256);
SetLength(szPort,256);
Printer.GetPrinter(PChar(szDeviceName),PChar(szDriver),PChar(szPort),DM);
ListBox1.Items.Clear;
Rslt := DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERNAMES, nil, nil);
if Rslt > 0 then
begin
SetLength(PaperNames, Rslt);
SetLength(PaperSizes, Rslt);
SetLength(PaperCodes, Rslt);
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERNAMES,Pointer(PaperNames), nil) = - 1 then
raise Exception.Create('DevCap Error');
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERSIZE,Pointer(PaperSizes), nil) = - 1 then
raise Exception.Create('DevCap Error');
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERS,Pointer(PaperCodes), nil) = - 1 then
raise Exception.Create('DevCap Error');
for i := 0 to Rslt - 1do
ListBox1.Items.Add(Strpas(TPaperName(PaperNames))+':'+IntToStr(PaperSizes.X)+':'+IntToStr(PaperSizes.Y)+':'+IntToStr(PaperCodes));
end;
end;

end.
 
这里一个更完整的.

{$IFDEF VER140}
{$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}
unit UnitPageSetup;
interface
uses
Windows, Messages, SysUtils, {$IFDEF VER140}Variants, {$ENDIF}Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, Buttons, ExtCtrls;
type
TPaper=Class
Code:Word;
Size:TSize;
Name:String;
end;
TFormPageSetup = class(TForm)
SpinEditWidth: TSpinEdit;
SpinEditHeight: TSpinEdit;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
CheckBox1: TCheckBox;
Label3: TLabel;
Label4: TLabel;
SpinEditLeft: TSpinEdit;
SpinEditRight: TSpinEdit;
Label5: TLabel;
Label6: TLabel;
SpinEditTop: TSpinEdit;
SpinEditBottom: TSpinEdit;
ComboBoxPaper: TComboBox;
Label7: TLabel;
RadioGroup1: TRadioGroup;
Bevel1: TBevel;
Bevel2: TBevel;
Label8: TLabel;
EditMasterHeader: TEdit;
EditSubHeader: TEdit;
Label9: TLabel;
Panel1: TPanel;
SpeedButton1: TSpeedButton;
Panel2: TPanel;
SpeedButton2: TSpeedButton;
FontDialog1: TFontDialog;
procedure SpinEditWidthChange(Sender: TObject);
procedure SpinEditHeightChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ComboBoxPaperChange(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
{ Private declarations }
public
OldSize:TSize;
Procedure UpdatePaperIDX;
{ Public declarations }
end;

var
FormPageSetup: TFormPageSetup;
function DeviceCapabilities(pDeviceName, pPort: PChar;
iIndex: Integer;
pOutput: PChar;
DevMode: PDeviceMode): Integer;
stdcall;
implementation
Uses Printers;
{$R *.dfm}
function DeviceCapabilities;
external 'winspool.drv' name 'DeviceCapabilitiesA';
procedure TFormPageSetup.SpinEditWidthChange(Sender: TObject);
begin
if OldSize.cx=0 then
Exit;
SpinEditHeight.OnChange:=nil;
try
if CheckBox1.Checked then
SpinEditHeight.Value:=Round(OldSize.cy/OldSize.cx*SpinEditWidth.Value)
else
OldSize.cx:=SpinEditWidth.Value;
ComboBoxPaper.ItemIndex:=ComboBoxPaper.Items.Count-1;
finally
SpinEditHeight.OnChange:=SpinEditHeightChange;
end;
end;

procedure TFormPageSetup.SpinEditHeightChange(Sender: TObject);
begin
if OldSize.cy=0 then
Exit;
SpinEditWidth.OnChange:=nil;
try
if CheckBox1.Checked then
SpinEditWidth.Value:=Round(OldSize.cx/OldSize.cy*SpinEditHeight.Value)
else
OldSize.cy:=SpinEditHeight.Value;
ComboBoxPaper.ItemIndex:=ComboBoxPaper.Items.Count-1;
finally
SpinEditWidth.OnChange:=SpinEditWidthChange;
end;
end;

procedure TFormPageSetup.FormCreate(Sender: TObject);
Type TPaperName=Array[0..63] of Char;
var Rslt,i:integer;
PaperNames:Array of TPaperName;
PaperSizes:Array of TSize;
PaperCodes:Array of Word;
szDriver,szDeviceName,szPort:String;//Array[0..255] of char;
DM:THandle;
Paper:TPaper;
begin
SetLength(szDeviceName,256);
SetLength(szDriver,256);
SetLength(szPort,256);
Printer.GetPrinter(PChar(szDeviceName),PChar(szDriver),PChar(szPort),DM);
ComboBoxPaper.Items.Clear;
Rslt := DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERNAMES, nil, nil);
if Rslt > 0 then
begin
SetLength(PaperNames, Rslt);
SetLength(PaperSizes, Rslt);
SetLength(PaperCodes, Rslt);
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERNAMES,Pointer(PaperNames), nil) = - 1 then
raise Exception.Create('DevCap Error');
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERSIZE,Pointer(PaperSizes), nil) = - 1 then
raise Exception.Create('DevCap Error');
if DeviceCapabilities(PChar(szDeviceName), PChar(szPort), DC_PAPERS,Pointer(PaperCodes), nil) = - 1 then
raise Exception.Create('DevCap Error');
for i := 0 to Rslt - 1do
begin
Paper:=TPaper.Create;
Paper.Code:=PaperCodes;
Paper.Size:=PaperSizes;
Paper.Name:=StrPas(PaperNames);
ComboBoxPaper.Items.AddObject(Paper.Name,Paper);
end;
ComboBoxPaper.Items.AddObject('自定义纸张',nil);
end;
end;

procedure TFormPageSetup.FormDestroy(Sender: TObject);
Var i:Integer;
Paper:TPaper;
begin
For i:=0 to ComboBoxPaper.Items.Count-2do
//最后一个是自定义纸张类型,所以这里少一项
begin
Paper:=TPaper(ComboBoxPaper.Items.Objects);
Paper.Free;
ComboBoxPaper.Items.Objects:=nil;
end;
end;

procedure TFormPageSetup.ComboBoxPaperChange(Sender: TObject);
Var Paper:TPaper;
begin
if (ComboBoxPaper.ItemIndex<>ComboBoxPaper.Items.Count-1) and (ComboBoxPaper.ItemIndex>=0) then
begin
Paper:=TPaper(ComboBoxPaper.Items.Objects[ComboBoxPaper.ItemIndex]);
SpinEditWidth.OnChange:=nil;
SpinEditHeight.OnChange:=nil;
try
if RadioGroup1.ItemIndex=0 then
begin
SpinEditWidth.Value:=Paper.Size.cx;
SpinEditHeight.Value:=Paper.Size.cy;
end else
begin
SpinEditWidth.Value:=Paper.Size.cy;
SpinEditHeight.Value:=Paper.Size.cx;
end;
finally
SpinEditWidth.OnChange:=SpinEditWidthChange;
SpinEditHeight.OnChange:=SpinEditHeightChange;
end;
end;
end;

procedure TFormPageSetup.FormResize(Sender: TObject);
begin
Bevel1.Left:=0;
Bevel1.Width:=Width;
Bevel2.Left:=0;
Bevel2.Width:=Width;
end;

procedure TFormPageSetup.FormShow(Sender: TObject);
begin
Bevel1.Left:=0;
Bevel1.Width:=Width;
Bevel2.Left:=0;
Bevel2.Width:=Width;
end;

procedure TFormPageSetup.UpdatePaperIDX;
Var i:Integer;
Paper:TPaper;
begin
RadioGroup1.OnClick:=nil;
ComboBoxPaper.OnChange:=nil;
try
For i:=0 to ComboBoxPaper.Items.Count-2do
//最后一个是自定义纸张类型,所以这里少一项
begin
Paper:=TPaper(ComboBoxPaper.Items.Objects);
if (Paper.Size.cx=SpinEditWidth.Value) and (Paper.Size.cy=SpinEditHeight.Value) then
begin
ComboBoxPaper.ItemIndex:=i;
RadioGroup1.ItemIndex:=0;
Break;
end else
if (Paper.Size.cy=SpinEditWidth.Value) and (Paper.Size.cx=SpinEditHeight.Value) then
begin
ComboBoxPaper.ItemIndex:=i;
RadioGroup1.ItemIndex:=1;
Break;
end;
end;
finally
RadioGroup1.OnClick:=RadioGroup1Click;
ComboBoxPaper.OnChange:=ComboBoxPaperChange;
end;
end;

procedure TFormPageSetup.RadioGroup1Click(Sender: TObject);
Var Tmp:TSize;
begin
SpinEditWidth.OnChange:=nil;
SpinEditHeight.OnChange:=nil;
try
Tmp.cx:=SpinEditWidth.Value;
Tmp.cy:=SpinEditHeight.Value;
SpinEditWidth.Value:=Tmp.cy;
SpinEditHeight.Value:=Tmp.cx;
finally
SpinEditWidth.OnChange:=SpinEditWidthChange;
SpinEditHeight.OnChange:=SpinEditHeightChange;
end;
end;

procedure TFormPageSetup.SpeedButton1Click(Sender: TObject);
begin
FontDialog1.Font.Assign(Panel1.Font);
if FontDialog1.Execute then
Panel1.Font.Assign(FontDialog1.Font);
end;

procedure TFormPageSetup.SpeedButton2Click(Sender: TObject);
begin
FontDialog1.Font.Assign(Panel2.Font);
if FontDialog1.Execute then
Panel2.Font.Assign(FontDialog1.Font);
end;

end.
 
to aizb:
谢谢!
{$IFDEF VER140}
{$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}

{$IFDEF VER140}Variants, {$ENDIF}
是什么意思?
 
这是条件编译指令,表示如果定义了VER140这个编译符号就编译编译从{$IFDEF VER140}到{$ENDIF}中的代码,
这段代码也可以是编译指令.
VER140是Delphi6定义的一个内部编译符号.用于识别是否是Delphi6环境.
 
多谢aizb.
 

Similar threads

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