Clipboard operation

来源:互联网 发布:vb最新版下载 编辑:程序博客网 时间:2024/04/20 14:22

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports System.Diagnostics

'This class is used to operating systems clipboard. Thread is most important thing.

Public Class ClipBoard

 

    Dim str As String

 

    Public Function GetText() As String

        Dim t As New System.Threading.Thread(AddressOf GetTextThreaded)

        t.SetApartmentState(Threading.ApartmentState.STA)

        t.Start()

        t.Join()

        Return str

    End Function

 

    Public Sub SetText(ByVal s As String)

        str = s

        Dim t As New System.Threading.Thread(AddressOf SetTextThreaded)

        t.SetApartmentState(Threading.ApartmentState.STA)

        t.Start()

        t.Join()

    End Sub

 

    Private Sub GetTextThreaded()

        Dim o As System.Windows.Forms.IDataObject

        o = System.Windows.Forms.Clipboard.GetDataObject()

 

        If (o Is Nothing) Then

            str = ""

        Else

            str = o.GetData(System.Windows.Forms.DataFormats.StringFormat)

        End If

    End Sub

 

    Private Sub SetTextThreaded()

        System.Windows.Forms.Clipboard.SetDataObject(str, True)

    End Sub

 

End Class

 

Usage: 

        'Copy to clipboard

        Dim cb As New ClipBoard()

        cb.SetText("Whatever")

原创粉丝点击