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

📄 printform.pas

📁 中小企业管理系统------ ERP系统原代码
💻 PAS
📖 第 1 页 / 共 3 页
字号:
{$DEFINE NoRmComPass}
unit PrintForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  dxDBGrid, RM_class, RM_Desgn, RM_dset, RM_dbset, dxEditor, StdCtrls,
  dxCntner, dxEdLib, dxDBELib, dxTL, dxDBCtrl, ExtCtrls, dxExEdtr, RM_shape,
  dxDBTL, KsSkinLabels, RM_e_csv, RM_e_txt, RM_chart, RM_FormReport, Series;

const
  SHOW_REPORT = 1;
  PRINT_REPORT = 2;
  DESIGN_REPORT = 3;
  INITI_REPORT = 4;

type
  TfrmPrintForm = class(TForm)
    rmReport: TRMReport;
    RMDesigner1: TRMDesigner;
    RMDBDataSet1: TRMDBDataSet;
    RMTextExport1: TRMTextExport;
    RMCSVExport1: TRMCSVExport;
    RMChartObject1: TRMChartObject;
    RMChartObject2: TRMChartObject;
  private
    { Private declarations }
    BandReportTitle: TRMBandView;
    BandPageHeader: TRMBandView;
    BandDetail: TRMBandView;
    BandDetailChart: TRMBandView;
    BandPageFooter: TRMBandView;
    BandReportSummary: TRMBandView;

    lTitleY, lGridY, lGridH: Integer;
    frmForm1: TForm;
    Page1: TRMPage;
    sReportFile, sFile: string;
    lTitle, lMode: Integer;
    bPrintBar, bReturn, bNewReport: Boolean;
    lPrintChartType: Integer;
    sChart, sPrintChartX, sPrintChartY, sChartNameX, sChartNameY: string;
    procedure PrintPrepare; //准备打印
    function PrintBegin: Boolean; //开始打印

    procedure LoadBand; //读入页边条
    procedure LoadControl(Control: TControl); //读入Control
    procedure LoadTopControl(Control: TControl); //读入最上面Control
    procedure LoadBottomControl(Control: TControl); //读入最下面Control
    procedure LoadOldControl(Control: TControl); //读入已存在Control

    procedure LoadDBGrid(Grid1: TdxDBGrid); //读入DBGrid
    procedure SetGridHeader(Grid1: TdxDBGrid); //设置表头
    procedure SetGridBody(Grid1: TdxDBGrid); //设置表体
    procedure SetGridSummary(Grid1: TdxDBGrid); //设置表尾

    procedure LoadDBTree(Tree1: TdxDBTreeList); //读入DBTree
    procedure SetTreeHeader(Tree1: TdxDBTreeList); //设置表头
    procedure SetTreeBody(Tree1: TdxDBTreeList); //设置表体
    procedure SetTreeSummary(Tree1: TdxDBTreeList); //设置表尾

    function LoadReportFile: Boolean; //读报表文件
    function SaveReportFile: boolean; //写报表文件

    procedure SetTitle; //设置标题
    function GetAlignment(Align: TAlignment): TRMAlignment;
    function GetBandSub(Grid1: TCustomdxDBTreeListControl; Index: Integer):
      Integer;
    function ViewAssign(m1: TRMMemoView; Control: TControl): Boolean;
    //为各种控件赋值
    function ShapeViewAssign(s1: TRMShapeView; Control: TControl): Boolean;
    //为各种Shape控件赋值
    function PicViewAssign(p1: TRMPictureView; Control: TControl): Boolean;
    //为各种图象控件赋值
    function GetPoint(Control: TControl): TPoint;
    procedure SetChart;
  public
    { Public declarations }
  end;

function WinPrint(Form1: TForm; lTitle1, lMode1: Integer; sFile1, sChart1:
  string): Boolean;
{
  参数( 要打印Form;标题类型(0=空,1=中间,2=左边);打印类型(显示,打印,编辑,初始化);文件ID )
  打印类型(显示=SHOW_REPORT,打印=PRINT_REPORT,编辑=DESIGN_REPORT,初始化=INITI_REPORT)
}
implementation

uses SysPublic;

{$R *.DFM}
const
  C_LEFT = 20;
  C_HEIGHT = 20;

function WinPrint(Form1: TForm; lTitle1, lMode1: Integer; sFile1, sChart1:
  string): Boolean;
var
  frmPrintForm: TfrmPrintForm;
