How to get a clipboard paste notification and provide my own data?

前端 未结 2 892
暗喜
暗喜 2021-02-03 12:34

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 in

相关标签:
2条回答
  • 2021-02-03 12:49

    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

    0 讨论(0)
  • 2021-02-03 13:00

    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.

    0 讨论(0)
提交回复
热议问题