这里一个更完整的.
{$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.