Clipboard monitor

I have a problem. I am trying to use ClipboardMonitor for my C # application. The goal is to track every change in the clipboard. When starting the monitor:

AddClipboardFormatListener(this.Handle);

when the listener stops:

RemoveClipboardFormatListener(this.Handle);

and rewrite method:

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;
    }
}

and of course the DLL import:

[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);

But when setting a breakpoint when calling a method ClipboardMonitor_OnClipboardChangeand changing the clipboard, I never get it.

+4
source share
2 answers

The problem is that you are processing the wrong window message. Documentation citation for 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;
    }
}
+5

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 executable name.
    Debug.WriteLine(e.SourceApplication.Name);
    // Gets the application window title.
    Debug.WriteLine(e.SourceApplication.Title);
    // Gets the application process ID.
    Debug.WriteLine(e.SourceApplication.ID.ToString());
    // Gets the application executable path.
    Debug.WriteLine(e.SourceApplication.Path);
}

, MonitorChanged , , .

, , , Windows, .

SharpClipboard, , .NET.

0

All Articles