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 »

Visual Studio 2013

Visual Studio 2013

Visual Studio 2013 is now available. Make sure to upgrade to the latest version of Visual Studio. 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 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.


Add your window to the chain of clipboard viewers so we can monitor the clipboard. Place the following either in your form’s constructor or in any of the on load events.

Next will be our WndProc method override that will monitor the window messages. Note that there are a few more DataFormats that can be used, so feel free to experiment.

And finally it’s cleaning up time. Make sure to remove your window from the clipboard viewers chain before closing the form.

Note: There is a new Windows function now that is the recommended one if you want to target machines that are running Windows Vista and up. For more information take a look at Monitor for clipboard changes using AddClipboardFormatListener.

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 clipboard in C#

  1. Nmelss says:

    So if i want to use this in a console application “this.handle” isn’t available, and base.WndPrc isnt either. Do you know of a solution ?

  2. Riffer says:

    This is really a great solution and has helped me a lot with a program that has to capture data from the clipboard by sending ctrl+c to other applications.

    Thank you very much!

Leave a Reply

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