Skip to content

Instantly share code, notes, and snippets.

@KOZ60
Last active October 1, 2023 22:24
Show Gist options
  • Save KOZ60/7630c63d72fe381e12871f7e56254bee to your computer and use it in GitHub Desktop.
Save KOZ60/7630c63d72fe381e12871f7e56254bee to your computer and use it in GitHub Desktop.
Clipboard Monitor

1.SetClipboardViewer

Imports System.Runtime.InteropServices

Public Class Form1

    Private hwndNextChain As IntPtr

    Public Sub New()
        InitializeComponent()
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        hwndNextChain = SetClipboardViewer(Me.Handle)
    End Sub

    Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
        MyBase.OnHandleDestroyed(e)
        ChangeClipboardChain(Me.Handle, hwndNextChain)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_DRAWCLIPBOARD
                If hwndNextChain <> IntPtr.Zero Then
                    SendMessage(hwndNextChain, m.Msg, m.WParam, m.LParam)
                End If
                If Clipboard.ContainsText() Then
                    Dim clipboardText As String = Clipboard.GetText()
                    TextBox1.AppendText(clipboardText & vbNewLine & vbNewLine)
                    Debug.WriteLine($"{Now} Change Clipboard")
                End If

            Case WM_CHANGECBCHAIN
                If m.WParam = hwndNextChain Then
                    hwndNextChain = m.LParam
                ElseIf hwndNextChain <> IntPtr.Zero Then
                    SendMessage(hwndNextChain, m.Msg, m.WParam, m.LParam)
                End If
        End Select
        MyBase.WndProc(m)
    End Sub

    <DllImport("User32.Dll")>
    Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function SetClipboardViewer(hWndNewViewer As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function ChangeClipboardChain(hWndRemove As IntPtr, hWndNewNext As IntPtr) As Boolean
    End Function

    Private Const WM_DRAWCLIPBOARD As Integer = &H308
    Private Const WM_CHANGECBCHAIN As Integer = &HD

End Class

2.AddClipboardFormatListener

Imports System.Runtime.InteropServices

Public Class Form2

    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)
        Select Case m.Msg
            Case WM_CLIPBOARDUPDATE
                If Clipboard.ContainsText() Then
                    Dim clipboardText As String = Clipboard.GetText()
                    TextBox1.AppendText(clipboardText & vbNewLine & vbNewLine)
                    Debug.WriteLine($"{Now} Change Clipboard")
                End If
        End Select
        MyBase.WndProc(m)
    End Sub

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function AddClipboardFormatListener(hWndNewViewer As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function RemoveClipboardFormatListener(hWndNewViewer As IntPtr) As Boolean
    End Function

    Private WM_CLIPBOARDUPDATE As Integer = &H31D

End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment