Guest User

Untitled

a guest
Sep 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. this.DataContext = new ViewModel(System.Windows.Window wnd);
  5. }
  6.  
  7. public ViewModel(System.Windows.Window wnd)
  8. {
  9. InitCBViewer(wnd);
  10. }
  11.  
  12. private void InitCBViewer(System.Windows.Window wnd)
  13. {
  14. WindowInteropHelper wih = new WindowInteropHelper(wnd);
  15. hWndSource = HwndSource.FromHwnd(wih.Handle);
  16.  
  17. hWndSource.AddHook(this.WinProc); // start processing window messages
  18. hWndNextViewer = Win32.SetClipboardViewer(hWndSource.Handle); // set this window as a viewer
  19.  
  20. }
  21.  
  22. private void InitCBViewer()
  23. {
  24. WindowInteropHelper wih = new WindowInteropHelper(this);
  25. hWndSource = HwndSource.FromHwnd(wih.Handle);
  26.  
  27. hWndSource.AddHook(this.WinProc); // start processing window messages
  28. hWndNextViewer = Win32.SetClipboardViewer(hWndSource.Handle); // set this window as a viewer
  29.  
  30. }
  31.  
  32. private void CloseCBViewer()
  33. {
  34. // remove this window from the clipboard viewer chain
  35. Win32.ChangeClipboardChain(hWndSource.Handle, hWndNextViewer);
  36.  
  37. hWndNextViewer = IntPtr.Zero;
  38. hWndSource.RemoveHook(this.WinProc);
  39. }
  40.  
  41. private IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  42. {
  43. switch (msg)
  44. {
  45. case Win32.WM_CHANGECBCHAIN:
  46. if (wParam == hWndNextViewer)
  47. {
  48. // clipboard viewer chain changed, need to fix it.
  49. hWndNextViewer = lParam;
  50. }
  51. else if (hWndNextViewer != IntPtr.Zero)
  52. {
  53. // pass the message to the next viewer.
  54. Win32.SendMessage(hWndNextViewer, msg, wParam, lParam);
  55. }
  56. break;
  57.  
  58. case Win32.WM_DRAWCLIPBOARD:
  59. // clipboard content changed
  60. this.DisplayContent(); // calls a function to do something with the data on the clipboard
  61. // pass the message to the next viewer.
  62. Win32.SendMessage(hWndNextViewer, msg, wParam, lParam);
  63. break;
  64. }
  65.  
  66. return IntPtr.Zero;
  67. }
  68.  
  69. internal static class Win32
  70. {
  71. /// <summary>
  72. /// The WM_DRAWCLIPBOARD message notifies a clipboard viewer window that
  73. /// the content of the clipboard has changed.
  74. /// </summary>
  75. internal const int WM_DRAWCLIPBOARD = 0x0308;
  76.  
  77. /// <summary>
  78. /// A clipboard viewer window receives the WM_CHANGECBCHAIN message when
  79. /// another window is removing itself from the clipboard viewer chain.
  80. /// </summary>
  81. internal const int WM_CHANGECBCHAIN = 0x030D;
  82.  
  83. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  84. internal static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
  85.  
  86. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  87. internal static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
  88.  
  89. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  90. internal static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  91. }
Add Comment
Please, Sign In to add comment