How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

Monitor for clipboard changes using AddClipboardFormatListener

CSharp Purple

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.

Then we need to add our window to the clipboard format listener list by calling the AddClipboardFormatListener method with our window’s handle as a parameter. Place the following code in your main window form constructor or any of its load events.

Override the WndProc method so we can catch when the WM_CLIPBOARDUPDATE is send.

And finally make sure to remove your main window from the clipboard format listener list before closing your form.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

4 Responses to Monitor for clipboard changes using AddClipboardFormatListener

  1. Marco says:

    It would be possible to also have the code version for C++ ?

    Thanks in advance

  2. Josemimos says:

    Thanks, but I’m using Unicode and WinForm ……….

    My changes:

    And:

    • CooLMinE says:

      Thanks for sharing your solution to the problem.

      If DataFormats.StringFormat can contain both Unicode and ASCII strings then it is a better solution than using DataFormats.Text, depending on your needs of course.

      Alternatively, if you have two different logics and want to handle them separately, you can use DataFormats.UnicodeText for Unicode.

  3. Damian says:

    It works perfect. Thanks and greetings from Poland :)

Leave a Reply

Your email address will not be published. Required fields are marked *