Tag Archives: WM_DRAWCLIPBOARD
Monitor clipboard in C#
The best way of monitoring the data in the clipboard through your application is by adding your application’s window to the chain of clipboard viewers so it can receive the WM_DRAWCLIPBOARD
message when the clipboard data is modified.
In order to do that we need to make use of the Windows SetClipboardViewer
and ChangeClipboardChain
APIs in order to monitor when the WM_DRAWCLIPBOARD
message triggers.
First we will need to pinvoke SetClipboardViewer
and ChangeClipboardChain
and WM_DRAWCLIPBOARD
and also set the variables that will hold the value for the next window in the chain that is required when we will want to stop our window from monitoring the clipboard. Place the following in your class.
1 2 3 4 5 6 7 8 | [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer); [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext); private const int WM_DRAWCLIPBOARD = 0x0308; // WM_DRAWCLIPBOARD message private IntPtr _clipboardViewerNext; // Our variable that will hold the value to identify the next window in the clipboard viewer chain |
Posted in C#.
Tagged C#, ChangeClipboardChain, csharp, SetClipboardViewer, snippet, tutorial, winforms, WM_DRAWCLIPBOARD