rm_formreport.pas

来自「report machine 2.3 功能强大」· PAS 代码 · 共 1,965 行 · 第 1/5 页

PAS
1,965
字号
  Font.Assign(TRMGridFontOptions(Source).Font);
end;

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{ TRMCustomGridReport }

constructor TRMCustomGridReport.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FShowProgress := True;
  FInitialZoom := pzDefault;
  FPreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit];
  FReportOptions := [rmgoStretch, rmgoWordWrap, rmgoGridLines];
  FReport := nil;
  FReportDataSet := nil;
  FormWidth := TStringList.Create;

  FGroups := TRMGroupItems.Create(Self);
  FPageLayout := TRMPageLayout.Create;
  FPageHeader := TRMPageHeaderFooter.Create;
  FPageHeader.ParentFormReport := Self;
  FPageFooter := TRMPageHeaderFooter.Create;
  FPageFooter.ParentFormReport := Self;
  FScaleMode := TRMScaleOptions.Create;
  FGridNumOptions := TRMGridNumOptions.Create;
  FMasterDataBandOptions := TRMMasterDataBandOptions.Create;
  FGridFontOptions := TRMGridFontOptions.Create;
end;

destructor TRMCustomGridReport.Destroy;
begin
  FGroups.Free;
  FPageLayout.Free;
  FPageHeader.Free;
  FPageFooter.Free;
  FScaleMode.Free;
  FGridNumOptions.Free;
  FReport.Free;
  FReportDataSet.Free;
  FormWidth.Free;
  FMasterDataBandOptions.Free;
  FGridFontOptions.Free;

  inherited Destroy;
end;

procedure TRMCustomGridReport.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation = opRemove then
  begin
    if AComponent = FDataSet then
      DataSet := nil
    else if AComponent = FDataSource then
      DataSource := nil;
  end;
end;

procedure TRMCustomGridReport.AssignFont(aView: TRMMemoView; aFont: TFont);
var
  liSaveColor: TColor;
begin
  liSaveColor := aView.Font.Color;
  aView.Font.Assign(aFont);
  if rmgoUseColor in ReportOptions then
    aView.Font.Color := liSaveColor;
end;

procedure TRMCustomGridReport.SetMemoViewFormat(aView: TRMMemoView; aField: TField);

  procedure SetFormat(aType, aIndex: Integer; const aFormat: string);
  begin
    if aFormat = '' then Exit;
    aView.Format := aType * $01000000 + aIndex * $00010000;
    aView.Format := aView.Format + Ord('.');
    aView.FormatStr := aFormat;
  end;

begin
  if aField.DataType in [ftSmallint, ftInteger, ftWord, ftFloat, ftCurrency,
    ftBCD, ftBytes, ftVarBytes, ftAutoInc{$IFDEF Delphi4}, ftLargeint{$ENDIF}] then
    SetFormat(1, RMFormatNumCount, TNumericField(aField).DisplayFormat)
  else if aField.DataType in [ftDate, ftDateTime] then
    SetFormat(2, RMFormatDateCount, TDateTimeField(aField).DisplayFormat)
  else if aField.DataType in [ftTime] then
    SetFormat(3, RMFormatTimeCount, TDateTimeField(aField).DisplayFormat)
  else if aField.DataType in [ftBoolean] then
    SetFormat(4, RMFormatBooleanCount, TBooleanField(aField).DisplayValues);
end;

function TRMCustomGridReport.CalcWidth(aWidth: Integer): Integer;
begin
  Result := aWidth;
  if FScaleMode.ScaleMode = rmsmAdjust then
  begin
    if FScaleMode.ScaleFactor <> 100 then
      Result := Round(aWidth * FScaleMode.ScaleFactor / 100);
  end
end;

procedure TRMCustomGridReport.CalcRect(t: TRMView; ParentBand: TRMBandView; aFormWidth: Integer);
var
  liScale: Double;

  procedure ScaleView;
  begin
    t.dx := Round((t.x + t.dx) * liScale) - Round(t.x * liScale);
    t.dy := Round((t.y + t.dy) * liScale) - Round(t.y * liScale);
    t.x := Round(t.x * liScale);
    t.y := ParentBand.y + Round(t.y * liScale) - Round(ParentBand.y * liScale);
    if t is TRMMemoView then
      TRMMemoView(t).Font.Height := -Trunc(TRMMemoView(t).Font.Size * 96 / 72 * liScale);
  end;

