C#-WinForm剪切板监控各类数据

看代码就行,下面是一个简单说实例

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
public partial class Form1 : Form
{

//一些dll的调用
public const int WM_CLIPBOARDUPDATE = 0x031D;
[DllImport("user32.dll", SetLastError = true)]
public static extern bool AddClipboardFormatListener(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RemoveClipboardFormatListener(IntPtr hWnd);


public Form1()
{
InitializeComponent();

//加上这一行
AddClipboardFormatListener(this.Handle);
//RemoveClipboardFormatListener(this.Handle); 销毁
Message mes = new Message();
DefWndProc(ref mes);
}

//监听处理事件
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_CLIPBOARDUPDATE)
{
if (Clipboard.ContainsText())
{
//输出剪切板中的文本
Console.WriteLine(Clipboard.GetText());
}
//显示剪贴板中的图片信息
else if (Clipboard.ContainsImage())
{

Console.WriteLine("图片");
//Clipboard.GetImage();
//赋值给imgbox 我这里没有
// pictureBox1.Update();
}
else
{

Console.WriteLine("其他待处理的文件类型");
}
}
else
{
base.DefWndProc(ref m);
}
}
}