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

📄 rm_pdbgrid.pas

📁 这是一个功能强大
💻 PAS
📖 第 1 页 / 共 3 页
字号:
unit RM_PDBGrid;

{$I RM.INC}

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Printers, Controls, DB,
  Forms, StdCtrls, Grids, DBCtrls, DBGrids, RM_const,
  RM_DataSet, RM_Class, RM_FormReport, RM_GridReport, RM_Grid;

type
  { TRMPrintDBGrid }
  TRMPrintDBGrid = class(TRMCustomGridReport)
  private
    FDBGrid: TCustomDBGrid;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    function CreateReportFromGrid: Boolean; override;
  public
  published
    property DBGrid: TCustomDBGrid read FDBGrid write FDBGrid;
  end;

  { TRMFormPrintDBGrid }
  TRMFormPrintDBGrid = class(TRMFormReportObject)
  private
    FDBGrid: TCustomDBGrid;
    FParentReport: TRMReport;

    procedure OnBeforePrintBandEvent(Band: TRMBand; var PrintBand: Boolean);
  public
    procedure OnGenerate_Object(aFormReport: TRMFormReport; aPage: TRMReportPage;
      aControl: 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; aPage: TRMReportPage;
      aControl: TControl; var t: TRMView); override;
  end;

implementation

uses RM_Utils;

type
  THackFormReport = class(TRMFormReport)
  end;

  THackDBGrid = class(TCustomDBGrid)
  end;

  {------------------------------------------------------------------------------}
  {------------------------------------------------------------------------------}
  { TRMPrintDBGrid }

procedure TRMPrintDBGrid.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation = opRemove then
  begin
    if AComponent = FDBGrid then
      FDBGrid := nil;
  end;
end;

function TRMPrintDBGrid.CreateReportFromGrid: Boolean;
var
  i, lColIndex, lRowOffset, lColCount: Integer;
  lRowHeight: Integer;
  lPage: TRMGridReportPage;
  lDBGrid: THackDBGrid;
  lGridEx: TRMGridEx;
  lBandColumnHeader: TRMBandColumnHeader;
  lBandMasterData: TRMBandMasterData;

  procedure _MakeOneColumn(lGridExCol, lDBGridCol: Integer);
  var
    t: TRMView;
  begin
    lGridEx.ColWidths[lGridExCol] := lDBGrid.Columns[lDBGridCol].Width + 1;
    if TDBGridOption(dgTitles) in lDBGrid.Options then //表头
    begin
      lGridEx.Cells[lGridExCol, 1].Text := lDBGrid.Columns[lDBGridCol].Title.Caption;
    end;

    // 表体
    lGridEx.Cells[lGridExCol, lRowOffset].Text := Format('[%s."%s"]', [AddRMDataSet(lDBGrid.DataSource.DataSet).Name, lDBGrid.Columns[lDBGridCol].FieldName]);
    t := lGridEx.Cells[lGridExCol, lRowOffset].View;
    if (lDBGrid.Columns[lDBGridCol].Field <> nil) and (lDBGrid.Columns[lDBGridCol].Field.DataType in [ftGraphic]) then //图像字段
    begin
    end
    else
    begin
      TRMMemoView(t).VAlign := rmvCenter;
      TRMMemoView(t).Stretched := rmgoStretch in ReportOptions;
      TRMMemoView(t).WordWrap := rmgoWordWrap in ReportOptions;
      case lDBGrid.Columns[lDBGridCol].Alignment of
        taLeftJustify: TRMMemoView(t).HAlign := rmhLeft;
        taRightJustify: TRMMemoView(t).HAlign := rmhRight;
        taCenter: TRMMemoView(t).HAlign := rmhCenter;
      end;
      TRMMemoView(t).Font.Assign(lDBGrid.Columns[lDBGridCol].Font);
      if GridFontOptions.UseCustomFontSize then
        TRMMemoView(t).Font.Size := GridFontOptions.Font.Size;
      SetMemoViewFormat(TRMMemoView(t), lDBGrid.DataSource.DataSet.FieldByName(lDBGrid.Columns[lDBGridCol].FieldName));
    end;

    if rmgoUseColor in ReportOptions then
      t.FillColor := lDBGrid.Columns[lDBGridCol].Color;
  end;

