How do I monitor clipboard content changes in C#?

.net, c#, clipboard

Solution

You could use SetClipboardViewer provided by Win32 API (through P/Invoke).

Here is a page which contains code to set one up in C#: http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c7315/

Problem

I want to have this feature in my C# program: When the user do Ctrl + C or Copy anywhere (i.e. when the clipboard content changes), my program will get notified, and check whether the content met certain criteria, if so, become the active program, and process the content, etc. I can get the contents out from `System.Windows.Forms.Clipboard`, however, I don't know how to monitor the content changes from the clipboard. If using Windows Vista or later, use `AddClipboardFormatListener` as in John Knoeller's answer, for Windows XP, I have to use the older, more fragile `SetClipboardViewer` API, as in the accepted answer.

Original source

Related problems