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.