begin
  t.x := t.x + OffsX;
  t.y := t.y + OffsY;
  liScale := FScaleMode.ScaleFactor / 100;
  if FScaleMode.ScaleMode = rmsmAdjust then
  begin
    if FScaleMode.ScaleFactor <> 100 then
      ScaleView;
  end
  else
  begin
    if FScaleMode.FitPageWidth then //水平缩放
    begin
      liScale := PageWidth / aFormWidth;
      ScaleView;
    end
    else if (not HaveDetailBand) and FScaleMode.FitPageHeight then
    begin
      liScale := PageHeight / FormHeight;
      ScaleView;
    end;
  end;

  if FScaleMode.CenterOnPageH then //水平居中
  begin
    t.x := t.x + (PageWidth - Round(aFormWidth * liScale)) div 2;
  end;
  if FScaleMode.CenterOnPageV and (not HaveDetailBand) then //垂直居中
  begin
    t.y := t.y + (PageHeight - Round(FormHeight * liScale)) div 2;
  end;
end;

type
  THackReport = class(TRMReport)
  end;

procedure TRMCustomGridReport.AddPage;
var
  liPage: TRMPage;
begin
  FReport.Pages.Add;
  liPage := FReport.Pages[FReport.Pages.Count - 1];
  with PageLayout do
  begin
    liPage.pgSize := pgSize;
    liPage.UseMargins := True;
    liPage.pgMargins := Rect(LeftMargin, TopMargin, RightMargin, BottomMargin);
    liPage.pgOr := pgOr;
    liPage.ChangePaper(pgSize, Width * 10, Height * 10, pgBin, pgOr);
  end;
end;

procedure TRMCustomGridReport.ShowReport(aFlag: Integer);
begin
  if FReport = nil then
    FReport := TRMReport.Create(Self);
  ReportDataSet.DataSet := nil;
  ReportDataSet.DataSource := nil;
  FReport.ModalPreview := TRUE;
  RM_Class.CurReport := FReport;
  RMVersion := RMCurrentVersion;

  try
    FReport.InitialZoom := FInitialZoom;
    FReport.PreviewButtons := FPreviewButtons;
    FReport.Preview := FPreview;
    FReport.ReportType := FReportType;
    FReport.ShowProgress := FShowProgress;
    FReport.Title := FReportTitle;
    FReport.OnBeginBand := FOnBeginBand;
    FReport.OnEndBand := FOnEndBand;
    FReport.OnBeginDoc := FOnBeginDoc;
    FReport.OnEndDoc := FOnEndDoc;
    FReport.OnBeginPage := FOnBeginPage;
    FReport.OnEndPage := FOnEndPage;
    FReport.OnGetValue := FOnGetValue;
//    FReport.OnBeforePrint := FOnBeforePrint;
    FReport.OnProgress := FOnProgress;
    FReport.OnBeginColumn := FOnBeginColumn;
    FReport.OnPrintColumn := FOnPrintColumn;
    if rmgoRebuildAfterPageChanged in ReportOptions then
      THackReport(FReport).OnAfterPreviewPageSetup := OnAfterPreviewPageSetup
    else
      THackReport(FReport).OnAfterPreviewPageSetup := nil;

    FReport.Pages.Clear;
    with PageLayout do
    begin
      FReport.DoublePass := DoublePass;
      FReport.PrintToDefault := PrintToDefault;
      THackReport(FReport).SetPrinterTo(PrinterName);
    end;
    AddPage;

    if CreateReportFromGrid then
    begin
      try
        if (ReportDataSet.DataSource <> nil) and (ReportDataSet.DataSource.DataSet <> nil) then
          ReportDataSet.DataSource.DataSet.DisableControls;
        case aFlag of
          1: FReport.ShowReport;
          2: FReport.PrintReport;
          3: FReport.DesignReport;
        end;
      finally
        if (ReportDataSet.DataSource <> nil) and (ReportDataSet.DataSource.DataSet <> nil) then
          ReportDataSet.DataSource.DataSet.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TRMCustomGridReport.PreviewReport;
begin
  ShowReport(1);
end;

procedure TRMCustomGridReport.PrintReport;
begin
  ShowReport(2);
end;

procedure TRMCustomGridReport.DesignReport;
begin
  ShowReport(3);
end;

procedure TRMCustomGridReport.BuildReport;
begin
  ShowReport(0);
end;

