CODE:
///<summary>
///Operating system Clipboard processing module, the method provided is static function
///</summary>
///<example>
///Use examples of this class in the C # language to obtain plain text data from the operating system Clipboard
/////Determine if the operating system Clipboard has saved plain text data
///if (Clipboardhandler.cangettext ())
/// {
/////Return to the obtained plain text data
///return Clipboardhandler.gettextfromclipboard ();
/// }
///
///sets plain text data to the operating system Clipboard
///String strText = "Plain text data to set";
///Clipboardhandler.settexttoclipboard (strText);
///</example>
public class Clipboardhandler
{
/**////<summary>
///Whether you can get text from the operating system Clipboard
///</summary>
///<returns>true can get text from the operating system Clipboard, false can not be </returns>
public static bool Cangettext ()
{
//Clipboard.getdataobject may throw a exception ...
Try
{
System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject ();
return data!= null && data. GetDataPresent (System.Windows.Forms.DataFormats.Text);
}
catch (Exception e)
{
return false;
}
}
//
/////<summary>
/////Whether you can set text to the operating system Clipboard
/////</summary>
/////<returns></returns>
//public static bool Cansettext ()
// {
//return true;
// }
/**////<summary>
Set text data to the operating system Clipboard
///</summary>
///<param name= "StrText" > Text data </param>
///<returns> Operation Success </returns>
public static bool Settexttoclipboard (string strText)
{
if (strText!= null && strtext.length > 0)
{
Try
{
System.Windows.Forms.DataObject DataObject = new System.Windows.Forms.DataObject ();
Dataobject.setdata (System.Windows.Forms.DataFormats.UnicodeText, True, StrText);
System.Windows.Forms.Clipboard.SetDataObject (DataObject, true);
return true;
}
Catch
{
}
}
return false;
}
/**////<summary>
///get text from the operating system Clipboard
///</summary>
///<returns> obtained text, if the operation fails, return empty object </returns>
public static string Gettextfromclipboard ()
{
Try
{
System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject ();
if (data. GetDataPresent (System.Windows.Forms.DataFormats.UnicodeText))
{
String strText = (string) data. GetData (System.Windows.Forms.DataFormats.UnicodeText);
return strText;
}
}
Catch
{}
return null;
}