Solved

How to save clipboard content in any format and deliver it back later?

Posted on 2004-04-21
9
417 Views
Last Modified: 2010-04-05
How do I do the following:

A Delphi form has two buttons. The first button will take a copy of the content of the clipboard, regardless what format it has. The second button will copy this back to the clipboard, after I have used the clipboard for other purposes. It is important that the format is maintained, so that i.e. formatted text from MS Word can be put back into it after I have used the clipboard in the meantime.
0
Comment
Question by:haab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10879105

uses
   Clipbrd;

procedure TForm1.Button1Click(Sender: TObject);
begin
if Clipboard.HasFormat(CF_TEXT) then
  Edit1.Text := Clipboard.AsText
else
  MessageDlg('There is no text on the Clipboard', mtInformation,
        [mbOK],0);

end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10879128
Here is a great article explaining all the operations your seeking:

http://delphi.about.com/library/weekly/aa092199.htm

Hope it helps!

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10879179
If thats not enough information, then check ou their other info links:


http://delphi.about.com/sitesearch.htm?terms=clipboard&SUName=delphi&TopNode=3176&type=1

Listening to the Clipboard - part 1/3
Extending the clipboard's flexibility and functionality from Delphi. Taking control over the Clipboard with custom formats. Coding Delphi to receive clipboard change notifications.

http://delphi.about.com/library/weekly/aa110700a.htm


Listening to the Clipboard - part 2/3
Extending the clipboard's flexibility and functionality from Delphi. Taking control over the Clipboard with custom formats. Coding Delphi to receive clipboard change notifications.

http://delphi.about.com/library/weekly/aa110700b.htm


Listening to the Clipboard - part 3/3
Extending the clipboard's flexibility and functionality from Delphi. Taking control over the Clipboard with custom formats. Coding Delphi to receive clipboard change notifications.

http://delphi.about.com/library/weekly/aa110700c.htm

Shane
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:shaneholmes
shaneholmes earned 500 total points
ID: 10880006
IN a nutshell, you do the following:

uses  Clipbrd;

for saving Bitmaps to Clipboard

Clipboard.Assign(MyBitmap);

retrieving bitmap

if Clipboard.HasFormat(CF_BITMAP) then
  Image1.Picture.Bitmap.Assign(Clipboard);


 
for saving Text to Clipboard

Clipboard.AsText  :=  SomeText;

retrieving text

if Clipboard.HasFormat(CF_TEXT) then
  SomeText := Clipboard.AsText;


to learn how to do other image formats & persistent objects, use the links above

CF_OBJECT Any persistent object.


Shane
0
 

Author Comment

by:haab
ID: 10886802
Thank you for the tips, Shane and Ivanov G! I have looked into your suggestions, but none of them solves the issue.

If necessary, I would consider saving the clipboard content in a file. This is possible in the program clipbrd.exe. It provides menu items that I would like to use: File/Save As ... and File/Open. With the first one the content of the clipboard is stored in a file, with the latter the content of the file is re-established on the clipboard. This is what I want; all formats persist (i do not have to think about them), and they are available to other programs exactly the same way as before I used the clipboard for my special purposes.

The question is: Is this possible without running an instance of this EXE file? Is there something I can use directly from Delphi, such as ClipBoard.SaveToFile('AFileName.tmp') and ClipBoard.GetFromFile('AFileName.tmp')?

This question seems to be more difficult than I first assumed, so I have raised the amount of points for the solving of it.
0
 
LVL 11

Assisted Solution

by:shaneholmes
shaneholmes earned 500 total points
ID: 10888018
Off the top of my head .... using some of the info already provided to you......

procedure ClpSaveToFile(AFileName: String);
var
 MemStr: TMemoryStream;
 bmp: TBitmap;
begin
 MemStr:= TMemoryStream.Create;
 if Clipboard.HasFormat(CF_TEXT) then
 begin
  WriteStreamStr(MemStr, 'CF_TEXT');
  WriteStreamStr(MemStr, Clipboard.AsText);
 end;
 if Clipboard.HasFormat(CF_BITMAP) then
 begin
 WriteStreamStr(MemStr, 'CF_BITMAP');
  bmp:= TBitmap.Create;
  bmp.Assign(Clipboard);
  Bmp.SaveToSTream(MemStr);
 end;
 MemStr.SaveToFile(FileName);
 MemStr.Free;
end;

procedure ClpLoadFromFile(AFileName: String);
var
 MemStr: TMemoryStream;
 Hdr: String;
 Bmp: TBitmap;
begin
 MemStr:= TMemoryStream.Create;
 MemStr.LoadFromFile(FileName);
 Hdr:= ReadStreamStr(MemStr);
 if Hdr = 'CF_TEXT' then
  Clipboard.AsText := ReadStreamStr(MemStr)
 else
 if Hdr = 'CF_BITMAP' then
 begin
  Bmp:= TBitmap.Create;
  Bmp.LoadFromStream(MemStr);
  Clipboard.Assign(Bmp);
 end;
 MemStr.Free;
end;


Shane
0
 

Author Comment

by:haab
ID: 10938643
Thank you for all your effort!

I have raised the amount of points once more, because my inquiries here and elsewhere has not given the solution that I need - it seems to be a difficult task.

To be more precise: I want to store (as a varaible in my Delphi program OR as a file on the system) the content og the clipboard, regardless of the current content and format of the clipboard. That is, I do not know and do not want to know what kind of data we are talking about - I just want to keep it temporarily or save it, so that I can use the clipboard for my own purposes and then deliver the original data back to it. If the clipboard contains stuff copied from i.e. MS Word, I want to deliver it back later - without any loss of format(s).

I know that the clipboard software (clipbrd.exe) can do it, but it seems like Delphi can deal with texts, bitmaps and a (limited) set of formats - why can't I just get what is there and store it this way or another - without bothering what it really is? To get or not to get, that's the question...

Does anybody know about a software that could be included in my program discretely?

... or a way to write the lines of code that it takes?

Again, thank you for your works and tips, I am sorry that I can't give away my points yet.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 500 total points
ID: 10938944
We have provided you all the answers.... There is no way to store the data in one variable if you dont know what type of data it is..... with even a variant, you will have to test it before using it, or loading ... and vice versus for saving or posting...


You have to test for the type both ways. And for each type, you must add the appropriate code to do so.

Included in the suggestions were methods for loading and saving to file.....


"it seems like Delphi can deal with texts, bitmaps and a (limited) set of formats - why can't I just get what is there and store it this way or another - without bothering what it really is? To get or not to get, that's the question..."

For those formats that are not there, you need to write your own methods.....

Which will be quite a task....

For example: lets say you want to transfer RTF text 7 attributes (like word does), you will have to write your own

(you could look at the TRichEdit, which has a limited set of functionality - SaveToClipBoard, CopFromClipboard methods


ANyway, theres some time & work involved

Shane





0
 

Author Comment

by:haab
ID: 10939056
Dear Shane!

Thank you! I have given you some of the points for all the effort you have made, and for clarifying that the task really is harder than I hoped.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 69
Reconfigure Delphi Install? 2 65
tidtcpserver connection lost handle 2 109
update joined tables 2 62
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question