unit UPrint;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls,Printers;//Printers
type
TFPrint = class(TForm)
PrinterSetupDialog1: TPrinterSetupDialog;
PrintDialog1: TPrintDialog;
BPrint: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
BSet: TButton;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
procedure BPrintClick(Sender: TObject);
procedure BSetClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FPrint: TFPrint;
implementation
{$R *.dfm}
procedure TFPrint.FormCreate(Sender: TObject);
begin
TabSheet1.Caption :='页面一';
TabSheet2.Caption :='页面二';
TabSheet3.Caption :='页面三';
TabSheet4.Caption :='页面四';
BSet.Caption :='页面设置...';
BPrint.Caption :='打印...';
end;
procedure TFPrint.BSetClick(Sender: TObject);
begin
PrinterSetupDialog1.Execute;
end;
procedure TFPrint.BPrintClick(Sender: TObject);
var
FirstPage, LastPage: Integer;
i:integer;
begin
//设置打印对话框的选项
PrintDialog1.Options := [poPageNums, poSelection];
PrintDialog1.FromPage := 1;
PrintDialog1.MinPage := 1;
PrintDialog1.ToPage := PageControl1.PageCount;
PrintDialog1.MaxPage := PageControl1.PageCount;
//显示打印对话框
if PrintDialog1.Execute then
begin
//确定用户所选择的打印范围
with PrintDialog1 do
begin
if PrintRange = prAllPages then
begin
FirstPage := MinPage - 1;
LastPage := MaxPage - 1;
end
else if PrintRange = prSelection then
begin
FirstPage := PageControl1.ActivePage.PageIndex;
LastPage := FirstPage;
end
else // PrintRange = prPageNums 的情况
begin
FirstPage := FromPage - 1;
LastPage := ToPage - 1;
end;
end;
//开始打印
with Printer do
begin
BeginDoc;
for i := FirstPage to LastPage do
begin
PageControl1.Pages[I].PaintTo(Handle, 10, 10);
if i <> LastPage then
NewPage;
end;
EndDoc;
end;
end;
end;
end.