Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipboard Monitor

Tags:

c#

clipboard

I have a problem. I am trying to use the ClipboardMonitor for my C# Application. The goal is to monitor every change in the clipboard. To start monitoring:

AddClipboardFormatListener(this.Handle);

To stop the listener:

RemoveClipboardFormatListener(this.Handle);

And the override WndProc() 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 putting a breakpoint at the method call ClipboardMonitor_OnClipboardChange and changing the clipboard, I never get the method called.

How can I change my code so that I receive a WM_ message notifying me that the clipboard has changed?

like image 355
a_hamm99 Avatar asked Jul 01 '16 14:07

a_hamm99


People also ask

What is clipboard monitoring?

An essential computer monitoring feature for Windows is the cability to view what has been copied by the user. This means that any time a user copies, pastes, or cuts anything, you'll be able to see that data. This is because Keyturion tracks any operations that are connected to the system's clipboard.

Where do I find my clipboard?

Open the messaging app on your Android, and press the + symbol to the left of the text field. Select the keyboard icon. When the keyboard appears, select the > symbol at the top. Here, you can tap the clipboard icon to open the Android clipboard.

How do I view the clipboard in Windows 10?

Open Windows 10 Clipboard Press Win key+V. The Clipboard history panel appears with each item you cut or copied, starting with the last one.

How do you open clipboard task pane?

To open the Clipboard task pane, click Home, and then click the Clipboard dialog box launcher. Double-click the image or text you want to paste. Note: To open the Clipboard task pane in Outlook, in an open message, click the Message tab, and then click the Clipboard dialog box launcher in the Clipboard group.


2 Answers

The problem is that you're handling the wrong window message. Quoting the documentation for AddClipboardFormatListener:

When a window has been added to the clipboard format listener list, it is posted a WM_CLIPBOARDUPDATE message whenever the contents of the clipboard have changed.

With that knowledge, change the code to:

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;
    }
}
like image 80
theB Avatar answered Sep 22 '22 10:09

theB


SharpClipboard as a library could be of more benefit as it encapsulates the same features into one fine component library. You can then access its ClipboardChanged event and detect various data-formats when they're cut/copied.

You can choose the various data-formats you want to monitor:

var clipboard = new SharpClipboard();

clipboard.ObservableFormats.Texts = true;
clipboard.ObservableFormats.Files = true;
clipboard.ObservableFormats.Images = true;
clipboard.ObservableFormats.Others = true;

Here's an example using its ClipboardChanged event:

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

You can also find out the application that the cut/copy event occurred on together with its details:

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

There are also other events such as the MonitorChanged event which listens whenever clipboard-monitoring is disabled, meaning that you can enable or disable monitoring the clipboard at runtime.

In addition to all this, since it's a component, you can use it in Designer View by dragging-and-dropping it to a Windows Form, making it super easy for anyone to customize its options and work with its inbuilt events.

SharpClipboard seems to be the very best option for clipboard-monitoring scenarios in .NET.

like image 42
Willy Kimura Avatar answered Sep 18 '22 10:09

Willy Kimura