procedure TRMCustomGridReport.OnAfterPreviewPageSetup(Sender: TObject);
begin
  ChangePageLayout(TRMPageSetting(Sender));
  BuildReport;
end;

procedure TRMCustomGridReport.ChangePageLayout(aPageSetting: TRMPageSetting);
begin
  PageLayout.PrinterName := aPageSetting.PrinterName;
  PageLayout.PrintToDefault := aPageSetting.PrintToDefault;
  PageLayout.DoublePass := aPageSetting.DoublePass;
  PageLayout.pgOr := aPageSetting.PageOr;
  PageLayout.Columns := aPageSetting.ColCount;
  PageLayout.ColumnSpace := Round(RMConvertToPixels(aPageSetting.ColGap * 10, rmsuMM));

  PageLayout.LeftMargin := Round(RMConvertToPixels(aPageSetting.MarginLeft * 10, rmsuMM));
  PageLayout.TopMargin := Round(RMConvertToPixels(aPageSetting.MarginTop * 10, rmsuMM));
  PageLayout.RightMargin := Round(RMConvertToPixels(aPageSetting.MarginRight * 10, rmsuMM));
  PageLayout.BottomMargin := Round(RMConvertToPixels(aPageSetting.MarginBottom * 10, rmsuMM));

  PageLayout.Width := aPageSetting.PageWidth;
  PageLayout.Height := aPageSetting.PageHeight;
  PageLayout.pgBin := aPageSetting.PageBin;
  PageLayout.pgSize := aPageSetting.PageSize;
end;

function TRMCustomGridReport.CreateReportFromGrid: Boolean;
begin
  Result := FALSE;
end;

procedure TRMCustomGridReport.SetGroups(Value: TRMGroupItems);
begin
  FGroups.Assign(Value);
end;

function TRMCustomGridReport.GetReport: TRMReport;
begin
  if FReport = nil then
    FReport := TRMReport.Create(Self);
  Result := FReport;
end;

function TRMCustomGridReport.GetReportDataSet: TRMDBDataSet;
var
  i: Integer;
  str: string;
begin
  if FReportDataSet = nil then
  begin
    FReportDataSet := TRMDBDataSet.Create(RMDialogForm);
    i := 1;
    while True do
    begin
      str := 'RMGridDataSet' + IntToStr(i);
      if RMDialogForm.FindComponent(str) = nil then
      begin
        FReportDataSet.Name := str;
        Break;
      end;
      Inc(i);
    end;
  end;
  Result := FReportDataSet;
end;

procedure TRMCustomGridReport.SetPageHeader(Value: TRMPageHeaderFooter);
begin
  FPageHeader.Assign(Value);
end;

procedure TRMCustomGridReport.SetPageFooter(Value: TRMPageHeaderFooter);
begin
  FPageFooter.Assign(Value);
end;

procedure TRMCustomGridReport.SetMasterDataBandOptions(Value: TRMMasterDataBandOptions);
begin
  FMasterDataBandOptions.Assign(Value);
end;

procedure TRMCustomGridReport.SetGridNumOptions(Value: TRMGridNumOptions);
begin
  FGridNumOptions.Assign(Value);
end;

procedure TRMCustomGridReport.SetScaleMode(Value: TRMScaleOptions);
begin
  FScaleMode.Assign(Value);
end;

procedure TRMCustomGridReport.SetPageLayout(Value: TRMPageLayout);
begin
  FPageLayout.Assign(Value);
end;

{procedure TRMCustomGridReport.SetGridFontOptions(Value: TRMGridFontOptions);
begin
  FGridFontOptions.Assign(Value);
end;}

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMFormReportObject}

constructor TRMFormReportObject.CreateObject;
begin
  inherited Create;
  AutoFree := True;
end;

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMAddinFormReportObjectInfo}

constructor TRMAddInFormReportObjectInfo.Create(AClassRef: TClass; AObjectClass: TClass);
begin
  inherited Create;
  FClassRef := AClassRef;
  FObjectClass := AObjectClass;
end;

{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{TRMFormReport}

constructor TRMFormReport.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FReportObjects := TList.Create;
  FPrintControl := nil;
  FDataSet := nil;
  FDataSource := nil;
  CanSetDataSet := True;
  FGridFixedCols := 0;
end;

destructor TRMFormReport.Destroy;
begin
  Clear;
  FReportObjects.Free;
  inherited Destroy;
end;

⌨️ 快捷键说明

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