剪贴板监视器

时间:2016-07-01 14:51:53

标签: c# clipboard

我有问题。我正在尝试将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处设置断点并更改剪贴板时,我从未接触到它。

2 个答案:

答案 0 :(得分:5)

问题是您正在处理错误的窗口消息。引用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;
    }
}

答案 1 :(得分:1)

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窗体中,在 Designer View 中使用它,这使任何人都可以非常轻松地自定义其选项并处理其内置事件。

SharpClipboard似乎是.NET中剪贴板监视方案的最佳选择。

相关问题