⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportcontrol.pas

📁 delphi编程控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
{**********************************************************
*  中国式报表处理系统 of  DELPHI                          *
*  China Report system of DELPHI                          *
*  原创:郭家骏 (Beijing),王寒松 (Beijing)               *
*  修改:廖伯志 (ShenZhen China P.R.C),王寒松             *
*  最后修改日期:1999.3.05  by wang han song              *
*  下载地址 http://www.nease.net/~wanghs/main.html        *
*  下载地址 http://www.nease.net/~bozhi                   *
***********************************************************}
Unit ReportControl;

Interface

Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, Math, Printers, Menus, Db, FileCtrl;

Const
  // Horz Align
  TEXT_ALIGN_LEFT = 0;
  TEXT_ALIGN_CENTER = 1;
  TEXT_ALIGN_RIGHT = 2;

  // Vert Align
  TEXT_ALIGN_TOP = 0;
  TEXT_ALIGN_VCENTER = 1;
  TEXT_ALIGN_BOTTOM = 2;

  // 斜线定义
  LINE_LEFT1 = 1; // left top to right bottom
  LINE_LEFT2 = 2; // left top to right
  LINE_LEFT3 = 4; // left top to bottom

  LINE_RIGHT1 = $100; // right top to left bottom
  LINE_RIGHT2 = $200; // right top to left
  LINE_RIGHT3 = $400; // right top to bottom

