Solved

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

Posted on 2004-04-21
9
405 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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