Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to copy rich text from a TRichEdit to 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
30-Aug-02
Category
Reporting /Printing
Language
Delphi 2.x
Views
60
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have used the TRichEdit component to generate some rich text which I am now 
holding in a byte array. How can I paste it to the clipboard so that it can be 
copied into MS Word?

Answer:

You have to copy it to the clipboard with a specific format. The richedit unit 
defines a string constant CF_RTF (very unfortunate name!). You feed that to 
RegisterClipboardFormat to obtain a format identifier which you can then use with 
Clipboard.SetAshandle.

If you write the data to a memorystream you can use the following procedure to copy 
the streams content to the clipboard. Use the format identifier you obtained from 
CF_RTF as first parameter.
1   
2   procedure CopyStreamToClipboard(fmt: Cardinal; S: TStream);
3   var
4     hMem: THandle;
5     pMem: Pointer;
6   begin
7     {Rewind stream position to start}
8     S.Position := 0;
9     {Allocate a global memory block the size of the stream data}
10    hMem := GlobalAlloc(GHND or GMEM_DDESHARE, S.Size);
11    if hMem <> 0 then
12    begin
13      {Succeeded, lock the memory handle to get a pointer to the memory}
14      pMem := GlobalLock(hMem);
15      if pMem <> nil then
16      begin
17        {Succeeded, now read the stream contents into the memory the pointer points 
18  at}
19        try
20          S.read(pMem^, S.Size);
21          {Rewind stream again, caller may be confused if the stream position is 
22  				left at the end}
23          S.Position := 0;
24        finally
25          {Unlock the memory block}
26          GlobalUnlock(hMem);
27        end;
28        {Open clipboard and put the block into it. The way the Delphi clipboard 
29  			object is written this will clear the clipboard first. Make sure the 
30  			clipboard is closed even in case of an exception. If left open it would 
31  			become unusable for other apps.}
32        Clipboard.Open;
33        try
34          Clipboard.SetAsHandle(fmt, hMem);
35        finally
36          Clipboard.Close;
37        end;
38      end
39      else
40      begin
41        {Could not lock the memory block, so free it again and raise an out of 
42  			memory exception}
43        GlobalFree(hMem);
44        OutOfMemoryError;
45      end;
46    end
47    else
48      {Failed to allocate the memory block, raise exception}
49      OutOfMemoryError;
50  end;


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

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC