forked from mlocati/MediaData
/
ClipboardMonitor.cs
162 lines (129 loc) · 4.62 KB
/
ClipboardMonitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MLocati.MediaData
{
public class ClipboardMonitor : IDisposable
{
#region Constants
private const int WM_DRAWCLIPBOARD = 0x00000308;
private const int WM_CHANGECBCHAIN = 0x0000030D;
#endregion
#region Types
private class MyControl : Control
{
private ClipboardMonitor _monitor;
public MyControl(ClipboardMonitor monitor)
{
this._monitor = monitor;
}
protected override void WndProc(ref Message m)
{
if (this._monitor == null || this._monitor.HandleWindowProc(ref m))
{
base.WndProc(ref m);
}
}
}
#endregion
#region Instance properties
public event EventHandler ClipboardChanged;
private MyControl _control = null;
private Form _listener = null;
private IntPtr? _nextClipboardViewer = null;
#endregion
#region Constructors
public ClipboardMonitor(Form listener)
{
if (listener == null)
{
throw new ArgumentNullException("listener");
}
this._listener = listener;
this._listener.Closed += this.ListenerClosed;
this._control = new MyControl(this);
this._nextClipboardViewer = ClipboardMonitor.SetClipboardViewer(this._control.Handle);
}
#endregion
#region Instace methods
private bool HandleWindowProc(ref Message m)
{
switch (m.Msg)
{
case ClipboardMonitor.WM_DRAWCLIPBOARD:
if (this._listener != null && this.ClipboardChanged != null)
{
this._listener.BeginInvoke(this.ClipboardChanged, new object[] { this, new EventArgs() });
}
if (this._nextClipboardViewer.HasValue)
{
ClipboardMonitor.SendMessage(this._nextClipboardViewer.Value, m.Msg, m.WParam, m.LParam);
}
return true;
case ClipboardMonitor.WM_CHANGECBCHAIN:
if (this._nextClipboardViewer.HasValue)
{
if (m.WParam == this._nextClipboardViewer.Value)
{
this._nextClipboardViewer = m.LParam;
}
else
{
ClipboardMonitor.SendMessage(this._nextClipboardViewer.Value, m.Msg, m.WParam, m.LParam);
}
}
return false;
default:
return true;
}
}
private void ListenerClosed(object sender, EventArgs e)
{
this.Dispose();
}
public void Dispose()
{
if (this._nextClipboardViewer.HasValue)
{
try
{
if (this._control != null)
{
ClipboardMonitor.ChangeClipboardChain(this._control.Handle, this._nextClipboardViewer.Value);
}
}
catch
{ }
this._nextClipboardViewer = null;
}
if (this._control != null)
{
try
{ this._control.Dispose(); }
catch
{ }
this._control = null;
}
if (this._listener != null)
{
try
{
this._listener.Closed -= this.ListenerClosed;
}
catch
{ }
this._listener = null;
}
}
#endregion
#region Static methods
[DllImport("User32.dll")]
private static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
#endregion
}
}