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

📄 rm_pdbgrid.pas

📁 进销存·完整的·有数据库的·非常完整·只得参考
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit RM_PDBGrid;

{$I RM.INC}

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Printers, Controls, DB,
  Forms, StdCtrls, Grids, DBCtrls, DBGrids, RM_const,
  RM_DBRel, RM_DSet, RM_DBSet, RM_Class, RM_FormReport;

type
  TRMPrintDBGrid = class(TComponent) // fake component
  end;

 { TRMFormPrintDBGrid }
  TRMFormPrintDBGrid = class(TRMFormReportObject)
  private
    FDBGrid: TCustomDBGrid;
    procedure OnBeforePrintBandEvent(Band: TRMBand; var PrintBand: Boolean);
  public
    procedure OnGenerate_Object(aFormReport: TRMFormReport; Page: TRMPage;
      Control: TControl; var t: TRMView); override;
  end;

  { TRMFormPrintStringGrid }
  TRMFormPrintStringGrid = class(TRMFormReportObject)
  private
    FFormReport: TRMFormReport;
    FGrid: TCustomGrid;
    FUserDataset: TRMUserDataset;
    FList: TStringList;
    FCurrentRow: Integer;
    procedure OnUserDatasetCheckEOF(Sender: TObject; var Eof: Boolean);
    procedure OnUserDatasetFirst(Sender: TObject);
    procedure OnUserDatasetNext(Sender: TObject);
    procedure OnUserDatasetPrior(Sender: TObject);
    procedure OnReportBeginBand(Band: TRMBand);
    procedure SetMemos;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure OnGenerate_Object(aFormReport: TRMFormReport; Page: TRMPage;
      Control: TControl; var t: TRMView); override;
  end;

implementation

uses RM_Utils;

type
  THackFormReport = class(TRMFormReport)
  end;

  THackDBGrid = class(TCustomDBGrid)
  end;

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMFormPrintDBGrid}

procedure TRMFormPrintDBGrid.OnBeforePrintBandEvent(Band: TRMBand; var PrintBand: Boolean);
begin
  if not THackDBGrid(FDBGrid).SelectedRows.CurrentRowSelected then
    PrintBand := FALSE;
end;

procedure TRMFormPrintDBGrid.OnGenerate_Object(aFormReport: TRMFormReport;
  Page: TRMPage; Control: TControl; var t: TRMView);
