public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddClipboardFormatListener(this.Handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLIPBOARDUPDATE)
{
Console.WriteLine("剪贴板内容改变");
}
base.WndProc(ref m);
}
//---------------------
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);
}
Form Close事件中再调用RemoveClipboardFormatListener:
RemoveClipboardFormatListener(this.Handle);