RichEdit 复制方法

The TRichEdit Delphi control is an editor control that supports rich text formatting: text that includes variation in font attributes and paragraph formatting information - rich text formatting.

RTF files are actually ASCII files with special commands to indicate formatting information, such as fonts and margins.

Append or Insert RTF from one RichEdit to Another
The TRichEdit control does not expose a method to append or insert a piece of RTF text. It does have a Lines property to let you add more text - but you'll have trouble if you want to append rich text formatted content - it will be added as a (ASCII) simple text.
If you want to "move" the entire text from one rich editor to another you can use streams - but the content of the "destination" rich editor will be totally overwritten by the "source" editor's text.

Here's a function that allows appending or inserting RTF text from one rich editor to another - using rich edit specific callback functions:

 uses RichEdit;
 
 procedure AppendToRichEdit(const source, destination : TRichEdit) ;
 var
   rtfStream: TEditStream;
   sourceStream : TMemoryStream;
 
   function EditStreamReader(
     dwCookie: DWORD;
     pBuff: Pointer;
     cb: LongInt;
     pcb: PLongInt): DWORD; stdcall;
   begin
     result := $0000;
     try
       pcb^ := TStream(dwCookie).Read(pBuff^, cb) ;
     except
       result := $FFFF;
     end;
   end; (*EditStreamReader*)
 begin
   destination.Lines.BeginUpdate;
   sourceStream := TMemoryStream.Create;
   try
     source.Lines.SaveToStream(sourceStream) ;
     sourceStream.Position := 0;
 
     destination.MaxLength := destination.MaxLength + sourceStream.Size;
 
     rtfStream.dwCookie := DWORD(sourceStream) ;
     rtfStream.dwError := $0000;
     rtfStream.pfnCallback := @EditStreamReader;
     destination.Perform(
       EM_STREAMIN,
       SFF_SELECTION or SF_RTF or SFF_PLAINRTF, LPARAM(@rtfStream)
     ) ;
     if rtfStream.dwError <> $0000 thenb
       raise Exception.Create('Error appending RTF data.') ;
   finally
     sourceStream.Free;
     destination.Lines.EndUpdate;
   end;
 end;
 
The AppendToRichEdit procedure takes two parameters. AppendToRichEdit copies the entire content of "source" rich editor to "destination" rich editor.
Depending on the selection inside the destination rich editor and the position of the text cursor, the above procedure will either

replace any current selection - if there's a selection in "destination",
insert content from source to destination at the point of the cursor,
append content from source to destination.
Note: There's a LoadFromStream method exposed by TRichEdit butLoadFromStream does not append the loaded text to the text already in the editor. Also, to move RTF from one rich editor to another, you have to use streams - and that's what the above code does + plus some specifi rich editor callbacks.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值