var
  liView: TRMView;
  i, tmpx, tmpx0, NextTop: Integer;
  liDBGrid: THackDBGrid;
  liPage: TRMPage;
  liPageNo: Integer;
  liNum: Integer;
  liGridTitleHeight: Integer;
  liFlagFirstColumn: Boolean;

  procedure DrawDoubleFrameBottom(aView: TRMView; aList: TList);
  var
    t: TRMMemoView;
  begin
    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      t := TRMMemoView(RMCreateObject(gtMemo, ''));
      t.CreateUniqueName;
      t.LeftFrame.Visible := False;
      t.TopFrame.Visible := True;
      t.RightFrame.Visible := False;
      t.BottomFrame.Visible := False;
      t.TopFrame.Width := 2;
      t.Gapx := 0; t.GapY := 0;
      t.SetBounds(aView.x, aFormReport.GridTop + aFormReport.GridHeight, aView.dx, 2);
      t.PStretched := rmgoStretch in aFormReport.ReportOptions;
      liPage.Objects.Add(t);
      aList.Add(t);
    end;
  end;

  procedure MakeOneHeader(aIndex: Integer);
  begin
    liView := RMCreateObject(gtMemo, '');
    liView.CreateUniqueName;
    liView.x := tmpx;
    liView.y := NextTop;
    liView.dx := liDBGrid.Columns[aIndex].Width + 1;
    liView.dy := liGridTitleHeight;
    TRMMemoView(liView).Font.Assign(liDBGrid.Columns[aIndex].Title.Font);
    liView.Memo.Text := liDBGrid.Columns[aIndex].Title.Caption;
    TRMMemoView(liView).PLayout := rmtlCenter;
    case liDBGrid.Columns[aIndex].Title.Alignment of
      taLeftJustify: TRMMemoView(liView).PAlignment := rmtaLeftJustify;
      taRightJustify: TRMMemoView(liView).PAlignment := rmtaRightJustify;
      taCenter: TRMMemoView(liView).PAlignment := rmtaCenterJustify;
    end;
    if rmgoUseColor in aFormReport.ReportOptions then
      liView.FillColor := liDBGrid.Columns[aIndex].Title.Color;
    if (rmgoGridLines in aFormReport.ReportOptions) and
      (TDBGridOption(dgColLines) in liDBGrid.Options) then
      liView.Prop['FrameTyp'] := $F
    else
      liView.Prop['FrameTyp'] := 0;
    liPage.Objects.Add(liView);
    aFormReport.PageHeaderViews.Add(liView);
    tmpx := tmpx + liView.dx;

    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      if liFlagFirstColumn then
        liView.LeftFrame.Width := 2;
      liView.TopFrame.Width := 2;
    end;
    liFlagFirstColumn := False;
  end;

  procedure MakeOneDetail(aIndex: Integer);
  begin
    if (liDBGrid.Columns[aIndex].Field <> nil) and (liDBGrid.Columns[aIndex].Field.DataType in [ftGraphic]) then //图像字段
    begin
      liView := RMCreateObject(gtPicture, '');
      liView.BandAlign := rmbaHeight;
      liView.Flags := (liView.Flags and not flPictRatio) + flPictRatio;
    end
    else
    begin
      liView := RMCreateObject(gtMemo, '');
      TRMMemoView(liView).PLayout := rmtlCenter;
      liView.PStretched := rmgoStretch in aFormReport.ReportOptions;
      TRMMemoView(liView).PWordWrap := rmgoWordWrap in aFormReport.ReportOptions;
      case liDBGrid.Columns[aIndex].Alignment of
        taLeftJustify: TRMMemoView(liView).PAlignment := rmtaLeftJustify;
        taRightJustify: TRMMemoView(liView).PAlignment := rmtaRightJustify;
        taCenter: TRMMemoView(liView).PAlignment := rmtaCenterJustify;
      end;
      TRMMemoView(liView).Font.Assign(liDBGrid.Columns[aIndex].Font);
      THackFormReport(aFormReport).SetMemoViewFormat(TRMMemoView(liView), liDBGrid.DataSource.DataSet.FieldByName(liDBGrid.Columns[aIndex].FieldName));
    end;
    if (rmgoGridLines in aFormReport.ReportOptions) and
      (TDBGridOption(dgColLines) in liDBGrid.Options) then
      liView.Prop['FrameTyp'] := $F
    else
      liView.Prop['FrameTyp'] := 0;

    liView.CreateUniqueName;
    liView.x := tmpx;
    liView.y := 0;
    liView.dx := liDBGrid.Columns[aIndex].Width + 1;
    liView.dy := liDBGrid.DefaultRowHeight + 4;
    liView.Memo.Text := Format('[%s.%s."%s"]', [liDBGrid.DataSource.DataSet.Owner.Name, liDBGrid.DataSource.DataSet.Name, liDBGrid.Columns[aIndex].FieldName]);
    if rmgoUseColor in aFormReport.ReportOptions then
      liView.FillColor := liDBGrid.Columns[aIndex].Color;

    liPage.Objects.Add(liView);
    aFormReport.PageDetailViews.Add(liView);
    tmpx := tmpx + liView.dx;
    if Assigned(aFormReport.OnAfterCreateGridObjectEvent) then
      aFormReport.OnAfterCreateGridObjectEvent(Control, liDBGrid.Columns[aIndex].FieldName, liView);

    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      if liFlagFirstColumn then
        liView.LeftFrame.Width := 2;
    end;
    DrawDoubleFrameBottom(liView, aFormReport.ColumnFooterViews);
    liFlagFirstColumn := False;
  end;

  procedure DrawFixedColHeader;
  var
    i: Integer;
  begin
    for i := 0 to aFormReport.GridFixedCols - 1 do
    begin
      if i < liDBGrid.Columns.Count then
        MakeOneHeader(i);
    end;
  end;

  procedure DrawFixedColDetail;
  var
    i: Integer;
  begin
    for i := 0 to aFormReport.GridFixedCols - 1 do //表体
    begin
{$IFDEF Delphi4}
      if not liDBGrid.Columns[i].Visible then
        Continue;
{$ENDIF}
      if i < liDBGrid.Columns.Count then
        MakeOneDetail(i);
    end;
  end;

  procedure MakeOnePageFooter(aIndex: Integer);
  begin
    if (liDBGrid.Columns[aIndex].Field <> nil) and (liDBGrid.Columns[aIndex].Field.DataType in [ftGraphic]) then //图像字段
    begin
      liView := RMCreateObject(gtPicture, '');
      liView.BandAlign := rmbaHeight;
      liView.Flags := (liView.Flags and not flPictRatio) + flPictRatio;
    end
    else
    begin
      liView := RMCreateObject(gtMemo, '');
      THackFormReport(aFormReport).SetMemoViewFormat(TRMMemoView(liView), liDBGrid.DataSource.DataSet.FieldByName(liDBGrid.Columns[aIndex].FieldName));
    end;

    TRMMemoView(liView).PLayout := rmtlCenter;
    liView.PStretched := rmgoStretch in aFormReport.ReportOptions;
    TRMMemoView(liView).PWordWrap := rmgoWordWrap in aFormReport.ReportOptions;
    case liDBGrid.Columns[aIndex].Alignment of
      taLeftJustify: TRMMemoView(liView).PAlignment := rmtaLeftJustify;
      taRightJustify: TRMMemoView(liView).PAlignment := rmtaRightJustify;
      taCenter: TRMMemoView(liView).PAlignment := rmtaCenterJustify;
    end;
    TRMMemoView(liView).Font.Assign(liDBGrid.Columns[aIndex].Font);
    if (rmgoGridLines in aFormReport.ReportOptions) and
      (TDBGridOption(dgColLines) in liDBGrid.Options) then
      liView.Prop['FrameTyp'] := $F
    else
      liView.Prop['FrameTyp'] := 0;

    liView.CreateUniqueName;
    liView.x := tmpx;
    liView.y := aFormReport.GridTop + aFormReport.GridHeight;
    liView.dx := liDBGrid.Columns[aIndex].Width + 1;
    liView.dy := liDBGrid.DefaultRowHeight + 4;
    liView.Memo.Text := Format('[%s.%s."%s"]', [liDBGrid.DataSource.DataSet.Owner.Name, liDBGrid.DataSource.DataSet.Name, liDBGrid.Columns[aIndex].FieldName]);
    if rmgoUseColor in aFormReport.ReportOptions then
      liView.FillColor := liDBGrid.Columns[aIndex].Color;

    liPage.Objects.Add(liView);
    aFormReport.ColumnFooterViews.Add(liView);
    tmpx := tmpx + liView.dx;
    if Assigned(aFormReport.OnAfterCreateGridObjectEvent) then
      aFormReport.OnAfterCreateGridObjectEvent(Control, liDBGrid.Columns[aIndex].FieldName, liView);

    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      if liFlagFirstColumn then
        liView.LeftFrame.Width := 2;
    end;
    DrawDoubleFrameBottom(liView, aFormReport.ColumnFooterViews);
    liFlagFirstColumn := False;
  end;

begin
  if aFormReport.DrawOnPageFooter then
    exit;
  liDBGrid := THackDBGrid(Control);
  if (liDBGrid.Datasource = nil) or (liDBGrid.Datasource.Dataset = nil) then
    Exit;
  if not liDBGrid.Datasource.Dataset.Active then
    Exit;

  FDBGrid := liDBGrid;
  if (rmgoSelectedRecordsOnly in aFormReport.ReportOptions) and
    (liDBGrid.SelectedRows.Count > 0) then //只打印选择的记录
  begin
    AutoFree := False;
    aFormReport.Report.OnBeforePrintBand := OnBeforePrintBandEvent;
  end;

  aFormReport.DrawOnPageFooter := TRUE;
  aFormReport.GridTop := THackFormReport(aFormReport).OffsY + Control.Top;
  aFormReport.GridHeight := Control.Height;
  NextTop := Control.Top + THackFormReport(aFormReport).OffsY;
  aFormReport.ReportDataSet.DataSource := liDBGrid.DataSource;
//  aFormReport.Report.DataSet := aFormReport.ReportDataSet;
  liGridTitleHeight := 0;
  tmpx := 0;
  for i := 0 to liDBGrid.Columns.Count - 1 do
  begin
{$IFDEF Delphi4}
    if liDBGrid.Columns[i].Visible then
{$ENDIF}
      tmpx := tmpx + liDBGrid.Columns[i].Width + 1;
  end;

  if (TDBGridOption(dgTitles) in liDBGrid.Options) and (rmgoGridNumber in aFormReport.ReportOptions) then
    tmpx := tmpx + RMCanvasHeight('a', liDBGrid.Font) * aFormReport.GridNumOptions.Number;

  if (aFormReport.PrintControl = Control) or (tmpx > StrToInt(THackFormReport(aFormReport).FormWidth[0])) then
    THackFormReport(aFormReport).FormWidth[0] := IntToStr(tmpx + (THackFormReport(aFormReport).OffsX + Control.Left) * 2);

