I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Single User Apps Topic   Print This     

Copy to windows clipboard

Copy to windows clipboard in Delphi Single User Apps topic (part of our Pascal and Delphi Coding group).

Quick Search: windows   clipboard   Copy windows   Copy windows clipboard  
Xion
-- USA
Im back with more trouble Big Grin!

Now I need to get a string copied to the windows clipboard, so I can paste(ctlr-V) the string in other application.

Again, I find my skills inadequate Big Grin!
 Posted 17 years ago (Thread Starter)
Comment Quote
About Xion -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread but email is NOT VERIFIED.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #4892, 2 replies
Thread Started 1/17/2002 11:11:00 AM
View Counter=3437
Location=-- USA 
Joined=17 years ago   MB Posts=8  
Larry J. Rutledge
Prestwood IT
Citrus Heights, CA USA
I have an excellent beginner's reference to the Clipboard in an article, but I don't remember the source so I am including the text of the article here.




Clipboard in General
As you probably know, the Clipboard can hold only one piece of data for cut, copy and paste at one time. In general, it can hold only only one piece of the same kind of data at a time. If we send new information of the same format to the Clipboard, we wipe out what was there before. The contents of the Clipboard stays with the Clipboard even after we paste those contents into another program.

TClipboard
In order to use the Windows Clipboard in our applications, we must add the ClipBrd unit to the uses clause of the project, except when we restrict cutting, copying and pasting to the components that have built in support for Clipboard methods. Those components are TEdit, TMemo, TOLEContainer, TDDEServerItem, TDBEdit, TDBImage, and TDBMemo.

The ClipBrd unit automatically instantiates a TClipboard object called Clipboard. We'll use the CutToClipboard, CopyToClipboard, PasteFromClipboard, Clear, and HasFormat() methods to deal with Clipboard operations and text/graphic manipulation.

Send and Retrieve Text
In order to send some text to the Clipboard the AsText property of the Clipboard object is used. If we want, for example, to send the string information contained in the variable SomeStringData to the Clipboard (wiping out whatever text was there), we'll use the following code:


uses Clipbrd;
...
Clipboard.AsText := SomeStringData


