Recipe 14.12. Accessing the Clipboard


Problem

You want to store data on the clipboard or retrieve data already found on the clipboard.

Solution

Use the My.Computer. Clipboard object to get and set data on the clipboard. This object includes four types of methods:

  • Contains… methods that indicate whether data of a particular type can be found right now on the clipboard

  • Get… methods that retrieve data already found on the clipboard in a specific data format

  • Set… methods that allow you to place data onto the clipboard in one or more predefined or custom formats

  • A Clear( ) method that removes all data from the clipboard

Each Contains…, Get…, and Set… method sets focuses on six types of data:

  • Text

  • Images

  • Sound files

  • Sets of files

  • Custom data

  • Custom data in multiple formats

To retrieve plain text data found on the clipboard, use the following statement:

 Dim fromClipboard As String = _      My.Computer.Clipboard.GetText( ) 

Use the Clear( ) method to remove all data from the clipboard:

 My.Computer.Clipboard.Clear( ) 

Discussion

The My.Computer.Clipboard object includes six distinct Get… methods that let you retrieve the contents of the system clipboard, each one based on a different type of data:


GetAudioStream( )

Retrieves audio content from the clipboard as a System.IO.Stream object. Any .NET features that support such streams can use the returned data. The following block of code plays a sound file retrieved from the clipboard:

 My.Computer.Audio.Play( _    My.Computer.Clipboard.GetAudioStream( ), _    AudioPlayMode.Background) 


GetFileDropList( )

Retrieves a list of file paths as a String collection. This collection is created by any application that stores compatible file lists on the clipboard. For instance, if you copy files in Windows Explorer, those files (but not their contents) appear on the clipboard as a File Drop List. Use this code to retrieve that list:

 Dim allFiles As System.Collections.Specialized. _    StringCollection = _    My.Computer.  Clipboard.GetFileDropList( ) Dim oneFile As String For Each oneFile In allFiles    ' ----- Process each file here. Next oneFile 


GetImage( )

Retrieves any image data stored on the clipboard as a System.Drawing.Image object.


GetText( )

Retrieves text from the clipboard. GetText( ) includes an optional parameter that lets you specify the specific type of text to retrieve, using the values of the System.Windows.Forms.TextDataFormat enumeration. Their names equate to the type of text retrieved:

  • TexTDataFormat.CommaSeparatedValue

  • TextdataFormat.Html

  • TextdataFormat.Rtf

  • TextdataFormat.UnicodeText

If you don't include the text type argument, GetText( ) retrieves the text in the most basic text format available on the clipboard.


GetData( )

Retrieves data in a custom format from the clipboard. All data stored on the clipboard includes a format name. You must pass a format name to the Getdata( ) argument to retrieve data of that type. For example:

 Dim roundaboutText = _    CStr(My.Computer.Clipboard.GetData("Text")) 

The data is returned as a System.Object, and it must be converted to its final data type manually.


GetDataObject( )

The clipboard can store data in multiple formats at once. GetdataObject( ) returns the complete set of all stored data formats, using an interface defined through System.Windows.Forms.IDataObject. Once retrieved, you can query the names of each format using this interface's GetFormats( ) method, check for a specific format using GeTDataPresent( ), and retrieve specific data as a System.Object using GeTData( ). The following code displays the names of each format included on the clipboard:

 MsgBox(Join(My.Computer.Clipboard.GetDataObject( ). _    GetFormats(True), ", ")) 

Before attempting to retrieve data in a specific format from the clipboard, it is a good idea to confirm that such data exists. (If the specified data type does not exist, the Get… methods return the value Nothing.) The My.Computer. Clipboard object includes several such confirmation methods that parallel the Get… methods listed above, each of which returns a Boolean value indicating whether or not the specified data is available:

  • Clipboard.ContainsAudio()

  • Clipboard.ContainsData(formatName)

  • Clipboard.ContainsFileDropList()

  • Clipboard.ContainsImage()

  • Clipboard.ContainsText(formatType)

Since the system clipboard is a resource shared among all running programs, and since the user can modify the clipboard through another program at any time, it is possible that one of these Contains… methods will return true for a particular format, but the related Get… method, even when used immediately, will return nothing.

A group of Set… methods let you store data back to the clipboard in a variety of formats:


SetAudio( )

Stores audio data on the clipboard. The lone argument to this method must be either a Byte array or a Stream containing audio data.


SetFileDropList( )

Stores a list of files on the clipboard. You must pass a collection of strings using the System.Collections.Specialized.StringCollection to this method. For example:

 Dim filesToInclude As New System.Collections. _    Specialized.StringCollection filesToInclude.Add("c:\datafile.txt") filesToInclude.Add("c:\temp\workfile.txt") My.Computer.Clipboard.SetFileDropList(filesToInclude) 


SetImage( )

Stores an image on the clipboard. Pass this method an argument of type System.Drawing.Image.


SetText( )

Stores text in a specific format on the clipboard. The first argument is a String containing the text to add. An optional second argument uses the TexTDataFormat enumeration discussed in the earlier GetText( ) entry.


SetData( )

Stores any type of custom data on the clipboard, based on a format name you provide:

 My.Computer.Clipboard.SetData("MyCustomFormat", dataObject) 


SetDataObject( )

Lets you append multiple formats at once to the clipboard. You must pass this method an instance of System.Windows.Forms.DataObject, populated with data you provide. This object includes each of the Set… methods used for the clipboard itself, including SetText( ) and SetData( ):

   Dim toClipboard As New System.Windows.Forms.DataObject toClipboard.SetData("MyCustomFormat", dataObject) toClipboard.SetText(dataObject.ToString( )) My.Computer.Clipboard.SetDataObject(toClipboard) 




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net