begin
  frmPrintForm := TfrmPrintForm.Create(Application);
  with frmPrintForm do
  begin
    frmForm1 := Form1;
    lMode := lMode1;
    lTitle := lTitle1;
    sFile := sFile1;
    sChart := sChart1;
    PrintPrepare;
    PrintBegin;
    Result := bReturn;
    {Free;}
  end;
end;

procedure TfrmPrintForm.PrintPrepare;
var
  i: Integer;
begin
  lPrintChartType := -1;
  sPrintChartX := '';
  sPrintChartY := '';
  sChartNameX := '';
  sChartNameY := '';

  lPrintChartType := StrToInt2(GetCommaStr(sChart, 1));
  sPrintChartX := GetCommaStr(sChart, 2);
  sPrintChartY := GetCommaStr(sChart, 3);
  if (sPrintChartX = '') or (sPrintChartY = '') then
    lPrintChartType := -1; //不是图形
  if lPrintChartType >= 0 then
    bNewReport := True;

  LoadReportFile;
  LoadBand;
  SetTitle;

  with frmForm1 do
  begin
    for i := 0 to ComponentCount - 1 do
    begin
      if (TControl(Components[i]).Visible) and (Components[i] is
        TCustomdxTreeList) then
      begin
        lGridY := GetPoint(TControl(Components[i])).Y;
        lGridH := TdxDBGrid(Components[i]).RowHeight;
      end;
    end;
    for i := 0 to ComponentCount - 1 do
      if (TControl(Components[i]).Visible) and (Components[i] is TControl) then
      begin
        if bNewReport then
          LoadTopControl(TControl(Components[i]))
        else
          LoadOldControl(TControl(Components[i]));
      end;
    for i := 0 to ComponentCount - 1 do
      if (TControl(Components[i]).Visible) and (Components[i] is TdxDBGrid) then
        LoadDBGrid(TdxDBGrid(Components[i]));
    for i := 0 to ComponentCount - 1 do
      if (TControl(Components[i]).Visible) and (Components[i] is TdxDBTreeList) then
        LoadDBTree(TdxDBTreeList(Components[i]));
    for i := 0 to ComponentCount - 1 do
    begin
      if (TControl(Components[i]).Visible) and (Components[i] is TControl) then
      begin
        if bNewReport then
          LoadBottomControl(TControl(Components[i]))
        else
          LoadOldControl(TControl(Components[i]));
      end;
    end;
    if bNewReport then
      for i := 0 to ComponentCount - 1 do
      begin
        if (sChartNameX <> '') and (sChartNameY <> '') then
        begin
          SetChart;
          Break;
        end;
      end;
  end;
end;

procedure TfrmPrintForm.SetChart;
var
  lBY, lBH: Integer;
  cv: TRMChartView;
begin
  //饼图
  BandPageHeader.Visible := False;
  BandDetail.dy := 0;
  BandReportSummary.Visible := False;
  lBY := BandReportSummary.y + BandReportSummary.dy + C_HEIGHT;
  lBH := 300;
  BandDetailChart := TRMBandView(RMCreateObject(gtBand, ''));
  BandDetailChart.BandType := btMasterData;
  BandDetailChart.Name := 'BandDetailChart';
  Page1.Objects.Add(BandDetailChart);
  BandDetailChart.SetBounds(0, lBY, 0, lBH);
  {$IFNDEF NoRmComPass}
  cv := TRMChartView.Create;
  cv.Name := 'ChartView';
  cv.SetBounds(C_LEFT, lBy + C_HEIGHT, 600, lBH - C_HEIGHT);
  cv.AddSeries;
  cv.Series[0].ChartOptions.ShowMarks := True;
  cv.Series[0].ChartOptions.Colored := True;
  cv.Series[0].ChartOptions.ChartType := lPrintChartType;
  cv.Series[0].ChartOptions.MarksStyle := 3;
  cv.Series[0].LegendObj := sChartNameX;
  cv.Series[0].ValueObj := sChartNameY;
  Page1.Objects.Add(cv);
  {$ELSE}

  {$ENDIF}
end;

function TfrmPrintForm.PrintBegin: Boolean;
begin
  Result := True;
  case lMode of
    SHOW_REPORT: rmReport.ShowReport;
    PRINT_REPORT: rmReport.PrintReport;
    DESIGN_REPORT: rmReport.DesignReport;
    INITI_REPORT: rmReport.DesignReport;
  else
    Result := False;
  end;
  if Result <> False then
    SaveReportFile;
end;

