Using the Clipboard
Earlier lessons have explained how to interact with the user (through controls), the file system,
and most recently the printer. This lesson explains how your program can interact with other
programs via the clipboard.
In this lesson, you learn how to place text, image, and other data on the clipboard so you can
paste it into other applications. You also learn how to receive data pasted into your program
from the clipboard.
ADDING DATA TO THE CLIPBOARD
Adding data to the clipboard is fairly easy. The Clipboard class provides several static meth-
ods to place certain kinds of data on the clipboard:
SetAudio
Copies an audio stream to the clipboard.
SetFileDropList
Copies a list of files to
the clipboard as if the user had selected them in
Windows Explorer and pressed Ctrl+C.
SetImage
Copies an image to the clipboard.
SetText
Copies a string to the clipboard.
Example program AddToClipboard, shown in Figure 32-1
and included as part of this lessons code download, dem-
onstrates these methods. Use the Copy buttons to copy the
corresponding content on the left to the clipboard. (When
the program starts, it copies the names of the files in its
startup directory into the list box. The bottom image rep-
resents an audio file.)
FIGURE 321
32
596906c32.indd 369 4/7/10 12:34:34 PM
370
LESSON 32 Using the Clipboard
The program uses the following code to copy data to the clipboard:
// Copy the text to the clipboard.
// Then manually try pasting into Word, WordPad, etc.
private void copyTextButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(dataTextBox.Text);
System.Media.SystemSounds.Beep.Play();
}
// Copy the picture to the clipboard.
// Then try pasting into Word, Paint, etc.
private void copyPictureButton_Click(object sender, EventArgs e)
{
Clipboard.SetImage(dataPictureBox.Image);
System.Media.SystemSounds.Beep.Play();
}
// Copy the picture to the clipboard.
// Then try pasting into ReadFromClipboard.
private void copyFilesButton_Click(object sender, EventArgs e)
{
// Make a StringCollection holding the file names.
StringCollection files = new StringCollection();
foreach (string filename in filesListBox.Items)
{
files.Add(filename);
}
// Save the list to the clipboard.
Clipboard.SetFileDropList(files);
System.Media.SystemSounds.Beep.Play();
}
// Copy some audio to the clipboard.
private void copyAudioButton_Click(object sender, EventArgs e)
{
Clipboard.SetAudio(Properties.Resources.boing);
using (SoundPlayer player =
new SoundPlayer(Properties.Resources.boing))
{
player.Play();
}
}
The text and image copying event handlers are straightforward. They simply use the appropriate
Clipboard method to copy data to the clipboard and then play the system’s beep sound.
The file list copying button builds a
StringCollection, adds the files in the list box to it and calls
the
Clipboard’s SetFileDropList.
596906c32.indd 370 4/7/10 12:34:34 PM
Getting Data from the Clipboard
371
The StringCollection class is in the System.Collections namespace, so the
program includes a
using directive to make using the class easier.
The audio copying button copies an audio resource to the clipboard.
To add an audio resource to the project at design time, open the Project menu and select Properties
at the bottom. Next click the Resources tab. Click the Add Resource dropdown arrow at the top of
the page and select Add Existing File. Select the audio file that you want to add to the project and
click Open.
Visual Studio adds the audio file to the project and creates a memory stream property that you can
use to refer to it. In this example, the file I used was named boing.wav so the program can refer to it
as
Properties.Resources.boing.
The audio copying button calls the
Clipboard’s SetAudio method, passing it the audio resource.
Then instead of playing the systems beep sound, the program creates a
SoundPlayer associated
with the audio resource and calls the player’s
Play method.
The SoundPlayer class is in the System.Media namespace, so the program
includes a
using directive to make using the class easier.
Programs that accept audio pasted from the clipboard are uncommon. To
test this program’s ability to copy audio to the clipboard, you can use the
ReadFromClipboard example program described in the following section.
GETTING DATA FROM THE CLIPBOARD
Getting data from the clipboard is just as easy as putting it there because the Clipboard class provides
static methods that return audio streams, file drop lists, images, and text.
Before you try to copy data from the clipboard, however, you should make sure the data you want is
present. To let you do that, the
Clipboard class also provides a set of functions that tell you whether
data in a given format is available.
For example, the following code checks whether text is available on the clipboard and, if it is, copies
the text into the textbox named
dataTextBox:
if (Clipboard.ContainsText()) dataTextBox.Text = Clipboard.GetText();
596906c32.indd 371 4/7/10 12:34:35 PM
372
LESSON 32 Using the Clipboard
The ReadFromClipboard example program (included in this lessons code download) uses the following
code to paste audio, file drop lists, images, or text from the clipboard:
// Paste whatever data is available from the clipboard.
private void pasteButton_Click(object sender, EventArgs e)
{
// Clear previous results.
dataTextBox.Clear();
filesListBox.Items.Clear();
dataPictureBox.Image = null;
// Paste the text if available.
if (Clipboard.ContainsText()) dataTextBox.Text = Clipboard.GetText();
// Paste the picture if available.
if (Clipboard.ContainsImage()) dataPictureBox.Image =
Clipboard.GetImage();
// Paste file drop list if available.
if (Clipboard.ContainsFileDropList())
{
StringCollection files = Clipboard.GetFileDropList();
foreach (string filename in files)
{
filesListBox.Items.Add(filename);
}
}
// Paste audio if available.
if (Clipboard.ContainsAudio())
{
using (SoundPlayer player =
new SoundPlayer(Clipboard.GetAudioStream()))
{
player.Play();
}
}
}
The code starts by clearing its current text, file list, and image. It then copies whatever data is avail-
able from the clipboard.
If the program finds audio content is available, it makes a
SoundPlayer associated with the audio
stream provided by the clipboard and plays it.
TRY IT
In this Try It, you create a program that backs up files. The program contains a textbox where
you can enter a directory name and a button. When you click the button, the program copies any
les in the clipboard’s file drop list into the backup directory, adding a numeric version number
at the end.
596906c32.indd 372 4/7/10 12:34:35 PM
Try It
373
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson32 folder of the download.
Lesson Requirements
Start a new project and add a textbox and button.
When the user clicks the button, see if the clipboard contains a file drop list. If it does:
Use the value in the textbox to create a
DirectoryInfo object. Create the directory
if necessary.
For each file in the drop list, make a
FileInfo object. Try adding 001, 002, 003, and
so on to the file’s name and see if there’s already a file with that name in the backup
directory. When you find a filename that isn’t already in use, copy the file using that
name.
Hints
You can use
Path.Combine to combine the directory’s path and the file’s name.
Step-by-Step
Start a new project and add a textbox and button.
1. This is straightforward.
When the user clicks the button, see if the clipboard contains a file drop list. If it does:
Use the value in the textbox to create a
DirectoryInfo object. Create the directory
if necessary.
For each file in the drop list, make a
FileInfo object. Try adding 001, 002, 003, and so
on to the file’s name and see if there’s already a file with that name in the backup direc-
tory. When you find a filename that isn’t already in use, copy the file using that name.
1. Use code similar to the following:
// Back up any les listed on the clipboard’s le drop list.
private void backupButton_Click(object sender, EventArgs e)
{
if (Clipboard.ContainsFileDropList())
{
// Get the backup directory.
DirectoryInfo dirInfo =
new DirectoryInfo(directoryTextBox.Text);
596906c32.indd 373 4/7/10 12:34:36 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.44.183