Einzelnen Beitrag anzeigen

mytbo

Registriert seit: 8. Jan 2007
477 Beiträge
 
#13

AW: FastReport: RFT Länge ermitteln

  Alt 13. Feb 2023, 01:19
Ich habe jetzt eine CustomerFunction eingebaut, die mir im Delphi Quelltext das Datenfeld in das Dummy RichEdit einliest und mir den Text ohne Steuerzeichen emittelt und einen Boolean zurückgibt.
Wenn immer möglich, sollte eine Bericht-Funktion ohne interne Abhängigkeiten geschrieben werden. Das bietet die Möglichkeit, mit der Zeit eine eigene Sammlung nützlicher Helfer zu erstellen, die nur durch Einbindung einer Unit zur Verfügung gestellt werden. Im konkreten Fall könnte es zum Beispiel so aussehen:
Delphi-Quellcode:
unit u_ReportFunctions;

interface

uses
  Windows, Messages, SysUtils, Classes, Variants,
  fs_iinterpreter;

type
  TReportFunctions = class(TfsRTTIModule)
  private
    function CallMethod(pmInstance: TObject; pmClassType: TClass;
      const pmcMethodName: String; pmCaller: TfsMethodHelper): Variant;
  public
    constructor Create(pmScript: TfsScript); override;
  end;

implementation

uses
  frxClass, frxVariables, frxRichEdit;

const
  OWN_FUNCTIONS = 'Own functions';

function RTFTextCharCount(const pmcRTFText: String): Integer;
var
  richEdit: TRxRichEdit;
  loadStream: TStringStream;
begin
  Result := 0;
  if pmcRTFText = 'then Exit; //=>

  richEdit := TRxRichEdit.CreateParented(HWND(HWND_MESSAGE));
  try
    loadStream := TStringStream.Create(pmcRTFText);
    try
      richEdit.Lines.LoadFromStream(loadStream);
    finally
      loadStream.Free;
    end;

    Result := richEdit.GetTextLen;
    if Result > 0 then
      Result := Result - (richEdit.Perform(EM_GETLINECOUNT, 0, 0) - 1);
  finally
    richEdit.Free;
  end;
end;

function IsRTFTextEmpty(const pmcRTFText: String): Boolean;
begin
  if pmcRTFText <> 'then
    Result := (RTFTextCharCount(pmcRTFText) = 0)
  else
    Result := True;
end;

//=== TReportFunctions =========================================================

constructor TReportFunctions.Create(pmScript: TfsScript);
begin
  inherited Create(pmScript);
  pmScript.AddMethod('function IsRTFTextEmpty(const pmcRTFText: String): Boolean',
    CallMethod, OWN_FUNCTIONS, 'IsRTFTextEmpty() returns if chars are present');
  pmScript.AddMethod('function RTFTextCharCount(const pmcRTFText: String): Integer;',
    CallMethod, OWN_FUNCTIONS, 'RTFTextCharCount() returns the number of chars');
end;

function TReportFunctions.CallMethod(pmInstance: TObject; pmClassType: TClass;
  const pmcMethodName: String; pmCaller: TfsMethodHelper): Variant;
begin
  if SameText(pmcMethodName, 'IsRTFTextEmpty') then
    Result := IsRTFTextEmpty(pmCaller.Params[0])
  else if SameText(pmcMethodName, 'RTFTextCharCount') then
    Result := RTFTextCharCount(pmCaller.Params[0]);
end;

initialization
  fsRTTIModules.Add(TReportFunctions);

end.
Und so im Bericht eingesetzt werden:
Delphi-Quellcode:
procedure DetailDataOnBeforePrint(Sender: TfrxComponent);
begin
  Child.Visible := not IsRTFTextEmpty(<data."RTFData">);
end;
Bis bald...
Thomas
  Mit Zitat antworten Zitat