剪貼板 (Clipboard)

Clipboard Copy and Paste with C#

This simple tutorial will show you how to access the Windows clipboard in .NET 2.0 using C#. Start by creating a Windows Application inside Visual Studio 2005 and to the form add a TextBox (txtClipboard), a PictureBox (picClipboard) andfive buttons (btnCopyText, btnPasteText, btnCopyImage, btnPasteImage, btnClear). Also add a OpenFileDialog object and name it opnImage. The form elements should be arranged so that it looks similar to the one below:

Clipboard Manager Form

As you can see here, the functionality of copying and pasting text is separated from the one of copying and pasting images, and it makes sense, since you can't always paste images in the same place you are pasting text, unless you are using an advanced container that supports both types of content, such as Microsoft Word. Since in our application we don't have a control where both images and text can be pasted, we are using a TextBox for the text, and a PictureBox for the images.

The first functionality to be added is copying text to the clipboard. Double click the btnCopyText button and you will get to its Click event. Inside it there is only one line to add:


Clipboard.SetText(txtClipboard.Text);


We are using the SetText() method to set the clipboard to the text inside the txtClipboard TextBox.
Now it's time to paste, thus double click the btnPasteText button and once you get to its Click event, use the following line:


txtClipboard.Text = Clipboard.GetText();


Here the TextBox is being set to the content of the clipboard (as long as the content is plain text).

When copying an image, we first need to allow the user to pick an image, and here's where the OpenFileDialog object comes into action. Double click the btnCopyImage button so that Visual Studio 2005 can create its Click event, and use the following piece of code:


if (opnImage.ShowDialog() == DialogResult.OK)

{

Image imgToCopy = Image.FromFile(opnImage.FileName);

Clipboard.SetImage(imgToCopy);

}


First we check to see if the user has clicked OK and thus selected a file. If he did, we create an Image object from that file. Please note that at this point, in a real-life application, you will want to allow the user to only select a number of file types (GIF, JPEG, PNG, etc.), and also check after the selection was made and before it is put into the clipboard, if a valid image file was selected.
After the Image object is created by passing the path of the image file, we put this object into the clipboard.

Now double click the btnPasteImage button and inside the Click event, we're going to put a line of code that pastes the content of the clipboard into a PictureBox. Pasting images is very similar to pasting text, only that now we use the GetImage() method instead of GetText() and for the container we use a PictureBox control instead of a TextBox control:


picClipboard.Image = Clipboard.GetImage();


As for clearing the clipboard, it couldn't get any easier. If you double click the btnClear button and use the following line of code inside the btnClear_Click event, you are done:


Clipboard.Clear();


Thanks to the Clipboard class, this tutorial was a very easy one. If you wish to download the code as a Visual Studio 2005 C# Solution, use the link at the top of the tutoria

http://www.geekpedia.com/tutorial188_Clipboard-Cop...

陳鍾誠 (2011年12月19日),(網頁標題) C# 視窗程式 — 剪貼板 (Clipboard),(網站標題) 免費電子書:C# 程式設計,2011年12月19日,取自 http://cs0.wikidot.com/clipboard ,網頁修改第 0 版。

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()