近段時間用Delphi7的RaveReport控件做票據打程序, 遇到不少問題. 幸好在網上得到得一些前輩(有意或無意)的幫助.得到不少心得. 在此表示感謝! 同時也將自己的所得拿出來與大家一起分享.
unit UReportClass;
interface
uses
Windows,classes,Forms,UGlobalSet,RpRave,RvClass,RvCSStd,RVProj,RpDefine,RpConDS,
RvDirectDataView,RvCsData,RvCsRpt,rvData,RpRenderPreVIEw,Rpfiler,sysUtils,
RpRenderPrinter,rvUtil,Controls,Graphics,RvCsDraw,RpBase,Printers;
type
ReportClass=class(TComponent)
private
ClsIndexList:TStringList;
FRpPage:TRavePage;
FReport:TRaveReport;
function GetReportName: CString;
procedure SetOnPVPageChange(const Value: TNotifyEvent);
function GetPVPageChange: TNotifyEvent;
protected
RvProject: TRvProject;
RvNDRWriter: TRvNDRWriter;
RvRenderPreView: TRvRenderPreVIEw;
RvRenderPrinter: TRvRenderPrinter;
DataVIEwList:TList;
function GetCtrlName(Cls:TClass):CString;
public
PreViewTop,PreVIEwLeft:Integer;
property OnPVPageChange:TNotifyEvent read GetPVPageChange write SetOnPVPageChange;
//
property ReportName:CString read GetReportName;
procedure ProjectExecute;
constructor Create(AWoner:TComponent);override;
destructor Destroy;override;
//
function AddText(Parent:TRavecomponent;aText:CString;Top,Left,
width,Height:Single;Font:TRaveFont):TRaveText;
function AddDataText(Parent:TRavecomponent;DataVIEwId:Integer;
FIEldName:CString;Top,Left,width,
Height:Single;Font:TRaveFont):TRaveDataText;
function AddRegion(Parent:TRavecomponent;Top,Left,width,Height:Single):TRaveRegion;
function AddDataBind(RvRegion:TRaveRegion;Height:Single;DataVIEwID:Integer):TRaveDataBand;
function AddDataCycle(dataVIEwId:Integer):TRaveDataCycle;
function AddBitmap(parent: TRavecomponent;aBitmap:TBitmap;
Top,Left,width,Height:Single): TRaveBitmap;
//
function XPointToInch(X:Integer):Single;//點坐標到英寸
function YPointToInch(Y:Integer):Single;//點坐標到英寸
procedure AddDataSetConnection(DSConn:TRvDataSetConnection); //加入數據連接
procedure SaveRav; //保存報表
//以下全部是關於預覽的函數與過程
procedure PreVIEw(ScrollBox:TScrollBox);
procedure PreVIEwfirstPage;
Procedure PreVIEwPrevPage;
Procedure PreVIEw下一頁:Delphi如何控制登陸失敗的次數 " href="http://www.pushad.com/Info/13345.Html">Delphi如何控制登陸失敗的次數 ;
procedure PreVIEw上一頁:JSP中Filter的簡單應用 ;
procedure GotoPage(Index:Integer);
procedure PreVIEwReDrawPage;
procedure PreVIEwZoomIn;
procedure PreVIEwZoomOut;
procedure PreVIEwFitPage;
function PrevIEwPages:Integer;
function PrevIEwCurPage:Integer;
function PrevIEwBof:Boolean; //首頁
function PrevIEwEof:Boolean; //尾頁
function PreVIEwPrinter:Boolean; //預覽後打印
procedure refreshScollBox;
//設置頁面大小
procedure SetPageSize(Height,Width:Single);
function Printer:Boolean; //打印
end;
implementation
{ ReportClass }
function ReportClass.AddBitmap(parent: TRavecomponent;aBitmap:TBitmap;Top,Left,width,Height:Single): TRaveBitmap;
var
RVBmp:TRaveBitmap;
begin
if Parent=nil then Parent:=FRpPage;
RVBmp:=TRaveBitmap.Create(parent);
RVBmp.Name:=GetCtrlName(TRaveBitmap);
RVBmp.Parent:=parent;
RVBmp.Top:=Top;
RVBmp.Left:=Left;
if Assigned(aBitmap) then
begin
RVBmp.Image.Assign(aBitmap);
end;
Result:=RVBmp;
end;
function ReportClass.AddDataBind(RvRegion: TRaveRegion; Height:Single;
DataVIEwID: Integer): TRaveDataBand;
var
RvDataBand:TRaveDataBand;
begin
RvDataBand:=TRaveDataBand.Create(RvRegion);
RvDataBand.Name:=GetCtrlName(TRaveDataBand);
RvDataBand.Parent:=RvRegion;
RvDataBand.Height:=Height;
RvDataBand.DataView:=TRaveDataView(DataViewList.items[DataVIEwID]);
Result:=RvDataBand;
end;
function ReportClass.AddDataCycle(dataVIEwId: Integer): TRaveDataCycle;
var
Cycle:TRaveDataCycle;
begin
Cycle:=TRaveDataCycle.Create(FRpPage);
Cycle.Name:=GetCtrlName(TRaveDataCycle);
Cycle.Parent:=FRpPage;
Cycle.DataView:=TRaveDataView(DataViewList.Items[dataVIEwId]);
Result:=Cycle;
end;
procedure ReportClass.AddDataSetConnection(DSConn: TRvDataSetConnection);
var
RvDataView:TRaveDataVIEw;
FIEldList:TList;
begin
FIEldList:=TList.Create;
RvDataView:=RvProject.ProjMan.NewDataObject(TRaveDataView) as TRaveDataVIEw;
RvDataView.Name:=GetCtrlName(TRaveDataVIEw);
RvDataVIEw.ConnectionName:=DSConn.Name;
rvData.CreateFields(RvDataVIEw,nil,nil,true);
DataViewList.Add(RvDataVIEw);
FIEldList.Free;
end;
function ReportClass.AddDataText(Parent: TRavecomponent;
DataViewId: Integer; FIEldName: CString; Top, Left,width,
Height: Single;Font:TRaveFont):TRaveDataText;
var
RvDataText:TRaveDataText;
begin
if Parent=nil then Parent:=FRpPage;
RvDataText:=TRaveDataText.Create(Parent);
RvDataText.Name:=GetCtrlName(TRaveDataText);
RvDataText.Parent:=Parent;
RvDataText.Top:=Top;
RvDataText.Left:=Left;
RvDataText.DataView:=TRaveDataView(DataViewList.Items[DataVIEwId]);
RvDataText.DataField:=FIEldName;
if Assigned(Font) then RvDataText.Font.Assign(Font);
Result:=RvDataText;
end;
function ReportClass.AddRegion(Parent: TRavecomponent; Top, Left, width,
Height: Single): TRaveRegion;
var
RvRegion:TRaveRegion;
begin
if Parent=nil then Parent:=FRpPage;
RvRegion:=TRaveRegion.Create(Parent);
RvRegion.Name:=GetCtrlName(TRaveRegion);
RvRegion.Parent:=Parent;
RvRegion.Top:=Top;
RvRegion.Left:=Left;
RvRegion.Width:=Width;
RvRegion.Height:=Height;
Result:=RvRegion;
end;
function ReportClass.AddText(Parent:TRavecomponent;aText: CString; Top, Left,width,
Height: Single;Font:TRaveFont):TRaveText;
var
RvText:TRaveText;
begin
if Parent=nil then Parent:=FRpPage;
RvText:=TRaveText.Create(Parent);
RvText.Name:=GetCtrlName(TRaveText);
RvText.Parent:=Parent;
RvText.Top:=Top;
RvText.Left:=Left;
RvText.Text:=aText;
if Assigned(Font) then RvText.Font.Assign(Font);
Result:=RvText;
end;
constructor ReportClass.Create(AWoner: TComponent);
begin
inherited;
PreVIEwTop:=10;
PreVIEwLeft:=10;
ClsIndexList:=TStringList.Create;
DataVIEwList:=TList.Create ;
RvProject:=TRvProject.Create(self);
RvProject.New;
FReport:=RvProject.ProjMan.FindRaveComponent(ReportName,nil) as TRaveReport;
FRpPage:=FReport.FirstPage;
//
RvNDRWriter:=TRvNDRWriter.Create(self);
RvNDRWriter.StreamMode:=SMUser;
RvNDRWriter.Stream:=TMemoryStream.Create ;
RvRenderPreView:=TRvRenderPreVIEw.Create(self);
RvRenderPrinter:=TRvRenderPrinter.Create(self);
end;
destructor ReportClass.Destroy;
begin
RvRenderPrinter.Free;
RvNDRWriter.Stream.Free;
RvNDRWriter.Free;
RvRenderPreVIEw.Free;
//
RvProject.Free;
DataVIEwList.Free;
ClsIndexList.Free;
inherited;
end;
function ReportClass.GetCtrlName(Cls: TClass): CString;
var
idx:Integer;
begin
idx:=StrToIntDef(ClsIndexList.Values[cls.ClassName],0);
Result:=cls.ClassName+inttostr(idx);
ClsIndexList.Values[cls.ClassName]:=IntToStr(idx+1);
end;
function ReportClass.GetReportName: CString;
begin
Result:=RvProject.ReportName;
end;
function ReportClass.PrevIEwPages: Integer;
begin
Result:=RvRenderPreVIEw.Pages
end;
procedure ReportClass.PreVIEw(ScrollBox: TScrollBox);
begin
RvProject.Engine:=RvNDRWriter;
RvProject.Execute;
RvRenderPreVIEw.ScrollBox:=ScrollBox;
RvRenderPreVIEw.Render(RvNDRWriter.Stream);
refreshScollBox;
RvProject.Engine:=nil;
end;
procedure ReportClass.PreVIEwfirstPage;
begin
RvRenderPreVIEw.RenderPage(0);
refreshScollBox;
end;
procedure ReportClass.PreVIEw上一頁:JSP中Filter的簡單應用 ;
begin
RvRenderPreView.RenderPage(RvRenderPreVIEw.Pages);
refreshScollBox;
end;
procedure ReportClass.PreVIEw下一頁:Delphi如何控制登陸失敗的次數 " href="http://www.pushad.com/Info/13345.Html">Delphi如何控制登陸失敗的次數 ;
begin
RvRenderPreVIEw.下一頁:Delphi如何控制登陸失敗的次數 " href="http://www.pushad.com/Info/13345.Html">Delphi如何控制登陸失敗的次數 ;
refreshScollBox;
end;
procedure ReportClass.PreVIEwPrevPage;
begin
RvRenderPreVIEw.PrevPage;
refreshScollBox;
end;
function ReportClass.PreVIEwPrinter: Boolean;
begin
RvRenderPrinter.Render(RvNDRWriter.Stream);
Result:=True;
end;
function ReportClass.Printer: Boolean;
begin
RvProject.Engine:=RvNDRWriter;
RvProject.Execute;
Result:=PreVIEwPrinter;
RvProject.Engine:=nil;
end;
procedure ReportClass.ProjectExecute;
begin
RvProject.Execute
end;
procedure ReportClass.SaveRav;
begin
RvProject.SaveToFile('RP.rav');
end;
procedure ReportClass.SetPageSize(Height, Width: single);
begin
//若設置大小超出范圍,將不起作用
FRpPage.PageHeight:=Height;
FRpPage.PageWidth:=Width;
end;
function ReportClass.PrevIEwBof: Boolean;
begin
if RvRenderPreVIEw.CurrentPage=1 then
Result:=True
else Result:=False;
end;
function ReportClass.PrevIEwEof: Boolean;
begin
if RvRenderPreView.CurrentPage=RvRenderPreVIEw.Pages then
Result:=True
else Result:=False;
end;
procedure ReportClass.SetOnPVPageChange(const Value: TNotifyEvent);
begin
RvRenderPreVIEw.OnPageChange := Value;
end;
function ReportClass.GetPVPageChange: TNotifyEvent;
begin
Result := RvRenderPreVIEw.OnPageChange;
end;
function ReportClass.PrevIEwCurPage: Integer;
begin
Result:=RvRenderPreVIEw.CurrentPage;
end;
procedure ReportClass.GotoPage(Index: Integer);
begin
if (Index>0) And (Index<=RvRenderPreVIEw.Pages) then
begin
RvRenderPreVIEw.RenderPage(Index);
refreshScollBox;
end;
end;
procedure ReportClass.PreVIEwReDrawPage;
begin
RvRenderPreVIEw.RedrawPage;
end;
procedure ReportClass.PreVIEwZoomIn;
begin
RvRenderPreVIEw.ZoomIn ;
refreshScollBox;
end;
procedure ReportClass.PreVIEwZoomOut;
begin
RvRenderPreVIEw.ZoomOut ;
refreshScollBox;
end;
procedure ReportClass.PreVIEwFitPage;
begin
RvRenderPreView.ZoomFactor:=RvRenderPreVIEw.ZoomPageFactor;
refreshScollBox
end;
procedure ReportClass.refreshScollBox;
begin
RvRenderPreView.ScrollBox.ScrollBy(PreViewTop,PreVIEwLeft);
RvRenderPreVIEw.ScrollBox.Refresh;
end;
function ReportClass.XPointToInch(X: Integer): Single;
var
dpiX:Integer;
begin
dpiX := GetDeviceCaps(GetDC(0),LOGPIXELSX);
Result := X/dpiX;
end;
function ReportClass.YPointToInch(Y: Integer): Single;
var
dpiY:Integer;
begin
dpiY := GetDeviceCaps(GetDC(0),LOGPIXELSY);
Result := Y/dpiY;
end;
end.