begin
  Result := False;
  lDBGrid := THackDBGrid(FDBGrid);
  if (lDBGrid.Datasource = nil) or (lDBGrid.Datasource.Dataset = nil) then Exit;
  if not lDBGrid.Datasource.Dataset.Active then Exit;

  lPage := Report.AddGridReportPage;
  lGridEx := lPage.Grid;

  lRowHeight := lDBGrid.DefaultRowHeight + 4;
  if MasterDataBandOptions.Height > 0 then
    lRowHeight := MasterDataBandOptions.Height;

  lColCount := 0;
  for i := 0 to lDBGrid.Columns.Count - 1 do
  begin
    {$IFDEF COMPILER4_UP}
    if not lDBGrid.Columns[i].Visible then Continue;
    {$ENDIF}
    lColCount := lColCount + 1;
  end;

  if (TDBGridOption(dgTitles) in lDBGrid.Options) and (rmgoGridNumber in ReportOptions) then
  begin
    lColCount := lColCount + 1;
  end;

  lGridEx.ColCount := lColCount + 1;
  if TDBGridOption(dgTitles) in lDBGrid.Options then //表头
  begin
    lGridEx.RowCount := 3;
    lRowOffset := 2;
    lGridEx.RowHeights[1] := lRowHeight;
    lGridEx.RowHeights[2] := lRowHeight;

    lBandColumnHeader := TRMBandColumnHeader.Create;
    lBandColumnHeader.ParentPage := lPage;
    lPage.RowBandViews[1] := lBandColumnHeader;
  end
  else
  begin
    lGridEx.RowCount := 2;
    lRowOffset := 1;
    lGridEx.RowHeights[1] := lRowHeight;
  end;

  lBandMasterData := TRMBandMasterData.Create;
  lBandMasterData.ParentPage := lPage;
  lPage.RowBandViews[lRowOffset] := lBandMasterData;
  lBandMasterData.DataSetName := AddRMDataSet(lDBGrid.DataSource.DataSet).Name;

  if (TDBGridOption(dgTitles) in lDBGrid.Options) and (rmgoGridNumber in ReportOptions) then
  begin
    lColIndex := 2;
    lGridEx.ColWidths[1] := RMCanvasHeight('a', lDBGrid.Font) * GridNumOptions.Number;
    if TDBGridOption(dgTitles) in lDBGrid.Options then //表头
    begin
      lGridEx.Cells[1, 1].Text := GridNumOptions.Text;
    end;

    lGridEx.Cells[1, lRowOffset].Text := '[_RM_Line]';
    TRMMemoView(lGridEx.Cells[1, lRowOffset].View).Stretched := rmgoStretch in ReportOptions;
  end
  else
    lColIndex := 1;
  for i := 0 to lDBGrid.Columns.Count - 1 do
  begin
    {$IFDEF COMPILER4_UP}
    if not lDBGrid.Columns[i].Visible then Continue;
    {$ENDIF}

    _MakeOneColumn(lColIndex, i);
    Inc(lColIndex);
  end;

  Result := True;
end;

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

procedure TRMFormPrintDBGrid.OnBeforePrintBandEvent(Band: TRMBand; var PrintBand: Boolean);
begin
  if (Band.BandType = rmbtMasterData) and (not FParentReport.Flag_TableEmpty) and
    (not THackDBGrid(FDBGrid).SelectedRows.CurrentRowSelected) then
    PrintBand := False;
end;

procedure TRMFormPrintDBGrid.OnGenerate_Object(aFormReport: TRMFormReport;
  aPage: TRMReportPage; aControl: TControl; var t: TRMView);
