unit Uprin;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,printers, Spin, ExtCtrls;type TFprin = class(TForm) mprinter: TImage; Pan1: TPanel; Gb1: TGroupBox; PCB0: TComboBox; Gb2: TGroupBox; Lab0: TLabel; Lab1: TLabel; Labx: TLabel; Laby: TLabel; PCB1: TComboBox; SE1: TSpinEdit; SE2: TSpinEdit; Rb1: TRadioButton; Rb2: TRadioButton; Button1: TButton; T1: TImage; T2: TImage; procedure Button1Click(Sender: TObject); procedure FormActivate(Sender: TObject); procedure PCB1Change(Sender: TObject); procedure PCB0Change(Sender: TObject); procedure Rb1Click(Sender: TObject); procedure Rb2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Fprin: TFprin;implementation{$R *.dfm} //自定义纸张过程x,y为0.1mm procedure SetPaperSize(X, Y: Integer); var Device: array[0..255] of char; Driver: array[0..255] of char; Port: array[0..255] of char; hDMode: THandle; PDMode: PDEVMODE; begin Printer.PrinterIndex := Fprin.PCB0.ItemIndex; Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin if (x = 0) or (y = 0) then begin pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize; pDMode^.dmPaperSize := DMPAPER_FANFOLD_US; end else begin //自定义纸张大小 pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH; pDMode^.dmPaperSize := DMPAPER_USER; if Fprin.Rb2.Checked then printer.Orientation:=polandscape else printer.Orientation:=poportrait; //showmessage(inttostr(pdmode^.dmorientation)); if pdmode^.dmOrientation=dmorient_landscape then begin pDMode^.dmPaperWidth := y; pDMode^.dmPaperLength := x; end else if PDMode^.dmOrientation=dmorient_portrait then begin pdmode^.dmPaperWidth:=x; pdmode^.dmPaperLength:=y; end; end; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; ResetDC(Printer.Handle,pDMode^); GlobalUnlock(hDMode); end; end; Printer.PrinterIndex := Fprin.PCB0.ItemIndex; end; procedure TFprin.Button1Click(Sender: TObject); var dc:THandle; i:integer; begin PCB0.ItemIndex := Printer.PrinterIndex; dc:=printer.Handle; { SE1.Value:=GetDeviceCaps(dc, horzsize)*10; SE2.Value:=GetDeviceCaps(dc, vertsize)*10; } Labx.Caption:=inttostr(GetDeviceCaps(dc, horzsize)*10); Laby.Caption:=inttostr(GetDeviceCaps(dc, vertsize)*10); exit; SetPaperSize(SE1.Value,SE2.Value); Fprin.Caption:='hhh'+Printer.Printers[Printer.PrinterIndex]; exit; Fprin.Caption:='hhh'+Printer.Printers[Printer.PrinterIndex]; Dc := printer.handle; printer.Orientation:=poportrait; printer.BeginDoc; with Printer do begin printer.Title:='Test'; Printer.Canvas.MoveTo(10,400); Printer.Canvas.LineTo(400,400); printer.Canvas.TextOut(10,10,'Testgjhajjhc'); printer.Canvas.TextOut(10,100,'TNNNNNNNNNNNN'); printer.Canvas.TextOut(10,200,PCB0.Items[Printer.PrinterIndex]); end; Printer.EndDoc; Printer.NewPage; end;procedure TFprin.FormActivate(Sender: TObject); var dc : THandle; begin PCB0.Items := Printer.Printers; PCB0.ItemIndex := Printer.PrinterIndex; dc:=printer.Handle; SE1.Value:=GetDeviceCaps(dc, horzsize)*10; SE2.Value:=GetDeviceCaps(dc, vertsize)*10; end;procedure TFprin.PCB1Change(Sender: TObject); begin SE1.ReadOnly:=True; SE2.ReadOnly:=True; Case PCB1.ItemIndex of 0:begin SE1.Value:=2100; SE2.Value:=2970; end; 1:begin SE1.Value:=1480; SE2.Value:=2100; end; 2:begin SE1.Value:=3810; SE2.Value:=2790; end; 3:begin SE1.Value:=2410; SE2.Value:=2790; end; 4:begin SE1.Value:=2410; SE2.Value:=1395; end; 5:begin SE1.Value:=2410; SE2.Value:=960; end; else begin SE1.ReadOnly:=False; SE2.ReadOnly:=False; end; end; //设置纸张大小 SetPaperSize(SE1.Value,SE2.Value); end;procedure TFprin.PCB0Change(Sender: TObject); begin Printer.PrinterIndex := Fprin.PCB0.ItemIndex; end;procedure TFprin.Rb1Click(Sender: TObject); begin T1.Visible:=True; T2.Visible:=False; end;procedure TFprin.Rb2Click(Sender: TObject); begin T2.Visible:=True; T1.Visible:=False; end;end.
我是用QuickReport做的报表,qianzhidao(钱知道) 的解答我不太能够理解
我做的打印纸张设置程序与用什么做报表无关,程序的关键事件是: //自定义纸张过程x,y为0.1mm procedure SetPaperSize(X, Y: Integer); var Device: array[0..255] of char; Driver: array[0..255] of char; Port: array[0..255] of char; hDMode: THandle; PDMode: PDEVMODE; begin Printer.PrinterIndex := Fprin.PCB0.ItemIndex;//选用哪个打印机,本例是默认的打印机 Printer.GetPrinter(Device, Driver, Port, hDMode); if hDMode <> 0 then begin pDMode := GlobalLock(hDMode); if pDMode <> nil then begin if (x = 0) or (y = 0) then begin pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize; pDMode^.dmPaperSize := DMPAPER_FANFOLD_US; end else begin //自定义纸张大小 pDMode^.dmFields := pDMode^.dmFields or DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH; pDMode^.dmPaperSize := DMPAPER_USER; if Fprin.Rb2.Checked then printer.Orientation:=polandscape else printer.Orientation:=poportrait; //showmessage(inttostr(pdmode^.dmorientation));//测试时显示打印方向 if pdmode^.dmOrientation=dmorient_landscape then begin pDMode^.dmPaperWidth := y; pDMode^.dmPaperLength := x; end else if PDMode^.dmOrientation=dmorient_portrait then begin pdmode^.dmPaperWidth:=x; pdmode^.dmPaperLength:=y; end; end; pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL; ResetDC(Printer.Handle,pDMode^); GlobalUnlock(hDMode); end; end; end;最后,打印机默认的纸张的大小尽量设小些.
那个自定义是自定义PAGE大小的,不是定义走纸,我要定义走纸,就是打印机出纸的长度
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,printers, Spin, ExtCtrls;type
TFprin = class(TForm)
mprinter: TImage;
Pan1: TPanel;
Gb1: TGroupBox;
PCB0: TComboBox;
Gb2: TGroupBox;
Lab0: TLabel;
Lab1: TLabel;
Labx: TLabel;
Laby: TLabel;
PCB1: TComboBox;
SE1: TSpinEdit;
SE2: TSpinEdit;
Rb1: TRadioButton;
Rb2: TRadioButton;
Button1: TButton;
T1: TImage;
T2: TImage;
procedure Button1Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure PCB1Change(Sender: TObject);
procedure PCB0Change(Sender: TObject);
procedure Rb1Click(Sender: TObject);
procedure Rb2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Fprin: TFprin;implementation{$R *.dfm}
//自定义纸张过程x,y为0.1mm
procedure SetPaperSize(X, Y: Integer);
var
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
Printer.PrinterIndex := Fprin.PCB0.ItemIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
if (x = 0) or (y = 0) then
begin
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
pDMode^.dmPaperSize := DMPAPER_FANFOLD_US;
end
else
begin
//自定义纸张大小
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
if Fprin.Rb2.Checked then
printer.Orientation:=polandscape
else
printer.Orientation:=poportrait;
//showmessage(inttostr(pdmode^.dmorientation));
if pdmode^.dmOrientation=dmorient_landscape then
begin
pDMode^.dmPaperWidth := y;
pDMode^.dmPaperLength := x;
end
else if PDMode^.dmOrientation=dmorient_portrait then
begin
pdmode^.dmPaperWidth:=x;
pdmode^.dmPaperLength:=y;
end;
end;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
ResetDC(Printer.Handle,pDMode^);
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Fprin.PCB0.ItemIndex;
end;
procedure TFprin.Button1Click(Sender: TObject);
var
dc:THandle;
i:integer;
begin
PCB0.ItemIndex := Printer.PrinterIndex;
dc:=printer.Handle;
{ SE1.Value:=GetDeviceCaps(dc, horzsize)*10;
SE2.Value:=GetDeviceCaps(dc, vertsize)*10; }
Labx.Caption:=inttostr(GetDeviceCaps(dc, horzsize)*10);
Laby.Caption:=inttostr(GetDeviceCaps(dc, vertsize)*10);
exit;
SetPaperSize(SE1.Value,SE2.Value);
Fprin.Caption:='hhh'+Printer.Printers[Printer.PrinterIndex];
exit;
Fprin.Caption:='hhh'+Printer.Printers[Printer.PrinterIndex];
Dc := printer.handle;
printer.Orientation:=poportrait;
printer.BeginDoc;
with Printer do
begin
printer.Title:='Test';
Printer.Canvas.MoveTo(10,400);
Printer.Canvas.LineTo(400,400);
printer.Canvas.TextOut(10,10,'Testgjhajjhc');
printer.Canvas.TextOut(10,100,'TNNNNNNNNNNNN');
printer.Canvas.TextOut(10,200,PCB0.Items[Printer.PrinterIndex]);
end;
Printer.EndDoc;
Printer.NewPage;
end;procedure TFprin.FormActivate(Sender: TObject);
var
dc : THandle;
begin
PCB0.Items := Printer.Printers;
PCB0.ItemIndex := Printer.PrinterIndex;
dc:=printer.Handle;
SE1.Value:=GetDeviceCaps(dc, horzsize)*10;
SE2.Value:=GetDeviceCaps(dc, vertsize)*10;
end;procedure TFprin.PCB1Change(Sender: TObject);
begin
SE1.ReadOnly:=True;
SE2.ReadOnly:=True;
Case PCB1.ItemIndex of
0:begin
SE1.Value:=2100;
SE2.Value:=2970;
end;
1:begin
SE1.Value:=1480;
SE2.Value:=2100;
end;
2:begin
SE1.Value:=3810;
SE2.Value:=2790;
end;
3:begin
SE1.Value:=2410;
SE2.Value:=2790;
end;
4:begin
SE1.Value:=2410;
SE2.Value:=1395;
end;
5:begin
SE1.Value:=2410;
SE2.Value:=960;
end;
else
begin
SE1.ReadOnly:=False;
SE2.ReadOnly:=False;
end;
end;
//设置纸张大小
SetPaperSize(SE1.Value,SE2.Value);
end;procedure TFprin.PCB0Change(Sender: TObject);
begin
Printer.PrinterIndex := Fprin.PCB0.ItemIndex;
end;procedure TFprin.Rb1Click(Sender: TObject);
begin
T1.Visible:=True;
T2.Visible:=False;
end;procedure TFprin.Rb2Click(Sender: TObject);
begin
T2.Visible:=True;
T1.Visible:=False;
end;end.
//自定义纸张过程x,y为0.1mm
procedure SetPaperSize(X, Y: Integer);
var
Device: array[0..255] of char;
Driver: array[0..255] of char;
Port: array[0..255] of char;
hDMode: THandle;
PDMode: PDEVMODE;
begin
Printer.PrinterIndex := Fprin.PCB0.ItemIndex;//选用哪个打印机,本例是默认的打印机
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then
begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then
begin
if (x = 0) or (y = 0) then
begin
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
pDMode^.dmPaperSize := DMPAPER_FANFOLD_US;
end
else
begin
//自定义纸张大小
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
if Fprin.Rb2.Checked then
printer.Orientation:=polandscape
else
printer.Orientation:=poportrait;
//showmessage(inttostr(pdmode^.dmorientation));//测试时显示打印方向
if pdmode^.dmOrientation=dmorient_landscape then
begin
pDMode^.dmPaperWidth := y;
pDMode^.dmPaperLength := x;
end
else if PDMode^.dmOrientation=dmorient_portrait then
begin
pdmode^.dmPaperWidth:=x;
pdmode^.dmPaperLength:=y;
end;
end;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
ResetDC(Printer.Handle,pDMode^);
GlobalUnlock(hDMode);
end;
end;
end;最后,打印机默认的纸张的大小尽量设小些.