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