Tag Archives: RemoveClipboardFormatListener
Monitor for clipboard changes using AddClipboardFormatListener
Microsoft has added a new windows function to help monitor when the data in the clipboard has been changed. The new function is called AddClipboardFormatListener but sadly it is only available for Windows Vista and higher. If you are looking for a method that will work for earlier versions of Windows take a look at Monitor clipboard in C#.
The principle is the same with the older method. We need 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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /// <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; |
Posted in C#.
Tagged AddClipboardFormatListener, C#, csharp, RemoveClipboardFormatListener, snippet, winforms, WM_CLIPBOARDUPDATE