views:

503

answers:

2

For a small utility I am writing (.NET, C#), I want to monitor clipboard copy operations and clipboard paste operations.

My idea is to provide my own data when pasting into an arbitrary application.

The monitoring of a copy operation can be easily done by using a clipboard viewer.

Something that seems much more advanced to me is to write a "clipboard paste provider":

  • Answer to "what formats are available" queries of applications.
  • Supply data to application paste operations.

I found this posting and this posting, but none of them seems to really help me.

What I guess is that I somehow have to mimic/hijack the current clipboard.

Question:

Is it possible to "wrap" the clipboard in terms of paste operations and provide my own kind of "clipboard proxy"?

Thanks
Uwe

+1  A: 

You need to hook in the clipboard hook by using a windows hook. A windows hook is a way of intercepting global events happening and providing your own hook procedure to replace or intercept the message. Have a look here on CodeProject that explains how to hook. Here's a clipboard helper that listens for the copy/paste functionality. Here's a Clipboard spy that just does that. Here's another article that implements a Clipboard hook.

Hope this helps, Best regards, Tom.

tommieb75
Thanks, Tom! The best article reference is the one who hooks "Ctrl+V". But: What if the user right-clicks and selects "Paste" from a context menu or some complete other way? I guess the solution would be _not_ to hook on keyboard/mouse/whatever but to intercept the direct clipboard communication itself.
Uwe Keim
A: 

Look into "delayed rendering" in the WinAPI. With this technique, you load the clipboard with null handles, and upon pasting, windows notifies you with a WM_RENDERFORMAT message. This is how apps like Excel can get away with "copying" 25 different formats. It doesn't really copy them all. It'll actually produce some common ones like TEXT, but "advertises" the others like Bitmap, Html, WKS, etc., opting to wait to see what the target application wants to paste.
Consider this: you can select 5000 cells in Excel and copy, and the clipboard is updated pretty quickly. Now paste into Windows Paint, and suddenly your system crawls as Excel tries to render a huge bitmap. Older versions would usually crash, after using all available memory and eating the pagefile. This was back in the Windows 3.1 days though. Modern versions give a message about "bitmap too large" or "not enough memory". Warning: Delayed Rendering will be prematurely triggered by apps that monitor the clipboard and auto-paste data into themselves, such as Remote Desktop, VMWare, Office Clipboard, and my own ClipMate. Some clipboard monitoring programs can be told to ignore the clipboard update by using the CF_Clipboard_Viewer_Ignore flag, which I've documented here: link text

Chris Thornton