PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Visual Studio 2008/VBWPFClipboardViewer/MainWindow.xaml.vb

#
Visual Basic | 150 lines | 85 code | 30 blank | 35 comment | 0 complexity | d64c3717567905d60a023a0d3714f2c5 MD5 | raw file
  1'****************************** Module Header ******************************'
  2' Module Name:  MainWindow.xaml.vb
  3' Project:      VBWPFClipboardViewer
  4' Copyright (c) Microsoft Corporation.
  5' 
  6' The VBWPFClipboardViewer project provides the sample on how to monitor
  7' Windows clipboard changes in a WPF application.
  8' 
  9' This source is subject to the Microsoft Public License.
 10' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 11' All other rights reserved.
 12' 
 13' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 14' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 15' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
 16'\***************************************************************************'
 17
 18Class MainWindow
 19
 20#Region "Private fields"
 21
 22    ''' <summary>
 23    ''' Next clipboard viewer window
 24    ''' </summary>
 25    Private hWndNextViewer As IntPtr
 26
 27    ''' <summary>
 28    ''' The <see cref="HwndSource"/> for this window.
 29    ''' </summary>
 30    Private hWndSource As HwndSource
 31
 32    Private isViewing As Boolean
 33
 34#End Region
 35
 36#Region "Clipboard viewer related methods"
 37
 38    Private Sub InitCBViewer()
 39        Dim wih As New WindowInteropHelper(Me)
 40        hWndSource = HwndSource.FromHwnd(wih.Handle)
 41
 42        hWndSource.AddHook(AddressOf WinProc)   ' start processing window messages
 43        hWndNextViewer = Win32.SetClipboardViewer(hWndSource.Handle)    ' set this window as a viewer
 44        isViewing = True
 45    End Sub
 46
 47    Private Sub CloseCBViewer()
 48        ' remove this window from the clipboard viewer chain
 49        Win32.ChangeClipboardChain(hWndSource.Handle, hWndNextViewer)
 50
 51        hWndNextViewer = IntPtr.Zero
 52        hWndSource.RemoveHook(AddressOf WinProc)
 53        pnlContent.Children.Clear()
 54        isViewing = False
 55    End Sub
 56
 57    Private Sub DrawContent()
 58        pnlContent.Children.Clear()
 59
 60        If Clipboard.ContainsText() Then
 61            ' we have some text in the clipboard
 62            Dim tb As New TextBox
 63            tb.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
 64            tb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
 65            tb.Text = Clipboard.GetText()
 66            tb.IsReadOnly = True
 67            tb.TextWrapping = TextWrapping.NoWrap
 68            pnlContent.Children.Add(tb)
 69
 70        ElseIf Clipboard.ContainsFileDropList() Then
 71            ' we have a file drop list in the clipboard
 72            Dim lb As New ListBox
 73            lb.ItemsSource = Clipboard.GetFileDropList()
 74            pnlContent.Children.Add(lb)
 75
 76        ElseIf Clipboard.ContainsImage() Then
 77            ' Because of a known issue in WPF,
 78            ' we have to use a workaround to get correct
 79            ' image that can be displayed.
 80            ' The image have to be saved to a stream and then 
 81            ' read out to workaround the issue.
 82            Dim ms As New MemoryStream
 83            Dim enc As New BmpBitmapEncoder
 84            enc.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()))
 85            enc.Save(ms)
 86            ms.Seek(0, SeekOrigin.Begin)
 87
 88            Dim dec As New BmpBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
 89            Dim img As New Image
 90            img.Stretch = Stretch.Uniform
 91            img.Source = dec.Frames(0)
 92            pnlContent.Children.Add(img)
 93
 94        Else
 95            Dim lb As New Label
 96            lb.Content = "The type of the data in the clipboard is not supported by this sample."
 97            pnlContent.Children.Add(lb)
 98        End If
 99    End Sub
100
101    Private Function WinProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByRef handled As Boolean) As IntPtr
102        Select Case msg
103            Case Win32.WM_CHANGECBCHAIN
104                If wParam = hWndNextViewer Then
105                    ' clipboard viewer chain changed, need to fix it.
106                    hWndNextViewer = lParam
107
108                ElseIf hWndNextViewer <> IntPtr.Zero Then
109                    ' pass the message to the next viewer
110                    Win32.SendMessage(hWndNextViewer, msg, wParam, lParam)
111                End If
112
113            Case Win32.WM_DRAWCLIPBOARD
114                ' clipboard content changed
115                Me.DrawContent()
116                ' pass the message to the next viewer
117                Win32.SendMessage(hWndNextViewer, msg, wParam, lParam)
118
119        End Select
120
121        Return IntPtr.Zero
122    End Function
123
124#End Region
125
126#Region "Control event handlers"
127
128    Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
129        ' switching between start/stop viewing state
130        If Not isViewing Then
131            Me.InitCBViewer()
132            btnSwitch.Content = "Stop viewer"
133
134        Else
135            Me.CloseCBViewer()
136            btnSwitch.Content = "Start viewer"
137        End If
138    End Sub
139
140    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
141        Me.Close()
142    End Sub
143
144    Private Sub Window_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs)
145        Me.CloseCBViewer()
146    End Sub
147
148#End Region
149
150End Class