我有个问题。我试图使用ClipboardMonitor我的C#应用程序。我们的目标是监视剪贴板中的每一个变化。当开始监测:
AddClipboardFormatListener(this.Handle);
停止侦听器时:
RemoveClipboardFormatListener(this.Handle);
和重写方法:
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
ClipboardMonitor_OnClipboardChange((string)iData.GetData(DataFormats.Text));
}
break;
default:
base.WndProc(ref m);
break;
}
}
当然还有DLL导入:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
但在方法调用ClipboardMonitor_OnClipboardChange
把一个断点,并改变剪贴板时,我从来没有得到它。
问题是,你正在处理错误的窗口消息。报价为AddClipboardFormatListener
的文档:
当窗口已被添加到剪贴板格式听众列表,它张贴每当剪贴板中的内容已经改变了
WM_CLIPBOARDUPDATE
消息。
有了这些知识,代码更改为:
const int WM_CLIPBOARDUPDATE = 0x031D;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_CLIPBOARDUPDATE:
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
string data = (string)iData.GetData(DataFormats.Text);
}
break;
default:
base.WndProc(ref m);
break;
}
}
SharpClipboard作为一个库可能是更多的利益,因为它封装了相同的功能,成为一个精致的组件库。然后,您可以访问其ClipboardChanged
事件,并检测各种数据格式时,他们正在剪切/复制。
你可以选择你要监控的各种数据格式:
var clipboard = new SharpClipboard();
clipboard.ObservableFormats.Texts = true;
clipboard.ObservableFormats.Files = true;
clipboard.ObservableFormats.Images = true;
clipboard.ObservableFormats.Others = true;
下面是使用它的ClipboardChanged
事件的例子:
private void ClipboardChanged(Object sender, ClipboardChangedEventArgs e)
{
// Is the content copied of text type?
if (e.ContentType == SharpClipboard.ContentTypes.Text)
{
// Get the cut/copied text.
Debug.WriteLine(clipboard.ClipboardText);
}
// Is the content copied of image type?
else if (e.ContentType == SharpClipboard.ContentTypes.Image)
{
// Get the cut/copied image.
Image img = clipboard.ClipboardImage;
}
// Is the content copied of file type?
else if (e.ContentType == SharpClipboard.ContentTypes.Files)
{
// Get the cut/copied file/files.
Debug.WriteLine(clipboard.ClipboardFiles.ToArray());
// ...or use 'ClipboardFile' to get a single copied file.
Debug.WriteLine(clipboard.ClipboardFile);
}
// If the cut/copied content is complex, use 'Other'.
else if (e.ContentType == SharpClipboard.ContentTypes.Other)
{
// Do something with 'e.Content' here...
}
}
您还可以发现,剪切/复制事件发生在与它的细节一起应用:
private void ClipboardChanged(Object sender, SharpClipboard.ClipboardChangedEventArgs e)
{
// Gets the application's executable name.
Debug.WriteLine(e.SourceApplication.Name);
// Gets the application's window title.
Debug.WriteLine(e.SourceApplication.Title);
// Gets the application's process ID.
Debug.WriteLine(e.SourceApplication.ID.ToString());
// Gets the application's executable path.
Debug.WriteLine(e.SourceApplication.Path);
}
还有一些其他的活动,如监听,每当剪贴板监视被禁用,这意味着你可以启用或禁用运行时监视剪贴板MonitorChanged
事件。
除了这一切,因为它是一个组件,您可以通过拖动和坠到一个Windows窗体,使其成为超级任何人都能轻松定制它的选择和其内置的事件工作用它,在设计器视图。
SharpClipboard似乎是在.NET剪贴板监视场景的最好的选择。