// Formatierter RTF-Text aus einem TRichEdit wird in die
// Zwischenablage gestellt, bzw. in ein TRichEdit aus der
// Zwischenablage geholt.


// Getestet mit D2010 unter Win7

uses Clipbrd; 
 
var 
  CF_RTF: cardinal; 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  CF_RTF := RegisterClipboardFormat('Rich Text Format'); 
end; 
 
procedure RtfToCB(Strm: TStream); 
var 
  Hnd: THandle; 
  Ptr: Pointer; 
begin 
  Strm.position := 0; 
  Hnd := GlobalAlloc(GHND or GMEM_DDESHARE, Strm.Size); 
  if Hnd <> 0 then 
  begin 
    Ptr := GlobalLock(Hnd); 
    if Ptr <> nil then 
    begin 
      try 
        Strm.Read(Ptr^, Strm.Size); 
        Strm.position := 0; 
      finally 
        GlobalUnlock(Hnd); 
      end; 
      Clipboard.Open; 
      try 
        Clipboard.SetAsHandle(CF_RTF, Hnd); 
      finally 
        Clipboard.Close; 
      end; 
    end 
    else 
      GlobalFree(Hnd); 
  end; 
end; 
 
procedure CBtoRE(Re: TRichEdit); 
var 
  Data: HGLOBAL; 
  Ptr: PChar; 
begin 
  Clipboard.Open; 
  Data := Clipboard.GetAsHandle(CF_RTF); 
  if Data <> 0 then 
  begin 
    Ptr := GlobalLock(Data); 
    if Ptr <> nil then 
    begin 
      Re.Text := Ptr; 
    end; 
    GlobalUnlock(Data); 
  end; 
  Clipboard.Close; 
end; 
 
 
 
// RTF ins ClipBoard stellen 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  ms: TMemoryStream; 
begin 
  ms := TMemoryStream.create; 
  RichEdit1.Lines.SaveToStream(ms); 
  RtfToCB(ms); 
  ms.Free; 
end; 

 
// RTF aus ClipBoard holen 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  if Clipboard.HasFormat(CF_RTF) then 
    CBtoRE(RichEdit1); 
end;

 


 

Zugriffe seit 6.9.2001 auf Delphi-Ecke