• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

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

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
haab
Asked:
haab
  • 5
  • 3
3 Solutions
 
Ivanov_GCommented:

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
 
shaneholmesCommented:
Here is a great article explaining all the operations your seeking:

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

Hope it helps!

Shane
0
 
shaneholmesCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
shaneholmesCommented:
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
 
haabAuthor Commented:
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
 
shaneholmesCommented:
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
 
haabAuthor Commented:
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
 
shaneholmesCommented:
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
 
haabAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now