Solved

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

Posted on 2004-04-21
9
399 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now