procedure TfrmPrintForm.LoadBand;
begin
  if bNewReport then
  begin
    rmReport.Pages.Clear;
    rmReport.Pages.Add;
    Page1 := rmReport.Pages[0];
    BandReportTitle := TRMBandView(RMCreateObject(gtBand, ''));
    BandReportTitle.BandType := btReportTitle;
    BandReportTitle.Name := 'BandReportTitle';
    BandReportTitle.SetBounds(0, C_HEIGHT, 0, 0);
    Page1.Objects.Add(BandReportTitle);

    BandPageHeader := TRMBandView(RMCreateObject(gtBand, ''));
    BandPageHeader.BandType := btPageHeader;
    BandPageHeader.Name := 'BandPageHeader';
    Page1.Objects.Add(BandPageHeader);
    BandPageHeader.SetBounds(0, C_HEIGHT * 2, 0, 0);

    BandDetail := TRMBandView(RMCreateObject(gtBand, ''));
    BandDetail.BandType := btMasterData;
    BandDetail.Name := 'BandDetail';
    Page1.Objects.Add(BandDetail);
    BandDetail.SetBounds(0, C_HEIGHT * 3, 0, 0);

    BandPageFooter := TRMBandView(RMCreateObject(gtBand, ''));
    BandPageFooter.BandType := btPageFooter;
    BandPageFooter.Name := 'BandPageFooter';
    Page1.Objects.Add(BandPageFooter);
    BandPageFooter.SetBounds(0, C_HEIGHT * 4, 0, 0);

    BandReportSummary := TRMBandView(RMCreateObject(gtBand, ''));
    BandReportSummary.BandType := btReportSummary;
    BandReportSummary.Name := 'BandReportSummary';
    Page1.Objects.Add(BandReportSummary);
    BandReportSummary.SetBounds(0, C_HEIGHT * 5, 0, 0);
  end
  else
  begin
    Page1 := rmReport.Pages[0];
    BandReportTitle := TRMBandView(page1.FindObject('BandReportTitle'));
    BandPageHeader := TRMBandView(page1.FindObject('BandPageHeader'));
    BandDetail := TRMBandView(page1.FindObject('BandDetail'));
    BandPageFooter := TRMBandView(page1.FindObject('BandPageFooter'));
    BandReportSummary := TRMBandView(page1.FindObject('BandReportSummary'));
  end;
end;

function TfrmPrintForm.LoadReportFile: Boolean;
var
  sDir: string;
begin
  Result := True;
  bNewReport := False;
  sReportFile := '';
  sDir := Trim(ExtractFilePath(Application.ExeName)) + 'Report';
  if not DirectoryExists(sDir) then
  begin
    if not CreateDir(sDir) then
    begin
      raise Exception.Create('不能新建目录,请手工新建这目录。' + #13 + sDir);
      Result := false;
    end;
  end;
  if Result <> False then
  begin
    if lPrintChartType = -1 then
      sReportFile := sDir + '\' + sFile + '.Rep'
    else
      sReportFile := sDir + '\' + sFile + '图形.Rep';
  end;
  if (not FileExists(sReportFile)) or (lMode = INITI_REPORT) then
    bNewReport := True
  else
    if not rmReport.LoadFromFile(sReportFile) then
      bNewReport := True;

  rmReport.FileName := sReportFile;
end;

function TfrmPrintForm.SaveReportFile: Boolean;
begin
  Result := True;
  if lPrintChartType >= 0 then
    Exit;
  if (sReportFile <> '') and bNewReport then
    rmReport.SaveToFile(sReportFile);
  if not FileExists(sReportFile) then
    Result := False;
end;

procedure TfrmPrintForm.SetTitle;
var
  mt: TRMMemoView;
  Font: TFont;
begin
  if not bNewReport then
    exit;
  mt := TRMMemoView(RMCreateObject(gtMemo, ''));
  mt.gapx := 0;
  mt.gapy := 0;
  mt.dx := 0;
  lTitleY := C_HEIGHT;
  lGridY := 0;
  if lTitle > 0 then
  begin
    Font := TFont.Create;
    Font.Name := '宋体';
    Font.Size := 22;
    Font.Charset := GB2312_CHARSET;
    Font.Style := [fsBold];
    mt.Font.Assign(Font);
    if lTitle = 1 then
      mt.PAlignment := rmtaCenterJustify
    else
      if lTitle = 2 then
        mt.PAlignment := rmtaLeftJustify;
    mt.Memo.Add(frmForm1.Caption);
    mt.SetBounds(C_LEFT, C_HEIGHT, frmForm1.Width, 40);
    mt.Name := 'TiTtle' + frmForm1.Name;
    Page1.Objects.Add(mt);

⌨️ 快捷键说明

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