PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/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. Class MainWindow
  18. #Region "Private fields"
  19. ''' <summary>
  20. ''' Next clipboard viewer window
  21. ''' </summary>
  22. Private hWndNextViewer As IntPtr
  23. ''' <summary>
  24. ''' The <see cref="HwndSource"/> for this window.
  25. ''' </summary>
  26. Private hWndSource As HwndSource
  27. Private isViewing As Boolean
  28. #End Region
  29. #Region "Clipboard viewer related methods"
  30. Private Sub InitCBViewer()
  31. Dim wih As New WindowInteropHelper(Me)
  32. hWndSource = HwndSource.FromHwnd(wih.Handle)
  33. hWndSource.AddHook(AddressOf WinProc) ' start processing window messages
  34. hWndNextViewer = Win32.SetClipboardViewer(hWndSource.Handle) ' set this window as a viewer
  35. isViewing = True
  36. End Sub
  37. Private Sub CloseCBViewer()
  38. ' remove this window from the clipboard viewer chain
  39. Win32.ChangeClipboardChain(hWndSource.Handle, hWndNextViewer)
  40. hWndNextViewer = IntPtr.Zero
  41. hWndSource.RemoveHook(AddressOf WinProc)
  42. pnlContent.Children.Clear()
  43. isViewing = False
  44. End Sub
  45. Private Sub DrawContent()
  46. pnlContent.Children.Clear()
  47. If Clipboard.ContainsText() Then
  48. ' we have some text in the clipboard
  49. Dim tb As New TextBox
  50. tb.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
  51. tb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
  52. tb.Text = Clipboard.GetText()
  53. tb.IsReadOnly = True
  54. tb.TextWrapping = TextWrapping.NoWrap
  55. pnlContent.Children.Add(tb)
  56. ElseIf Clipboard.ContainsFileDropList() Then
  57. ' we have a file drop list in the clipboard
  58. Dim lb As New ListBox
  59. lb.ItemsSource = Clipboard.GetFileDropList()
  60. pnlContent.Children.Add(lb)
  61. ElseIf Clipboard.ContainsImage() Then
  62. ' Because of a known issue in WPF,
  63. ' we have to use a workaround to get correct
  64. ' image that can be displayed.
  65. ' The image have to be saved to a stream and then
  66. ' read out to workaround the issue.
  67. Dim ms As New MemoryStream
  68. Dim enc As New BmpBitmapEncoder
  69. enc.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()))
  70. enc.Save(ms)
  71. ms.Seek(0, SeekOrigin.Begin)
  72. Dim dec As New BmpBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
  73. Dim img As New Image
  74. img.Stretch = Stretch.Uniform
  75. img.Source = dec.Frames(0)
  76. pnlContent.Children.Add(img)
  77. Else
  78. Dim lb As New Label
  79. lb.Content = "The type of the data in the clipboard is not supported by this sample."
  80. pnlContent.Children.Add(lb)
  81. End If
  82. End Sub
  83. 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
  84. Select Case msg
  85. Case Win32.WM_CHANGECBCHAIN
  86. If wParam = hWndNextViewer Then
  87. ' clipboard viewer chain changed, need to fix it.
  88. hWndNextViewer = lParam
  89. ElseIf hWndNextViewer <> IntPtr.Zero Then
  90. ' pass the message to the next viewer
  91. Win32.SendMessage(hWndNextViewer, msg, wParam, lParam)
  92. End If
  93. Case Win32.WM_DRAWCLIPBOARD
  94. ' clipboard content changed
  95. Me.DrawContent()
  96. ' pass the message to the next viewer
  97. Win32.SendMessage(hWndNextViewer, msg, wParam, lParam)
  98. End Select
  99. Return IntPtr.Zero
  100. End Function
  101. #End Region
  102. #Region "Control event handlers"
  103. Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  104. ' switching between start/stop viewing state
  105. If Not isViewing Then
  106. Me.InitCBViewer()
  107. btnSwitch.Content = "Stop viewer"
  108. Else
  109. Me.CloseCBViewer()
  110. btnSwitch.Content = "Start viewer"
  111. End If
  112. End Sub
  113. Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  114. Me.Close()
  115. End Sub
  116. Private Sub Window_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs)
  117. Me.CloseCBViewer()
  118. End Sub
  119. #End Region
  120. End Class