web analytics

How to Monitor Clipboard Changes in C#?

Options

codeling 1599 - 6654
@2016-02-09 23:39:29

Since Windows Vista and higher, Microsoft has added a new windows function called AddClipboardFormatListener to help monitor when the data in the clipboard has been changed. The principle is to to add our window to the clipboard format listener list so it can receive the WM_CLIPBOARDUPDATE message.

In order to do that we need to pinvoke the AddClipboardFormatListener and RemoveClipboardFormatListener.

/// <summary>
/// Places the given window in the system-maintained clipboard format listener list.
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AddClipboardFormatListener(IntPtr hwnd);
 
/// <summary>
/// Removes the given window from the system-maintained clipboard format listener list.
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
 
/// <summary>
/// Sent when the contents of the clipboard have changed.
/// </summary>
private const int WM_CLIPBOARDUPDATE = 0x031D;

Then we need to add our window to the clipboard format listener list by calling the AddClipboardFormatListener method with our window’s handle as a parameter. Place the following code in your main window form constructor or any of its load events.

// Add our window to the clipboard's format listener list.

AddClipboardFormatListener(this.Handle);   

Override the WndProc method so we can catch when the WM_CLIPBOARDUPDATE is send.

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
 
    if (m.Msg == WM_CLIPBOARDUPDATE)
    {
        IDataObject iData = Clipboard.GetDataObject();      // Clipboard's data.
 
        /* Depending on the clipboard's current data format we can process the data differently.
         * Feel free to add more checks if you want to process more formats. */
        if (iData.GetDataPresent(DataFormats.Text))
        {
            string text = (string)iData.GetData(DataFormats.Text);
            // do something with it
        }
        else if (iData.GetDataPresent(DataFormats.Bitmap))
        {
            Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap);
            // do something with it
        }
    }
}

And finally make sure to remove your main window from the clipboard format listener list before closing your form.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

    // Remove our window from the clipboard's format listener list.
    RemoveClipboardFormatListener(this.Handle);    
}

@2016-02-09 23:45:41

AddClipboardFormatListener function

Places the given window in the system-maintained clipboard format listener list.

BOOL WINAPI AddClipboardFormatListener( _In_ HWND hwnd );

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.

Minimum supported client

Windows Vista [desktop apps only]

Minimum supported server

Windows Server 2008 [desktop apps only]

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com