How to retrieve clipboard format and data from any application

Mansour_Dalir 1,936 Reputation points
2024-08-31T17:49:36.1866667+00:00

How can I use Visual Basic (VB) to retrieve the format and data stored on the clipboard of any running application? I want to build a tool that extracts this information.

20240831_210801

As you can see in the program, I copy and paste the rectangle. I want to retrieve the clipboard data so that I can get the size of the rectangle.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,652 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

Accepted answer
  1. Castorix31 85,881 Reputation points
    2024-08-31T21:15:43+00:00

    In your GIF, this app uses a custom Clipboard format (PXCEditCbData), so it cannot be generic

    With this test, I get the size of the Rectangle from PXCEditCbData (nValues array), but it will be different for any other application :

    Public Class Form1
    
        Private Const WM_CLIPBOARDUPDATE As Integer = &H31D
    
        <DllImport("User32.dll", SetLastError:=True)>
        Private Shared Function AddClipboardFormatListener(hwnd As IntPtr) As Boolean
        End Function
    
        <DllImport("User32.dll", SetLastError:=True)>
        Private Shared Function RemoveClipboardFormatListener(hwnd As IntPtr) As Boolean
        End Function
    
        Protected Overrides Sub OnHandleCreated(e As EventArgs)
            MyBase.OnHandleCreated(e)
            AddClipboardFormatListener(Me.Handle)
        End Sub
    
        Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
            MyBase.OnHandleDestroyed(e)
            RemoveClipboardFormatListener(Me.Handle)        
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = WM_CLIPBOARDUPDATE Then
                GetClipboardContent()
            End If
        End Sub
    
        Private Sub GetClipboardContent()
            If Clipboard.ContainsImage() Then
                Dim clipboardImage As Image = Clipboard.GetImage()
                Dim nImageSize As Size = clipboardImage.Size           
            ElseIf Clipboard.ContainsData("PXCEditCbData") Then
                Dim dataObject As Object = Clipboard.GetData("PXCEditCbData")
                If TypeOf dataObject Is MemoryStream Then
                    Dim stream As MemoryStream = DirectCast(dataObject, MemoryStream)
                    ' BBox [0 0 6.237762 148.957275]
                    Dim sResult As String = SearchForBBoxString(stream)
                    Dim sValues As String = sResult.Substring(6, sResult.Length - 7)
                    Dim sStringValues() As String = sValues.Split(" "c)
                    Dim nValues(3) As Single
                    For i As Integer = 0 To 3
                        nValues(i) = Single.Parse(sStringValues(i), CultureInfo.InvariantCulture)
                    Next
                End If
            End If
        End Sub
    
        Private Function SearchForBBoxString(stream As MemoryStream) As String
            stream.Position = 0
            Dim reader As New StreamReader(stream, Encoding.ASCII)
            Dim sContent As String = reader.ReadToEnd()
            Dim nStartIndex As Integer = sContent.IndexOf("BBox [")
            If nStartIndex >= 0 Then
                Dim nEndIndex As Integer = sContent.IndexOf("]", nStartIndex)
                If nEndIndex > nStartIndex Then
                    Dim sString As String = sContent.Substring(nStartIndex, nEndIndex - nStartIndex + 1)
                    Return sString
                End If
            End If
            Return Nothing
        End Function  
    End Class
    
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.