var
  liView: TRMView;
  i, tmpx, tmpx0, lNextTop: Integer;
  liDBGrid: THackDBGrid;
  liPageNo: Integer;
  liNum: Integer;
  liGridTitleHeight: Integer;
  liFlagFirstColumn: Boolean;
  liRowHeight: Integer;
  liPage: TRMReportPage;

  procedure _DrawDoubleFrameBottom(aView: TRMView; aList: TList);
  var
    t: TRMMemoView;
  begin
    if rmgoDoubleFrame in aFormReport.ReportOptions then
    begin
      t := TRMMemoView(RMCreateObject(rmgtMemo, ''));
      t.ParentPage := liPage;
      t.LeftFrame.Visible := False;
      t.TopFrame.Visible := True;
      t.RightFrame.Visible := False;
      t.BottomFrame.Visible := False;
      t.TopFrame.Width := 2;
      t.GapLeft := 0;
      t.GapTop := 0;
      t.SetspBounds(aView.spLeft, aFormReport.GridTop + aFormReport.GridHeight, aView.spWidth, 2);
      t.Stretched := rmgoStretch in aFormReport.ReportOptions;
      aList.Add(t);
    end;
  end;

  procedure _MakeOneHeader(aIndex: Integer);
  begin
    liView := RMCreateObject(rmgtMemo, '');
    liView.ParentPage := liPage;
    liView.spLeft := tmpx;
    liView.spTop := lNextTop;
    liView.spWidth := liDBGrid.Columns[aIndex].Width + 1;
    liView.spHeight := liGridTitleHeight;
    TRMMemoView(liView).Font.Assign(liDBGrid.Columns[aIndex].Title.Font);
    liView.Memo.Text := liDBGrid.Columns[aIndex].Title.Caption;
    TRMMemoView(liView).VAlign := rmvCenter;
    case liDBGrid.Columns[aIndex].Title.Alignment of
      taLeftJustify: TRMMemoView(liView).HAlign := rmhLeft;
      taRightJustify: TRMMemoView(liView).HALign := rmhRight;
      taCenter: TRMMemoView(liView).HAlign := rmhCenter;
    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
    begin
      liView.LeftFrame.Visible := True;
      liView.TopFrame.Visible := True;
      liView.RightFrame.Visible := True;
      liView.BottomFrame.Visible := True;
    end
    else
    begin
      liView.LeftFrame.Visible := False;
      liView.TopFrame.Visible := False;
      liView.RightFrame.Visible := False;
      liView.BottomFrame.Visible := False;
    end;

    aFormReport.ColumnHeaderViews.Add(liView);
    tmpx := tmpx + liView.spWidth;

    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(rmgtPicture, '');
      liView.ParentPage := liPage;
      //      TRMPictureView(liView).BandAlign := rmbaHeight;
      TRMPictureView(liView).PictureRatio := True;
    end
    else
    begin
      liView := RMCreateObject(rmgtMemo, '');
      liView.ParentPage := liPage;
      TRMMemoView(liView).VAlign := rmvCenter;
      TRMMemoView(liView).Stretched := rmgoStretch in aFormReport.ReportOptions;
      TRMMemoView(liView).WordWrap := rmgoWordWrap in aFormReport.ReportOptions;
      TRMMemoView(liView).DBFieldOnly := True;
      case liDBGrid.Columns[aIndex].Alignment of
        taLeftJustify: TRMMemoView(liView).HAlign := rmhLeft;
        taRightJustify: TRMMemoView(liView).HAlign := rmhRight;
        taCenter: TRMMemoView(liView).HAlign := rmhCenter;
      end;
      TRMMemoView(liView).Font.Assign(liDBGrid.Columns[aIndex].Font);
      if aFormReport.GridFontOptions.UseCustomFontSize then
        TRMMemoView(liView).Font.Size := aFormReport.GridFontOptions.Font.Size;
      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
    begin

⌨️ 快捷键说明

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