Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to feed rich text chunks to a TRichEdit via the clipboard Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius 

It is possible to feed rich text chunks to the control but it is kind of 
convoluted. There are three options: the clipboard, the rich edits OLE interface, 
and the EM_STREAMIN message. We concentrate on the clipboard here.


The first step is to register a clipboard format for RTF, since this is not a 
predefined format:

1   var
2     CF_RTF: Word;
4   CF_RTF := RegisterClipboardFormat('Rich Text Format');

The format name has to appear as typed above, this is the name used by MS Word for 
Windows and similar MS products.

Note: The Richedit Unit declares a constant CF_RTF, which is not the clipboard 
format handle but the string you need to pass to RegisterClipboard format! So you 
can place Richedit into your uses clause and change the line above to
6   CF_RTF := RegisterClipboardFormat(Richedit.CF_RTF);

The next step is to build a RTF string with the embedded format information. You 
will get a shock if you inspect the mess of RTF stuff Wordpad (or much worse: Word) 
will put into the clipboard if you copy just a few characters ), but you can get 
away with a lot less. The bare minimum would be something like this (inserts a 12 
followed by an underlined 44444):

7   const
8     testtext: PChar = '{\rtf1\ansi\pard\plain 12{\ul 44444}}';

The correct balance of opening and closing braces is extremely important, one 
mismatch and the target app will not be able to interpret the text correctly. If 
you want to control the font used for the pasted text you need to add a fonttable 
(the default font is Tms Rmn, not the active font in the target app!). See example 
testtext2 below. If you want more info, the full RTF specs can be found on, a subset is also described in the Windows help compiler docs 
(hcw.hlp, comes with Delphi).

9   procedure TForm1.BtnSetRTFClick(Sender: TObject);
10  const
11    testtext: PChar = '{\rtf1\ansi\pard\plain 12{\ul 44444}}';
12    testtext2: PChar = '{\rtf1\ansi' +
13    '\deff4\deflang1033{\fonttbl{\f4\froman\fcharset0\fprq2 Times New Roman;}}' +
14      '\pard\plain 12{\ul 44444}}';
15    flap: Boolean = False;
16  var
17    MemHandle: THandle;
18    rtfstring: PChar;
19  begin
20    if flap then
21      rtfstring := testtext2
22    else
23      rtfstring := testtext;
24    flap := not flap;
25    MemHandle := GlobalAlloc(GHND or GMEM_SHARE, StrLen(rtfstring) + 1);
26    if MemHandle <> 0 then
27    begin
28      StrCopy(GlobalLock(MemHandle), rtfstring);
29      GlobalUnlock(MemHandle);
30      with Clipboard do
31      begin
32        Open;
33        try
34          AsText := '1244444';
35          SetAsHandle(CF_RTF, MemHandle);
36        finally
37          Close;
38        end;
39      end;
40    end
41    else
42      MessageDlg('Global Alloc failed!', mtError, [mbOK], 0);
43  end;

Once the text is in the clipboard you can call the richedits PasteFromClipboard method to insert it at the caret.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC