您的位置:首页 > 编程语言 > VB

VB.net 监视Windows剪切板中数据变化

2016-02-27 23:30 756 查看
新建一个Winform,拉进去一个PictureBox控件,完整代码如下,实现效果即当剪切板中更新图片数据的时候,picturebox中自动显示出来剪切板中的图片

Public Class clipboardMon
#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 "
Public Sub NewViewer()
'InitializeComponent()
'To register this form as a clipboard viewer...
Clipboard.Clear()
mNextClipBoardViewerHWnd = SetClipboardViewer(Me.Handle)

End Sub
#End Region
#Region " Message Process "
'Override WndProc to get messages...
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case Is = WM_DRAWCLIPBOARD 'The clipboard has changed...
'##########################################################################
' Process Clipboard Here :)........................
'##########################################################################
SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam, m.LParam)

'显示剪贴板中的图片信息
If Clipboard.ContainsImage() = True Then
PictureBox1.Image = Clipboard.GetImage()
PictureBox1.Update()
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
#End Region
Private Sub clipboardMon_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NewViewer()
End Sub
End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: