To: All
From: "Peter Below (TeamB)"
Newsgroup: borland.public.delphi.vcl.components.using
 
29-Sep-2000

Re:

In article <39d19247$1_1@dnews>, Borland wrote:
> From: "Borland" <✉t-online.de>

Please change your from name, you are not a Borland employee.

> I need to display formatted Text. So I decided to use TRichEdit. But: It
> doesn't work the way I expect it to do. I want to use it for something like
> this:
> 
>   RichEdit1.Text := 'This is \b bold\b0 .';

No, it's not that simple (remember: this is a Microsoft control we are 
talking about <G>). 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. Lets start with the 
clipboard since i happen to have a related post on file.

OK, the first step is to register a clipboard format for RTF, since this is not a predefined format:
  Var     CF_RTF : Word;
  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
  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):
 Const    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 www.microsoft.com, a subset is also described in the Windows help compiler docs (hcw.hlp, comes with Delphi).
procedure TForm1.BtnSetRTFClick(Sender: TObject); Const   testtext: PChar = '{\rtf1\ansi\pard\plain 12{\ul 44444}}';   testtext2: PChar = '{\rtf1\ansi'+   '\deff4\deflang1033{\fonttbl{\f4\froman\fcharset0\fprq2 Times New Roman;}}'                      +'\pard\plain 12{\ul 44444}}';   flap: Boolean = False; Var   MemHandle: THandle;   rtfstring: PChar; begin     If flap Then       rtfstring := testtext2     Else       rtfstring := testtext;     flap := not flap;     MemHandle := GlobalAlloc( GHND or GMEM_SHARE, StrLen(rtfstring)+1 );     If MemHandle <> 0 Then Begin       StrCopy( GlobalLock( MemHandle ), rtfstring );       GlobalUnlock( MemHandle );       With Clipboard Do Begin         Open;         try           AsText := '1244444';           SetAsHandle( CF_RTF, MemHandle );         finally           Close;         end;       End;     End     Else       MessageDlg('Global Alloc failed!',                  mtError, [mbOK], 0 ); end;
Once the text is in the clipboard you can call the richedits PasteFromClipboard method to insert it at the caret.
For using the OLE interface (which keeps the clipboard untouched) i have to direct you to Brian Longs Delphi Clinic column in the Delphi Magazine, he published some code in issue 58. You may find it on the companion disk for this issue, see www.itecuk.com.
For EM_STREAMIN go to http://www.deja.com, http://www.mers.com/searchsite.html or http://www.tamaracka.com/search.htm and search the newsgroups for GetRTFSelection and PutRTFSelection. That will give you methods to insert rich text from a stream (e.g. a TStringstream) into a rich edit control.
Peter Below (TeamB) ✉compuserve.com) No e-mail responses, please, unless explicitly requested!
 
Originally created by
Tamarack Associates
Thu, 28 Mar 2024 12:18:04 UTC
Copyright © 2009-2024
HREF Tools Corp.