Solved

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

Posted on 2004-04-21
9
396 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:haab
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now