clipboard code for vb.net in .netcf

R

Rudis Muiznieks

There's a lot of copies of the C# code that p/invokes the clipboard
functions, but I wasn't able to find it in VB code. So I did up my own
class for it. Here's the code for anyone else who's interested (I have
no doubt that Google will garble the carriage returns, sorry I'm too
lame to use a real newsreader):

== Begin code from Clipboard.vb ==

Imports System.Runtime.InteropServices

Public Class Clipboard

#Region "Win32 API"
<DllImport("Coredll.dll", EntryPoint:="OpenClipboard", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As
Boolean
End Function

<DllImport("Coredll.dll", EntryPoint:="EmptyClipboard", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function EmptyClipboard() As Boolean
End Function

<DllImport("Coredll.dll", EntryPoint:="SetClipboardData", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function SetClipboardData(ByVal uFormat As Integer,
ByVal hWnd As IntPtr) As IntPtr
End Function

<DllImport("Coredll.dll", EntryPoint:="CloseClipboard", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function CloseClipboard() As Boolean
End Function

<DllImport("Coredll.dll", EntryPoint:="GetClipboardData", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function GetClipboardData(ByVal uFormat As Integer)
As IntPtr
End Function

<DllImport("Coredll.dll",
EntryPoint:="IsClipboardFormatAvailable", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As
Integer) As Boolean
End Function

<DllImport("Coredll.dll", EntryPoint:="LocalAlloc", _
SetLastError:=True, CallingConvention:=CallingConvention.Winapi)>
_
Public Shared Function LocalAlloc(ByVal uFlags As Integer, ByVal
uBytes As Integer) As IntPtr
End Function
#End Region

Public ReadOnly Property HasContent() As Boolean
Get
Return IsClipboardFormatAvailable(13)
End Get
End Property

Public Property Content() As String
Get
Dim Value As String
If HasContent Then
If OpenClipboard(IntPtr.Zero) Then
Dim hData As IntPtr = GetClipboardData(13)
Value = Marshal.PtrToStringUni(hData)
CloseClipboard()
End If
End If
Return Value
End Get
Set(ByVal Value As String)
If OpenClipboard(IntPtr.Zero) Then
EmptyClipboard()

Dim hMem As IntPtr = LocalAlloc(0,
Value.ToCharArray.Length * Marshal.SystemDefaultCharSize)
If hMem.ToInt32 > 0 Then
Marshal.Copy(Value.ToCharArray, 0, hMem,
Value.Length)
SetClipboardData(13, hMem)
End If
CloseClipboard()
End If
End Set
End Property

End Class


== End code from "Clipboard.vb" ==

This class deals only with unicode String values, but could be
modified to handle other complex objects. It's easy to use, for
example:

Dim cb As New Clipboard ' Create the clipboard object
cb.Content = "i am copying this text" ' set the contents of the
clipboard
If cb.HasContent Then ' returns true if there's unicode text in the
clipboard
Dim myStr As String = cb.Content ' get the contents of the
clipboard
End If

That's it.
-Rudis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top