Type
  TReportCell = Class;
  TReportLine = Class;
  TReportControl = Class;

  TReportCell = Class(TObject)
  Private
    { Private declarations }
    FLeftMargin : Integer; // 左边的空格
    FOwnerLine : TReportLine; // 隶属行
    FOwnerCell : TReportCell; // 隶属的单元格
    FCellsList : TList; // 覆盖的Cell

    // Index
    FCellIndex : Integer; // Cell在行中的索引

    // size & position
    FCellLeft : Integer;
    FCellWidth : Integer;

    FCellRect : TRect; // 计算得来
    FTextRect : TRect;

    FDragCellHeight : Integer;
    FMinCellHeight : Integer;
    FRequiredCellHeight : Integer;

    // Cell的Top属性从隶属的行中取得
    // int GetCellTop();
    // Cell的Height属性从隶属行和跨越行中取得
    // int GetCellHeight();

    // border
    FLeftLine : Boolean;
    FLeftLineWidth : Integer;

    FTopLine : Boolean;
    FTopLineWidth : Integer;

    FRightLine : Boolean;
    FRightLineWidth : Integer;

    FBottomLine : Boolean;
    FBottomLineWidth : Integer;

    // 斜线
    FDiagonal : UINT;

    // color
    FTextColor : COLORREF;
    FBackGroundColor : COLORREF;

    // align
    FHorzAlign : Integer;
    FVertAlign : Integer;

    // string
    FCellText : String;

    // font
    FLogFont : TLOGFONT;
    Function GetCellHeight : Integer;
    Function GetCellTop : Integer;
    Function GetOwnerLineHeight : Integer;
  Protected
    { Protected declarations }
    Procedure SetLeftMargin(LeftMargin : Integer);
    Procedure SetOwnerLine(OwnerLine : TReportLine);

    Procedure SetOwnerCell(Cell : TReportCell);
    Function GetOwnedCellCount : Integer;

    Procedure SetCellLeft(CellLeft : Integer);
    Procedure SetCellWidth(CellWidth : Integer);

    Procedure SetLeftLine(LeftLine : Boolean);
    Procedure SetLeftLineWidth(LeftLineWidth : Integer);

    Procedure SetTopLine(TopLine : Boolean);
    Procedure SetTopLineWidth(TopLineWidth : Integer);

    Procedure SetRightLine(RightLine : Boolean);
    Procedure SetRightLineWidth(RightLineWidth : Integer);

    Procedure SetBottomLine(BottomLine : Boolean);
    Procedure SetBottomLineWidth(BottomLineWidth : Integer);

    Procedure SetCellText(CellText : String);
    Procedure SetLogFont(NewFont : TLOGFONT);

    Procedure SetBackGroundColor(BkColor : COLORREF);
    Procedure SetTextColor(TextColor : COLORREF);

  Public
    { Public declarations }
    Procedure AddOwnedCell(Cell : TReportCell);
    Procedure RemoveAllOwnedCell;
    Procedure RemoveOwnedCell(Cell : TReportCell);
    Function IsCellOwned(Cell : TReportCell) : Boolean;
    Procedure CalcCellRect;
    Procedure CalcMinCellHeight;
    Procedure PaintCell(hPaintDC : HDC; bPrint : Boolean);
    Procedure CopyCell(Cell : TReportCell; bInsert : Boolean);

    Constructor Create;
    Destructor Destroy; Override;

    // Properties
    Property LeftMargin : Integer Read FLeftMargin Write SetLeftMargin;
    Property OwnerLine : TReportLine Read FOwnerLine Write SetOwnerLine;
    Property OwnerCell : TReportCell Read FOwnerCell Write SetOwnerCell;
    Property OwnedCellCount : Integer Read GetOwnedCellCount;

    Property CellIndex : Integer Read FCellIndex Write FCellIndex;

    // size & position
    Property CellLeft : Integer Read FCellLeft Write SetCellLeft;
    Property CellWidth : Integer Read FCellWidth Write SetCellWidth;
    Property CellTop : Integer Read GetCellTop;
    Property CellHeight : Integer Read GetCellHeight;

    Property CellRect : TRect Read FCellRect;
    Property TextRect : TRect Read FTextRect;

    Property DragCellHeight : Integer Read FDragCellHeight;
    // or protected property ?
    Property MinCellHeight : Integer Read FMinCellHeight Write FMinCellHeight;
    Property RequiredCellHeight : Integer Read FRequiredCellHeight;
    Property OwnerLineHeight : Integer Read GetOwnerLineHeight;

    // border
    Property LeftLine : Boolean Read FLeftLine Write SetLeftLine Default True;
    Property LeftLineWidth : Integer Read FLeftLineWidth Write SetLeftLineWidth Default 1;

    Property TopLine : Boolean Read FTopLine Write SetTopLine Default True;
    Property TopLineWidth : Integer Read FTopLineWidth Write SetTopLineWidth Default 1;

    Property RightLine : Boolean Read FRightLine Write SetRightLine Default True;
    Property RightLineWidth : Integer Read FRightLineWidth Write SetRightLineWidth Default 1;

    Property BottomLine : Boolean Read FBottomLine Write SetBottomLine Default True;
    Property BottomLineWidth : Integer Read FBottomLineWidth Write SetBottomLineWidth Default 1;

    // 斜线
    Property Diagonal : UINT Read FDiagonal Write FDiagonal;

    // color
    Property TextColor : COLORREF Read FTextColor Write SetTextColor Default clBlack;
    Property BkColor : COLORREF Read FBackGroundColor Write SetBackGroundColor Default clWhite;

    // align
    Property HorzAlign : Integer Read FHorzAlign Write FHorzAlign Default 1;
    Property VertAlign : Integer Read FVertAlign Write FVertAlign Default 1;

    // string
    Property CellText : String Read FCellText Write SetCellText;

    // font
    Property LogFont : TLOGFONT Read FLogFont Write SetLogFont;
  End;

  TReportLine = Class(TObject)
  Private
    { Private declarations }
    FReportControl : TReportControl; // Report Control的指针
    FCells : TList; // 保存所有在该行中的CELL
    FIndex : Integer; // 行的索引

    FMinHeight : Integer;
    FDragHeight : Integer;
    FLineTop : Integer;
    FLineRect : TRect;

    Function GetLineHeight : Integer;
    Function GetLineRect : TRect;
    Procedure SetDragHeight(Const Value : Integer);
    Procedure SetLineTop(Const Value : Integer);
  Protected
    { Protected declarations }
  Public
    { Public declarations }
    Property ReportControl : TReportControl Read FReportControl Write FReportControl;
    Property Index : Integer Read FIndex Write FIndex;
    Property LineHeight : Integer Read GetLineHeight Write SetDragHeight;
    Property LineTop : Integer Read FLineTop Write SetLineTop;
    Property LineRect : TRect Read GetLineRect;
    Property PrevLineRect : TRect Read FLineRect;

    Procedure CalcLineHeight;
    Procedure CreateLine(LineLeft, CellNumber, PageWidth : Integer);
    Procedure CopyLine(Line : TReportLine; bInsert : Boolean);

    Constructor Create;
    Destructor Destroy; Override;
  End;

  TReportControl = Class(TWinControl)
  Private
    { Private declarations }
    FPreviewStatus : Boolean;

    FLineList : TList;
    FSelectCells : TList;
    FEditCell : TReportCell;

    FReportScale : Integer;
    FPageWidth : Integer;
    FPageHeight : Integer;

    FLeftMargin : Integer;
    FRightMargin : Integer;
    FTopMargin : Integer;
    FBottomMargin : Integer;

    FLeftMargin1 : Integer;
    FRightMargin1 : Integer;
    FTopMargin1 : Integer;
    FBottomMargin1 : Integer;

    // 换页加表头(不加表头)
    FNewTable : Boolean;

    // 定义打印多少行后从新加表头
    FDataLine : Integer;

    FTablePerPage : Integer;

    // 鼠标操作支持
    FMousePoint : TPoint;

    // 编辑、颜色及字体
    FEditWnd : HWND;
    FEditBrush : HBRUSH;
    FEditFont : HFONT;
    Procedure SetScale(Const Value : Integer);

    //    FReportMenu : TPopupMenu;
  Protected
    { Protected declarations }
    Procedure CreateWnd; Override;
  Public
    { Public declarations }
    Constructor Create(AOwner : TComponent); Override;
    Destructor Destroy; Override;
    Procedure SaveToFile(FileName : String);
    Procedure LoadFromFile(FileName : String);
    Procedure PrintIt;
    Procedure ResetContent;

    // Message Handler
    Procedure WMLButtonDown(Var Message : TMessage); Message WM_LBUTTONDOWN;
    Procedure WMLButtonDBLClk(Var Message : TMessage); Message WM_LBUTTONDBLCLK;
    Procedure WMMouseMove(Var Message : TMessage); Message WM_MOUSEMOVE;
    Procedure WMContextMenu(Var Message : TMessage); Message WM_CONTEXTMENU;
    Procedure WMPaint(Var Message : TMessage); Message WM_PAINT;
    Procedure WMCOMMAND(Var Message : TMessage); Message WM_COMMAND;
    Procedure WMCtlColor(Var Message : TMessage); Message WM_CTLCOLOREDIT;

    // Window size
    Procedure CalcWndSize;

    Procedure NewTable(ColNumber, RowNumber : Integer);

    Procedure InsertLine;
    Function CanInsert : Boolean;
    Procedure AddLine;
    Function CanAdd : Boolean;
    Procedure DeleteLine;

    Procedure InsertCell;
    Procedure DeleteCell;
    Procedure AddCell;

    Procedure CombineCell;

    Procedure SplitCell;
    Procedure VSplitCell(Number : Integer);
    Function CanSplit : Boolean;

    Procedure SetCellLines(bLeftLine, bTopLine, bRightLine, bBottomLine : Boolean;
      nLeftLineWidth, nTopLineWidth, nRightLineWidth, nBottomLineWidth : Integer);

    Procedure SetCellDiagonal(NewDiagonal : UINT);
    Procedure SetCellColor(NewTextColor, NewBackColor : COLORREF);
    Procedure SetCellBackColor(NewBackColor : COLORREF);
    Procedure SetCellTextColor(NewTextColor : COLORREF);
    Procedure SetCellFont(CellFont : TLOGFONT);
    Procedure SetCellAlign(NewHorzAlign, NewVertAlign : Integer);
    Procedure SetCellAlignNewVertAlign(NewVertAlign : Integer);
    Procedure SetCellAlignHorzAlign(NewHorzAlign : Integer);
    Procedure SetMargin(nLeftMargin, nTopMargin, nRightMargin, nBottomMargin : Integer);
    Function GetMargin : TRect;

    Function GetCellFont : TFont; //add wang han song

    Procedure UpdateLines;

    Procedure StartMouseDrag(point : TPoint);
    Procedure StartMouseSelect(point : TPoint; bSelectFlag : Boolean);
    Procedure MouseMoveHandler(message : TMSG);

    // 选中区的操作
    Function AddSelectedCell(Cell : TReportCell) : Boolean;
    Function RemoveSelectedCell(Cell : TReportCell) : Boolean;
    Procedure RemoveAllSelectedCell;

    Function IsCellSelected(Cell : TReportCell) : Boolean;
    Function CellFromPoint(point : TPoint) : TReportCell;

    Property IsPreview : Boolean Read FPreviewStatus Write FPreviewStatus Default False;
    Property ReportScale : Integer Read FReportScale Write SetScale Default 100;
    Property IsNewTable : Boolean Read FNewTable Write FNewTable Default True;
    Property DataLine : Integer Read FDataLine Write FDataLine Default 2147483647;
    Property TablePerPage : Integer Read FTablePerPage Write FTablePerPage Default 2147483647;

  Published
    { Published declarations }
    Property Left;
    Property Top;
    Property Cursor;
    Property Hint;
    Property Visible Default True;
    Property Enabled Default True;
  End;

  TDatasetItem = Class(TObject)
    pDataset : TDataset;
    strName : String;
  End;

  TVarTableItem = Class(TObject)
    strVarName : String;
    strVarValue : String;
  End;

  TMyRect = Class(TObject)
  Public
    Left : Integer;
    Top : Integer;
    Right : Integer;
    Bottom : Integer;
  End;

  TReportRunTime = Class(TComponent)
  Private
    FFileName : TFileName;
    FAutoGroup : Boolean;
    FEnableEdit : Boolean;
    FDList : TList; // 保存数据集的指针和名称的对照
    FVarList : TList; // 保存变量的名字和值的对照表
    FLineList : TList; // 保存报表的设计信息(从文件中读入)
    FPrintLineList : TList; //保存要打印的某一页的行信息
    FOwnerCellList : TList; // 保存每一页中合并后单元格前后的指针


    Width : Integer;
    Height : Integer;

    // 定义换页加表头
    FNewTable : Boolean;

    // 定义打印多少行后从新加表头
    FDataLine : Integer;
    FTablePerPage : Integer;

    FReportScale : Integer;
    FPageWidth : Integer;
    FPageHeight : Integer;

    FHeaderHeight : Integer;

    Fallprint : Boolean; //是否打印全部记录,默认为全部

    FLeftMargin : Integer;
    FRightMargin : Integer;
    FTopMargin : Integer;
    FBottomMargin : Integer;

    FLeftMargin1 : Integer;
    FRightMargin1 : Integer;
    FTopMargin1 : Integer;
    FBottomMargin1 : Integer;

    FPageCount : Integer; // page count in print preview

    Procedure UpdateLines;
    Procedure UpdatePrintLines;
    Procedure PrintOnePage;
    Procedure LoadRptFile;
    Function GetDatasetName(strCellText : String) : String;
    Function GetDataset(strCellText : String) : TDataset;
    Function DatasetByName(strDatasetName : String) : TDataset;
    Function GetVarValue(strVarName : String) : String;
    Function GetFieldName(strCellText : String) : String;
    Procedure SetRptFileName(Const Value : TFileName);
    Procedure SaveTempFile(PageNumber : Integer);
    Procedure LoadTempFile(strFileName : String);
    Procedure DeleteAllTempFiles;
    Procedure SetAutoGroup(Value : Boolean);
    Procedure SetEnableEdit(Value : Boolean);
  Public
    Constructor Create(AOwner : TComponent); Override;
    Destructor Destroy; Override;
    Procedure SetDataset(strDatasetName : String; pDataSet : TDataSet);
    Procedure SetVarValue(strVarName, strVarValue : String);
    Property allprint : boolean Read Fallprint Write Fallprint Default true;
    Procedure ResetContent;
    Procedure PrintPreview(bPreviewMode : Boolean);
    Procedure PreparePrint;
    Procedure loadfile(value : tfilename);
    Procedure Print;
    Procedure ResetSelf;
    Function CancelPrint : Boolean;
  Published
    Property ReportFile : TFileName Read FFileName Write SetRptFileName;
    Property AutoGroup : Boolean Read FAutoGroup Write SetAutoGroup Default True;
    Property EnableEdit : Boolean Read FEnableEdit Write SetEnableEdit;
  End;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -