C # Operating system Clipboard processing module

Source: Internet
Author: User
Tags bool

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;


    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.