Getting RTF from clipboard

878 views
Skip to first unread message

Chad Jones

unread,
Dec 7, 1999, 3:00:00 AM12/7/99
to
How do you detect/access RTF content on the clipboard? I'm trying to
check the clipboard for RTF content at paste time. I don't see any
definition for CF_RTF format.

Thanks,
Chad Jones

Peter Below (TeamB)

unread,
Dec 7, 1999, 3:00:00 AM12/7/99
to

That's because it is not one of the stock clipboard formats. The
Richedit unit defines a number of badly named string constants which
can be handed to RegisterClipboardFormat to obtain a clipboard format
ID:

const
CF_RTF = 'Rich Text Format';
CF_RTFNOOBJS = 'Rich Text Format Without Objects';
CF_RETEXTOBJ = 'RichEdit Text and Objects';


Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


Gerold Gruber

unread,
Dec 7, 1999, 3:00:00 AM12/7/99
to
> How do you detect/access RTF content on the clipboard? I'm trying to
> check the clipboard for RTF content at paste time. I don't see any
> definition for CF_RTF format.

RTF is not a predefined or build in clipboard format. So the formatId to
access rtf content on the
clipboard can be different on different computers.
You can get this formatId by trying to register the rtf-format again.
Just call:

var formatID:integer;
..
formatID:=RegisterClipboardFormat(PChar('Rich Text Format'));

To detect if there is currently rtf content on the clipboard you can use
the
Clipboard variable of Delphi declared in unit clipbrd.
e.g.

if Clipboard.HasFormat(formatID) then .... dosomething


hope this helps


Peter Below (TeamB)

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to
> How to implement this? I found dejavu references to writing to the
> clipboard -- it looks terribly complex. But no references to reading
> custom data (such as RTF) from the clipboard. Nor is it covered in help.
> Is this because it is even more complex than writing?

Depends on how you define "complex" <G>. It certainly requires more than 5
lines of code.

> Function getRTFfromClipboard: string;
> Begin
> // just feel free to fill in this part <grin>
> End;

What do you intend to do with the string once you've got it? It will
contain all the formatting codes, you know. To get it into a richedit
control one would simply call the controls Pastefromclipboard function. Do
get the plain text version (which a richedit will always put into the
clipboard in parallel to the rich text version) you simply assign
clipboard.astext to a string.

Anyway, you asked for it, you get it.

In the following sample project Action1 is linked to Button1.Action.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ActnList, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
ActionList1: TActionList;
Action1: TAction;
procedure Action1Execute(Sender: TObject);
procedure Action1Update(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses richedit, Clipbrd;

{$R *.DFM}
Var
CF_RTF: Word = 0;

Procedure CopyStreamToClipboard( fmt: Cardinal; S: TStream );
Var
hMem: THandle;
pMem: Pointer;
Begin
S.Position := 0;
hMem := GlobalAlloc( GHND or GMEM_DDESHARE, S.Size );
If hMem <> 0 Then Begin
pMem := GlobalLock( hMem );
If pMem <> Nil Then Begin
try
S.Read( pMem^, S.Size );
S.Position := 0;
finally
GlobalUnlock( hMem );
end;
Clipboard.Open;
try
Clipboard.SetAsHandle( fmt, hMem );
finally
Clipboard.Close;
end;
End { If }
Else Begin
GlobalFree( hMem );
OutOfMemoryError;
End;
End { If }
Else
OutOfMemoryError;
End; { CopyStreamToClipboard }

Procedure CopyStreamFromClipboard( fmt: Cardinal; S: TStream );
Var
hMem: THandle;
pMem: Pointer;
Begin
hMem := Clipboard.GetAsHandle( fmt );
If hMem <> 0 Then Begin
pMem := GlobalLock( hMem );
If pMem <> Nil Then Begin
try
S.Write( pMem^, GlobalSize( hMem ));
S.Position := 0;
finally
GlobalUnlock( hMem );
end;
End { If }
Else
raise Exception.Create(
'CopyStreamFromClipboard: could not lock global handle '+
'obtained from clipboard!');
End; { If }
End; { CopyStreamFromClipboard }

Function GetRTFFromClipboard: String;
Var
ss: TStringstream;
Begin
If CF_RTF > 0 Then Begin
ss:= TStringstream.Create( EmptyStr );
try
CopyStreamFromClipboard( CF_RTF, ss );
Result := ss.DataString;
finally
ss.free;
end;
End
Else
Result := EmptyStr;
End;

procedure TForm1.Action1Execute(Sender: TObject);
begin
memo1.Text := GetRTFFromClipboard;
end;

procedure TForm1.Action1Update(Sender: TObject);
begin
(Sender As TAction).enabled := Clipboard.Hasformat( CF_RTF );
end;

Initialization
CF_RTF := RegisterClipboardFormat( richedit.CF_RTF );
end.

Chad Jones

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to
Thanks Peter, that was really helpful!!

- Chad

Peter Below (TeamB) wrote:
>
> > How to implement this? I found dejavu references to writing to the
> > clipboard -- it looks terribly complex. But no references to reading

> > custom data (such as RTF) from the clipboard. Nor is it covered in ...

super...@gmail.com

unread,
Oct 8, 2016, 4:46:43 PM10/8/16
to
Thanks...solved my problem.
Reply all
Reply to author
Forward
0 new messages