Thema: Delphi Richedit Kopieren

Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#9

Re: Richedit Kopieren

  Alt 18. Mär 2004, 17:04
Die Fragestellung war ja:

Zitat:
Ich will den inhalt mehrer Richedit in eins Kopiern wie geht das (mit Formatierung)
Dann kannst du den TMemoryStream vergessen und es wird alles 100 Mal komplizierter.
Dann hilft nur noch EM_STREAMOUT, EM_STREAMIN, damit es alle RTF Formatierungen kopiert und einfügt.

Habe auch mal ein kleines Demo Projekt geschrieben.

Delphi-Quellcode:
uses
 RichEdit;

Type
  // These declarations are wrong in richedit.pas, the stdcall is missing.
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
 cb: Longint; var pcb: Longint): Longint; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do begin
    dataAvail := Size - Position;
    Result := 0; {assume everything is ok}
    if dataAvail <= cb then begin
      pcb := Read(pbBuff^, dataAvail);
      if pcb <> dataAvail then {couldn't read req. amount of bytes}
        result := E_FAIL;
    end
    else begin
      pcb := Read(pbBuff^, cb);
      if pcb <> cb then
        result := E_FAIL;
    end;
  end;
end;

function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): Longint; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then
       pcb := Write(pbBuff^, cb);
     Result := 0;
   end;
 end;

procedure GetRTFSelection( aRichEdit: TRichEdit; intoStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(intoStream);
    dwError:= 0;
    pfnCallback:= EditStreamOutCallBack;
  end;
  aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION,
   longint(@editstream));
End;

Procedure PutRTFSelection( aRichEdit: TRichEdit; sourceStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(sourceStream);
    dwError:= 0;
    pfnCallback:= EditStreamInCallBack;
  end;
  aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION,
longint(@editstream));
End;

function LastPos(SearchStr, Str: string): Integer;
var
  i: Integer;
  TempStr: string;
begin
  Result := Pos(SearchStr, Str);
  if Result = 0 then Exit;
  if (Length(Str) > 0) and (Length(SearchStr) > 0) then
  begin
    for i := Length(Str) + Length(SearchStr) - 1 downto Result do
    begin
      TempStr := Copy(Str, i, Length(Str));
      if Pos(SearchStr, TempStr) > 0 then
      begin
        Result := i;
        break;
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
  ss: TStringStream;
  re1, re2: string;
begin
  ss := TStringStream.Create('');
  try
    // 1. RichEdit
    RichEdit1.SelectAll;
    GetRTFSelection( richedit1, ss);
    re1 := ss.DataString;
    // 2. RichEdit
    ss.Position := 0;
    RichEdit2.SelectAll;
    GetRTFSelection( richedit2, ss);
    re2 := ss.DataString;
    // abschliessendes } vom Rtf-Code des 1.RE löschen
    Delete(re1,LastPos('}',re1),Length(re1));
    // {\rtf1 vom Rtf-Code des 2.RE löschen
    Delete(re2,1,6);
    // bei Rtf-Codes in den StringStream Speichern
    ss.Position := 0;
    ss.WriteString(re1 + re2);
    ss.Position := 0;
    // RichEdit1 + RichEdit2 in ein 3. Richedit streamen.
    PutRTFSelection(richedit3, ss);
  finally
    ss.Free;
  end;
end;
Angehängte Dateien
Dateityp: zip restream.zip (2,7 KB, 36x aufgerufen)
Thomas
  Mit Zitat antworten Zitat