自定义报表打印预览窗口_Delphi

  • 内容
  • 评论
  • 相关

作者:doomtoo0(红羽)

QuickRep中提供的默认打印预览窗口是英文界面的,有时客户会提出要中文界面和帮助提示,于是我首先想到的是修改预览窗口的.PAS源码。说干就干,按住Ctrl键点击TQuickRep,结果出来错误信息:Unable to locate file "quickrpt.pas",我昏!... ...

既然改不了源码,只好另想办法。在QuickRep控件栏里有个TQRPreview,好!就用它吧,于是俺刻苦钻研了一下午终于做成了和原预览窗口界面一样,功能一致的中文报表打印预览窗口。现拿出献丑大家共同探讨。

步骤如下:
1. 新建一个窗体,设置Name为Form_MyPreview。(以下代码中报表form为Form_baobiao)

2. 在窗体上添加Toolbar控 件,仿照默认预览窗口创建添加相应的Toolbutton和Speedbutton,并设置各按钮的Hint中文提示。在下面代码中,toolbar里的前三个是Toolbutton并分为一组(grouped=true,style=tbsCheck),后面全是Speedbutton,中间相应插入new separator(作用不用我说了吧)。

http://www.gaodaima.com/65662.html自定义报表打印预览窗口_Delphi

3. 添加一 Panel控件,对齐方式置为alBottom。再在此Panel上放一TStatusBar,对齐方式alClient,双击TStatusBar按add new加两项。再在TStatusBar上左边放一 ProgressBar1(用于显示报表装载进度)对齐方式alNone,left=0,top=3,height=StatusBar1的height-2,width=自己选加吧。

4. 添加QRPreview控件,对齐方式置为alClient。

5. 在Toolbutton上加载图标:添加TImageList控件并将Toolbar的images属性与之连接。当然别忘了Toolbutton的imageindex属性要和ImageList加载的图标一致。

5. 添加OpenDialog,设置Filter属性为*.QR; 添加SaveDialog,设置Filter 属性为*.QR,设置DefaultExt属性为 QR。

6. 重写TQuickRep的OnPreview事件,当然是在报表form里写了,代码如下:
procedure TForm_baobiao.QuickRep1Preview(Sender: TObject);
begin
  with TForm_MyPreview.Create(Application) do
  begin
     QRPreview1.QRPrinter := TQRPrinter(Sender);
        Show;
  end;

end;

7. 在报表form里加上: uses 新预览窗口的单元名
  
下面给出了完整的代码,run后预览功能和原预览窗口功能完全一样,你可以做修改使之更完美。另外我在编译此程序时遇到以下错误提示:
[Error] Unit2.pas(42): Undeclared identifier: 'TQRPrinter'
[Error] Unit2.pas(43): Undeclared identifier: 'CurRep'
我解决的办法很菜(还忘各位指点):在报表窗口里添加TQRPreview,再在新增预览窗口里添加TQuickRep, 运行,呵呵!通过了,然后分别删除,ok了。

匆忙赶稿,如有疏漏还望指正。谢谢

unit MyPreview;

interface

uses

  Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
  ComCtrls, ToolWin, qrprntr,Quickrpt,  
StdCtrls, ExtCtrls,qrextra,Buttons,ImgList;

type
  TForm_MyPreview = class(TForm)
    ImageList1: TImageList;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Panel1: TPanel;
    StatusBar1: TStatusBar;
    ProgressBar1: TProgressBar;
    QRPreview1: TQRPreview;
    SpeedButton1: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton2: TSpeedButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ToolButton6: TToolButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    ToolButton7: TToolButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    ToolButton8: TToolButton;
    SpeedButton9: TSpeedButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure QRPreview1PageAvailable(Sender: TObject; PageNum: Integer);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton9Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
  private
  FPageCount : integer;  //生成报表的总页数
    { Private declarations }
  public
  CurRep : TQuickRep;    // 所预览的报表
  procedure UpdatePanelShow;  //更新显示
    { Public declarations }
  end;

var
  Form_MyPreview: TForm_MyPreview;

implementation

{$R *.DFM}

procedure TForm_MyPreview.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
     CurRep := nil;
     Action := caFree;
end;

procedure TForm_MyPreview.ToolButton2Click(Sender: TObject);
begin
  QRPreview1.ZoomToFit;
  //缩放至全屏
end;

procedure TForm_MyPreview.FormCreate(Sender: TObject);
begin
  ToolButton4.Down:=true;
end;

procedure TForm_MyPreview.ToolButton3Click(Sender: TObject);
begin
  QRPreview1.Zoom:=100;
end;

procedure TForm_MyPreview.UpdatePanelShow;
begin   
     StatusBar1.Panels[1].text :=
' 第 '+inttostr(QRPreview1.PageNumber)+
   ' 页  总 '+inttostr(FPageCount)+' 页';
end;

procedure TForm_MyPreview.ToolButton4Click(Sender: TObject);
begin
  QRPreview1.ZoomToWidth;
end;

procedure TForm_MyPreview.SpeedButton1Click(Sender: TObject);
begin
  QRPreview1.PageNumber := 1;
  UpdatePanelShow;
end;

procedure TForm_MyPreview.QRPreview1PageAvailable(Sender: TObject;
  PageNum: Integer);
begin
  FPageCount := PageNum ;
  UpdatePanelShow ;

end;

procedure TForm_MyPreview.SpeedButton5Click(Sender: TObject);
begin
  QRPreview1.PageNumber := QRPreview1.PageNumber-1;
  if QRPreview1.PageNumber =0 then
  QRPreview1.PageNumber:=1;
  UpdatePanelShow;

end;

procedure TForm_MyPreview.SpeedButton6Click(Sender: TObject);
begin
  QRPreview1.PageNumber := QRPreview1.PageNumber+1;
  UpdatePanelShow;
end;

procedure TForm_MyPreview.SpeedButton2Click(Sender: TObject);
begin
  QRPreview1.PageNumber := FPageCount;
  UpdatePanelShow;

end;

procedure TForm_MyPreview.SpeedButton3Click(Sender: TObject);
begin
  QRPreview1.QRPrinter.PrintSetup;
   
end;

procedure TForm_MyPreview.SpeedButton4Click(Sender: TObject);
begin
  QRPreview1.QRPrinter.Print;

end;

procedure TForm_MyPreview.SpeedButton9Click(Sender: TObject);
begin
  Close;
end;

procedure TForm_MyPreview.SpeedButton7Click(Sender: TObject);
begin
    if SaveDialog1.Execute then
    begin
      if (SaveDialog1.FIlterIndex <>1) and (CurRep = nil) then Exit;
      //--*.QR
      QRPreview1.QRPrinter.Save(SaveDialog1.FileName);
    end;
end;

procedure TForm_MyPreview.SpeedButton8Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    QRPreview1.QRPrinter.Load(OpenDialog1.FileName);
end;

procedure TForm_MyPreview.QRPreview1ProgressUpdate(Sender: TObject;
  Progress: Integer);
begin
  ProgressBar1.Position := Progress;

end;

end.

欢迎大家阅读《自定义报表打印预览窗口_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

本文链接地址: 自定义报表打印预览窗口_Delphi

微信支付二维码

微信 赏一包辣条吧~

支付宝支付二维码

支付宝 赏一听可乐吧~

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注