qreport的例子的详细分析

qreport的例子的详细分析

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, QuickRpt, QRCtrls, ExtCtrls, DB, ADODB, StdCtrls;

type
TForm1 = class(TForm)
ADOTable1: TADOTable;
QuickRep1: TQuickRep;
DetailBand1: TQRBand;
meCutWord: TMemo;
QRDBText2: TQRDBText;
QRShape1: TQRShape;
QRDBText1: TQRDBText;
Label1: TLabel;
QRShape2: TQRShape;
QRLabel1: TQRLabel;
procedure QRDBText1Print(sender: TObject; var Value: String);
procedure FormCreate(Sender: TObject);
procedure DetailBand1AfterPrint(Sender: TQRCustomBand;
BandPrinted: Boolean);
procedure QuickRep1BeforePrint(Sender: TCustomQuickRep;
var PrintReport: Boolean);
procedure DetailBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
private
{ Private declarations }
public
{ Public declarations }
hhh: array of Extended; // 用来记录每条记录扩充的高度
end;
const
MAXLEN= 21;

var
Form1: TForm1;
rows : integer;


implementation

{$R *.dfm}

(* DetailBand1AfterPrint, QuickRep1BeforePrint ,DetailBand1BeforePrint
都是用来当多行显示的时候,qrdbtext将向下扩展,这时默认情况下,qrshape是不会向下扩展的,
上述三个处理就是用来解决这个问题的。
此外,还用到 rows : integer;
hhh: array of Extended;
setlength(hhh, 1000); //1000是代表的记录的总数,这里用1000是一个大概的表示。
//可以的话应该精确表示,以避免空间的浪费
quickRep1.Prepare;


*)

procedure TForm1.QRDBText1Print(sender: TObject; var Value: String);
var
I: Integer;
text:TQRDBText;
begin
//解决中文多行显示的问题。
//dbtext.autosize:= false;
//dbtext.autostretch := true;
//dbtext.wrap := true;
text := sender as TQRDBText;
meCutWord.Font.Assign(text.Font); // 指定字型
meCutWord.Width := text.Width;// 指定寬度
meCutWord.Lines.Clear;// 清除上一次的東西
meCutWord.Lines.Text := Value; // 把 value assign 進去
Value := meCutWord.Lines.Strings[0];
for I := 1 to meCutWord.Lines.Count - 1 do
begin
Value := Value + #13 + meCutWord.Lines.Strings[i];// 逐行取出再塞回去
end;

 

end;
procedure TForm1.FormCreate(Sender: TObject);
begin
setlength(hhh, 1000);
QuickRep1.Prepare;
quickRep1.PreviewModal;
end;

procedure TForm1.DetailBand1AfterPrint(Sender: TQRCustomBand;
BandPrinted: Boolean);
begin
hhh[rows] := QuickRep1.bands.DetailBand.Expanded;
rows := rows + 1;
end;

procedure TForm1.QuickRep1BeforePrint(Sender: TCustomQuickRep;
var PrintReport: Boolean);
var
i:integer;
begin
rows := 1;
for I := 0 to quickrep1.Bands.DetailBand.ControlCount - 1 do // Iterate
begin
if QuickRep1.Bands.DetailBand.Controls[i] is TQRShape then
(QuickRep1.Bands.DetailBand.Controls[i] as TQRShape).Height :=
QuickRep1.Bands.DetailBand.Height;
end; // for
end;

procedure TForm1.DetailBand1BeforePrint(Sender: TQRCustomBand;
var PrintBand: Boolean);
var
i:Integer;
begin

for I := 0 to quickrep1.Bands.DetailBand.ControlCount - 1 do // Iterate
begin
if QuickRep1.Bands.DetailBand.Controls[i] is TQRShape then
begin
(QuickRep1.Bands.DetailBand.Controls[i] as TQRShape).top := 1;
(QuickRep1.Bands.DetailBand.Controls[i] as TQRShape).Size.Height :=
QuickRep1.Bands.DetailBand.Size.Height + hhh[rows];
end;
end;
end;

end.