// 表头
  liPage := aFormReport.Report.Pages[0];
  if TDBGridOption(dgTitles) in liDBGrid.Options then //表头
  begin
    liFlagFirstColumn := True;
    liGridTitleHeight := liDBGrid.RowHeights[0] + 4;
    tmpx := Control.Left + THackFormReport(aFormReport).OffsX;
    if rmgoGridNumber in aFormReport.ReportOptions then
    begin
      liView := RMCreateObject(gtMemo, '');
      liView.CreateUniqueName;
      liView.x := tmpx;
      liView.y := NextTop;
      liView.dx := RMCanvasHeight('a', liDBGrid.Font) * aFormReport.GridNumOptions.Number;
      liView.dy := liGridTitleHeight;
      liView.Memo.Add(aFormReport.GridNumOptions.Text);
      TRMMemoView(liView).PLayout := rmtlCenter;
      TRMMemoView(liView).PAlignment := rmtaCenterJustify;
      if (rmgoGridLines in aFormReport.ReportOptions) and (TDBGridOption(dgColLines) in liDBGrid.Options) then
        liView.Prop['FrameTyp'] := $F
      else
        liView.Prop['FrameTyp'] := 0;
      liPage.Objects.Add(liView);
      aFormReport.PageHeaderViews.Add(liView);
      tmpx := tmpx + liView.dx;

      if rmgoDoubleFrame in aFormReport.ReportOptions then
      begin
        if liFlagFirstColumn then
          liView.LeftFrame.Width := 2;
        liView.TopFrame.Width := 2;
      end;
      liFlagFirstColumn := False;
    end;

    tmpx0 := tmpx; liNum := 0;
    for i := 0 to liDBGrid.Columns.Count - 1 do
    begin
{$IFDEF Delphi4}
      if not liDBGrid.Columns[i].Visible then
        Continue;
{$ENDIF}
      if (aFormReport.ScaleMode.ScaleMode <> rmsmFit) or (not aFormReport.ScaleMode.FitPageWidth) then
      begin
        if (liNum > 0) and (THackFormReport(aFormReport).CalcWidth(tmpx + (liDBGrid.Columns[i].Width + 1)) > THackFormReport(aFormReport).PageWidth) then // 超宽
        begin
          liNum := 0;
          liFlagFirstColumn := True;
          if rmgoDoubleFrame in aFormReport.ReportOptions then
            liView.RightFrame.Width := 2;

          THackFormReport(aFormReport).FormWidth[THackFormReport(aFormReport).FormWidth.Count - 1] := IntToStr(tmpx);
          THackFormReport(aFormReport).AddPage;
          THackFormReport(aFormReport).FormWidth.Add('0');
          liPage := aFormReport.Report.Pages[aFormReport.Report.Pages.Count - 1];
          tmpx := tmpx0;
          liFlagFirstColumn := True;
          DrawFixedColHeader;
        end;
      end;

      MakeOneHeader(i);
      Inc(liNum);
    end;

    liFlagFirstColumn := True;
    if rmgoDoubleFrame in aFormReport.ReportOptions then
      liView.RightFrame.Width := 2;
  end;


  if THackFormReport(aFormReport).FormWidth.Count > 1 then
    THackFormReport(aFormReport).FormWidth[THackFormReport(aFormReport).FormWidth.Count - 1] := IntToStr(tmpx);

// 表体
  liPageNo := 0;
  liPage := aFormReport.Report.Pages[0];
  tmpx := Control.Left + THackFormReport(aFormReport).OffsX;
  liFlagFirstColumn := True;
  if (dgTitles in liDBGrid.Options) and (rmgoGridNumber in aFormReport.ReportOptions) then
  begin
    liView := RMCreateObject(gtMemo, '');
    liView.CreateUniqueName;
    liView.x := tmpx;
    liView.y := 0;
    liView.dx := RMCanvasHeight('a', liDBGrid.Font) * aFormReport.GridNumOptions.Number;
    liView.dy := liDBGrid.DefaultRowHeight + 4;
    liView.Memo.Add('[Line#]');
    liView.PStretched := rmgoStretch in aFormReport.ReportOptions;
    TRMMemoView(liView).PLayout := rmtlCenter;
    TRMMemoView(liView).PAlignment := rmtaCenterJustify;
    if (rmgoGridLines in aFormReport.ReportOptions) and (TDBGridOption(dgColLines) in liDBGrid.Options) then
      liView.Prop['FrameTyp'] := $F
    else
      liView.Prop['FrameTyp'] := 0;
    liPage.Objects.Add(liView);
    aFormReport.PageDetailViews.Add(liView);
    tmpx := tmpx + liView.dx;

    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      if liFlagFirstColumn then
        liView.LeftFrame.Width := 2;
    end;
    DrawDoubleFrameBottom(liView, aFormReport.ColumnFooterViews);
    liFlagFirstColumn := False;
  end;

  tmpx0 := tmpx; liPage := Page; liNum := 0;
  for i := 0 to liDBGrid.Columns.Count - 1 do //表体
  begin
{$IFDEF Delphi4}
    if not liDBGrid.Columns[i].Visible then
      Continue;
{$ENDIF}
    if (aFormReport.ScaleMode.ScaleMode <> rmsmFit) or (not aFormReport.ScaleMode.FitPageWidth) then
    begin
      if (liNum > 0) and (THackFormReport(aFormReport).CalcWidth(tmpx + (liDBGrid.Columns[i].Width + 1)) > THackFormReport(aFormReport).PageWidth) then // 超宽

⌨️ 快捷键说明

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