Adam Robinson is on the right track, but not quite right. I will try to present a โlongโ version (as opposed to its โshortโ version) and explain how and why I think that the solution that he offers does not reach your final goal.
As documentation, he refers to explanations; there are three different ways to monitor changes in the Windows clipboard, each of which has its own reservations:
Creating a clipboard viewer window that is hooked into the clipboard viewer chain and receives notifications when the contents of the clipboard have been modified by the user. (Available on all versions of Windows, but it is usually more difficult to code and thereby discourage new applications that do not have a specific need for its features.)
A request for the clipboard sequence number, which is a 32-bit value that changes every time the contents of the clipboard change. Your program calls the GetClipboardSequenceNumber Windows API GetClipboardSequenceNumber once and caches its value, and then each time you want to check if the contents of the clipboard have changed, you call the same function again and compare its return value with the value you cached. There are two important caveats here:
This feature is available only in Windows 2000 and later. This is unlikely to be a problem if you are writing .NET applications, since Framework versions have already lost W2K support in 3.0.
This is not a notification method, and you should not call this function repeatedly in the polling cycle. This means that you need to manually call the corresponding function and compare the serial number of the clipboard. You cannot use this method if you want to โlistenโ and be notified immediately when the contents of the clipboard change, as you describe in your question. The documentation here is very clear:
This method is more suitable for programs whose caching results are based on the current contents of the clipboard, and you need to know if all of them are still valid before using the results from this cache. Please note that this is not a notification method and should not be used in the polling cycle. To be notified when clipboard contents change, use the clipboard format listener or the clipboard viewer.
Create a clipboard format listener that registers for notification when the contents of the clipboard change. This is the ideal solution in your case, since it avoids the difficulties of creating a clipboard viewer window (option 1), but also allows you to listen and receive notifications every time the contents of the clipboard change (unlike option 2).
The problem is that this is only available on Windows Vista and later. If you still need to focus on Windows XP (like most of us), this is really not an option for you.
Therefore, from the example that you provide in the question, it sounds to me as the only option available to you is option 1, which creates a clipboard viewer window. The documentation contains detailed information on how you configured this using the SetClipboardViewer function and listening to the WM_DRAWCLIPBOARD and WM_CHANGECBCHAIN messages. Performing this task correctly may be a difficult task for you, but, fortunately for .NET developers, others have already done the hard work for us. ("Others," I say, despite the fact that he was one of those who himself was.)
This CodeProject article is a good example. It implements three different types of hooks: a mouse hook, a keyboard hook, and a clipboard hook. The only thing that interests you is the capture of the clipboard, but you can simply add a link to the DLL in your project to immediately start using its functions.
If you are interested in the internal principles of how this works, and you want to try to code it yourself, this article seems like a fantastic description of the specific steps.
source share