The string value of the AsText property is limited to 255 characters. To set and retrieve more than 255 characters, use the
Old UBB Archived Link: [URL=http://www.developers.demon.co.uk/copyclip.html]SetTextBuf and GetTextBuf[/URL]
Clipboard methods.

Naturally, to retrieve the text information from the Clipboard we'll use


uses Clipbrd;
...
SomeStringData := Clipboard.AsText


Note: The Clipboard must contain a string or an exception occurs. To determine if the Clipboard contains a string type we'll have to use the HasFormat method.


uses Clipbrd;
...
if Clipboard.HasFormat(CF_TEXT) then
SomeStringData := Clipboard.AsText
else
ShowMessage('No text in the Clipboard');


Note: If we only want to copy the text from, let's say, Edit component to the Clipboard, we do not have to include the Clipbrd unit to the uses clause. The CopyToClipboard method of TEdit copies the selected text in the edit control to the Clipboard in CF_TEXT format.


procedure TForm1.Button2Click(Sender: TObject);
begin
// the following line will select
// All the text in the edit control
{Edit1.SelectAll;}
Edit1.CopyToClipboard;
end;


Clipboard Images
To retrieve graphical images from the Clipboard, Delphi must know what type of image is stored there. Similarly, to transfer images to the clipboard, the application must tell the Clipboard what type of graphics it is sending. Some of the possible values of the Format parameter follow, there are many more Clipboard formats provided by Windows.



  • CF_TEXT - Text with each line ending with a CR-LF combination.

  • CF_BITMAP - A Windows bitmap graphic.

  • CF_METAFILEPICT - A Windows metafile graphic.

  • CF_PICTURE - An object of type TPicture.

  • CF_OBJECT - Any persistent object.




The HasFormat method returns True if the image in the Clipboard has the right format.


uses Clipbrd;
...
if Clipboard.HasFormat(CF_METAFILEPICT) then
ShowMessage('Clipboard has metafile');


To send (assign) an image to the Clipboard, we use the Assign method. For example, the following code copies the bitmap from a bitmap object named MyBitmap to the Clipboard.


Clipboard.Assign(MyBitmap);


In general, MyBitmap is an object of type TGraphic, TBitmap, TMetafile, or TPicture.

To retrieve an image from the Clipboard we have to verify the format of the current contents of the clipboard and use the Assign method of the target object.


{ Place one button and one image control on form 1 }
{ Prior to executing this code press Alt-PrintScreen key combination }
uses Clipbrd;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Clipboard.HasFormat(CF_BITMAP) then
Image1.Picture.Bitmap.Assign(Clipboard);
end;


More Control
Clipboard stores information in multiple formats so we can transfer data between applications that use different formats. When reading information from the clipboard with Delphi's TClipboard class, we are limited to standard clipboard formats: text, pictures and metafiles.

Suppose we have two different Delphi applications running, what do you say about defining custom clipboard format in order to send and receive data between those two programs?

Suppose we are trying to code a Paste menu item - we want it to be disabled when there is no, lets say, text in the clipboard. Since the entire process with the clipboard takes place behind the scenes, there is no method TClipboard class that will inform us that there has been some change in the contents of the clipboard. What we need is to hook in the clipboard notification system, so we can get and respond to events when the clipboard changes.

If we want more flexibility and functionality we have to deal with clipboard change notifications and custom clipboard formats:
Old UBB Archived Link: [URL=http://delphi.about.com/library/weekly/aa110700a.htm]Listening to the Clipboard[/URL]
.

Related Articles/Components



  • Old UBB Archived Link: [URL=http://www.developers.demon.co.uk/copyclip.html]Copying large amounts of Text to the Clipboard.[/URL]
    How to store text that exceeds 255 characters and the data is coming from a source that does not have implicit clipboard support.


  • Old UBB Archived Link: [URL=http://www.undu.com/articles/980317b.htm]The Clipboard and WAV files[/URL]
    Sending and retrieving sound files to the Clipboard.


  • Old UBB Archived Link: [URL=http://support.microsoft.com/support/kb/articles/q106/3/86.asp]Retrieve DIBs from the Clipboard[/URL]
    "The difference stems from the fact that a bitmap is a GDI object and a DIB is a global memory object."


  • Old UBB Archived Link: [URL=http://www.ora.com/centers/gff/formats/micclip/index.htm]Microsoft Windows Clipboard[/URL]
    File formats and organizations.


  • Old UBB Archived Link: [URL=http://www.tarkvara.com/components/selcompn/]SelectedComponentsExpert[/URL]
    This expert copies to the Clipboard full names of all selected components from all loaded proejct forms and data modules.




I hope this has been helpful. Near the end of the article it talks about "Listening to the Clipboard" for notification when the clipboard changes. If you have an interest in this, I can send you a component I wrote for Delphi which hooks into the Clipboard chain and notifies you (by an event) when the clipboard contents change.

Hope this helps,
Larry
Larry J. Rutledge

Analyst

Prestwood Software

www.prestwood.com
 Posted 17 years ago
Comment Quote
About Larry J. Rutledge -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
About Larry J. Rutledge
I worked for Prestwood Software as a Delphi developer from 1997 through 2002. During that time I enjoyed working with Mike Prestwood and the other developers at Prestwood.

Post ID #4893 (Level 1.1)  Reply to 4892
Thread Started 1/17/2002 12:10:00 PM
View Counter=2
Location=Citrus Heights, CA USA 
Joined=19 years ago   MB Posts=73   KB Posts=9  
Most Recent Post
Xion
-- USA
Problem solved. Now thats what I call help :)
 Posted 17 years ago (Thread Starter)
Comment Quote
About Xion -Collapse +Expand
Visit Profile
Membership pending.
Member subscribes to this thread but email is NOT VERIFIED.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #4899 (Level 1.2)  Reply to 4892
Reply Posted 1/17/2002 4:33:00 PM
Location=-- USA 
Joined=17 years ago   MB Posts=8  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1222A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #4892 Counter
3437
Since 4/2/2008
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech