user934820
user934820

Reputation: 1178

Detect Clipboard text change with unicode support in vb.net

iam using the below code to check text change in clipboard but its not supported with unicode chracters like ا ب ج is there any way to add support for unicode text or is there any other way to do same work?

#Region " Definitions "
    'Constants for API Calls...
    Private Const WM_DRAWCLIPBOARD As Integer = &H308
    Private Const WM_CHANGECBCHAIN As Integer = &H30D
    'Handle for next clipboard viewer...
    Private mNextClipBoardViewerHWnd As IntPtr
    'API declarations...
    Declare Auto Function SetClipboardViewer Lib "user32" (ByVal HWnd As IntPtr) As IntPtr
    Declare Auto Function ChangeClipboardChain Lib "user32" (ByVal HWnd As IntPtr, ByVal HWndNext As IntPtr) As Boolean
    Declare Auto Function SendMessage Lib "User32" (ByVal HWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Long
#End Region

#Region " Contructor "
#End Region

#Region " Message Process "
    'Override WndProc to get messages...
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim iData As IDataObject = New DataObject()
        iData = Clipboard.GetDataObject()
        Select Case m.Msg

            Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed...
                '##########################################################################
                ' Process Clipboard Here :)........................
                '##########################################################################
                MsgBox(CStr(iData.GetData(DataFormats.Text)))
                SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)

            Case Is = WM_CHANGECBCHAIN 'Another clipboard viewer has removed itself...
                If m.WParam = CType(mNextClipBoardViewerHWnd, IntPtr) Then
                    mNextClipBoardViewerHWnd = m.LParam
                Else
                    SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)
                End If
        End Select

        MyBase.WndProc(m)
    End Sub
#End Region

#Region " Dispose "
    'Form overrides dispose to clean up...
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            'Set the next clipboard viewer back to the original... 
            ChangeClipboardChain(Me.Handle, mNextClipBoardViewerHWnd)
            MyBase.Dispose(disposing)
        End If
    End Sub

Upvotes: 0

Views: 2473

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596948

Your code is not able to detect changes for Unicode text because it is not looking for the Unicode text format. Ansi and Unicode use different clipboard formats, and they can coexist on the clipboard at the same time. DataFormats.Text only supports Ansi text. The documentation even says as much:

Specifies the standard ANSI text format

You need to look for DataFormats.UnicodeText instead of, or in addition to, DataFormats.Text, eg:

Protected Overrides Sub WndProc(ByRef m As Message) 
    Select Case m.Msg 
        Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed... 
            '########################################################################## 
            ' Process Clipboard Here :)........................ 
            '########################################################################## 
            Dim iData As IDataObject = Clipboard.GetDataObject() 
            Dim oData as Object = iData.GetData(DataFormats.Text)
            If oData IsNot Nothing
              MsgBox(CStr(oData), MsgBoxStyle.OKOnly, "Ansi") 
            End If
            oData = iData.GetData(DataFormats.UnicodeText)
            If oData IsNot Nothing
              MsgBox(CStr(oData), MsgBoxStyle.OKOnly, "Unicode") 
            End If

        Case Is = WM_CHANGECBCHAIN 'Another clipboard viewer has removed itself... 
            If m.WParam = CType(mNextClipBoardViewerHWnd, IntPtr) Then 
                mNextClipBoardViewerHWnd = m.LParam 
            Else 
                SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam) 
            End If 
    End Select 

    MyBase.WndProc(m) 
End Sub 

Upvotes: 1

Related Questions