2012-05-15 39 views
0

iam使用下面的代碼來檢查剪貼板中的文本更改,但不支持unicode字符,如ابج是否有任何添加對unicode文本支持的方式,或者是否存在任何其他方式做同樣的工作?在vb.net中使用unicode支持檢測剪貼板文本

#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 

回答

1

您的代碼無法檢測到Unicode文本的更改,因爲它沒有查找Unicode文本格式。 Ansi和Unicode使用不同的剪貼板格式,並且它們可以同時在剪貼板上共存。 DataFormats.Text只支持Ansi文本。該documentation甚至說之多:

指定標準ANSI文本格式

你需要尋找DataFormats.UnicodeText,而不是,或除了,DataFormats.Text,如:

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 
+0

謝謝Lebeau我使用Clipboard.GetText來解決這個問題,但是當你證明這個問題時,它幫助我理解邏輯。祝你今天愉快。 – user934820