Ignoring Clipboard Updates with the CF_CLIPBOARD_VIEWER_IGNORE Clipboard Format
Jul 12th, 2007 by Chris Thornton
Copyright 1999-2003 Thornsoft Development, Inc.
Introduction
There are times when it is necessary or desirable to inform clipboard viewers (any application that is monitoring clipboard updates) that you do NOT want it to capture the clipboard update. Such a time is when ClipMate’s PowerPaste function is active, and ClipMate wants to tell other applications to ignore the new data arriving on the clipboard. Since this requires cooperation between ClipMate and the other clipboard viewers, we decided to establish a standardized way of handling the interaction. It can be used in reverse – to tell ClipMate not to capture data, as well. This method is being provided in an attempt to make all programs work better together, even competitors.
Methodology
When a clipboard update occurs, all clipboard viewers are notified via the WM_DrawClipboard message. Upon receiving the message, look for the presence of the CF_CLIPBOARD_VIEWER_IGNORE data format. If it is present – do not grab the data. Do not even open the clipboard (you can check without opening it). Simply pass the message down the chain as you normally would. If it is appropriate to change the state of your program to indicate “update ignored”, do so. (In ClipMate, we have a special kind of beep, and we gray out the area where we show the current clipboard contents.)
If you are on the other end, and need other clipboard viewers to ignore YOUR update, then simply add a CF_CLIPBOARD_VIEWER_IGNORE data format to the clipboard, right along with the other data type(s). For example, if you normally placed CF_TEXT and CF_Bitmap on the clipboard, you’d also have CF_CLIPBOARD_VIEWER_IGNORE. The data itself should contain a short string identifying your app.
Specification
Format Name: “Clipboard Viewer Ignore”
Clipboard Format Type: Private (must be registered by your program at startup)
Contains: NULL Data. Optinally, you can include text string up to 80 bytes, identifying originating program. Zero terminated. But since nothing should ever look at it, you should be able to leave it null.
Implementation
During start-up, register the CF_CLIPBOARD_VIEWER_IGNORE format, so that you can use it later on during clipboard calls. The spelling of the name is important. Since don’t all work from a common header file, we all need to call it the same thing: “Clipboard Viewer Ignore”
In Delphi, it looks like this:
CF_CLIPBOARD_VIEWER_IGNORE := RegisterClipboardFormat(‘Clipboard Viewer Ignore’);
When you receive a clipboard update via the WM_DrawClipboard message, see if the Clipboard Viewer Ignore format is present on the clipboard. If it is, don’t paste it.
/* Without opening the clipboard, see if the ignore format is present*/
if (Clipboard.HasFormat(CF_CLIPBOARD_VIEWER_IGNORE))
Then Begin
/* Don’t paste it */
End
Else Begin
/* Paste it, and process normally */
End;
/* Now send the message along to the next viewer, as you always do. */
If, on the other hand, you need to tell other clipboard viewers to ignore YOUR data (such as if you don’t want data going into ClipMate), then you need to put the CF_CLIPBOARD_VIEWER_IGNORE data format on the clipboard, along with your regular data. Be sure to do this in the SAME open/clear/update/close operation.
Delphi example using WinAPI
IF OpenClipboard(hWindow) THEN
BEGIN
EmptyClipboard;
/* Copy the ignore format, plus any of your own */
SetClipboardData(CF_CLIPBOARD_VIEWER_IGNORE,0);
/* Here is yours – allocate as usual */
SetClipboardData(CF_TEXT,hTextHandle);
/* And any more formats that you have…. */
CloseClipboard;
END;
DEMO/Sample Code
There is a demo application called the Clipboard Viewer / Ignore Demo, which handles both sides of this situation. It can send text to the clipboard, both with and without the CF_CLIPBOARD_VIEWER_IGNORE format, so that you can test your apps. It also is a Viewer, which implements the “ignoring”, so that you can see the behavior. It logs clipboard messages as well, so that you can troubleshoot certain situations, such as when applications send multiple updates in a row. Finally, it’s a darn good example of a clipboard viewer, so you can base clipboard viewer designs from it. There is a complete Delphi4 project, and the main program – IgnoreDemoForm.PAS is suitable as pseudo-code for VB and C++ programmers. And there is a compiled executable, if you just want to play. Get the demo here: http://www.thornsoft.com/dist/techsupport/ignoredemo.zip
The demo app has two sections on the form – the top half is for sending the ignore format, plus some text, to the clipboard. The bottom half is a clipboard viewer, so that you can see how to properly deal with the ignore format. You can, of course, send data from the top portion, to the clipboard, and then see the result in the bottom portion.
DEMO – Clipboard Monitoring
To use as an example of a viewer, just click the Monitor Clipboard button, and it starts monitoring. By default, it uses the “ignore” format, but you can de-select that option. While it monitors, all events are logged, even if they’re “ignored”. There is a handy counter, showing the number of events received while monitoring. If you see it go up by more than 1 for each copy, then some application is double-copying, or doing the clear and updates in different steps. That’s BAD – you should always do it like this: open/clear/update/close. Multiple open..close sequences send multiple updates.
DEMO – Sending Data With The IGNORE Instruction
In the top half of the demo, you can enter some text, press the COPY button, and it goes to the clipboard. If you have the “ignore” option checked, it’ll send the CF_CLIPBOARD_VIEWER_IGNORE format along with the CF_TEXT. Otherwise, it won’t. If you have monitoring enabled in the bottom half, you can see the results.
Reference
- Microsoft Platform SDK – Base Services | Interprocess Communication | Clipboard | Clipboard References | Clipboard Messages
- Microsoft Platform SDK – Base Services | Interprocess Communication | Clipboard | Using The Clipboard | Creating A Clipboard Viewer Window
The above two topics, while available in the MSDN, are currently online at:
In case they’ve moved it, do a search on: “Clipboard Viewer Windows”
Licensing
Just use it. We don’t want to get paid, or restrict anyone from using it. But please document the original source of this information as http://www.thornsoft.com/developer.htm
If Microsoft wants to incorporate this into the grand scheme of things, all the better.
Nice article, I’m noticing with alot of microsoft office applications, when you copy test from within the app (e.g copy “HI” from within a word document) then you close the word document the action of closing the document triggers a clipboard event, is there a way to ignore it in that case because this is anoying when developing an application.
Hi Mina,
Sorry, there isn’t a way to ignore that, within a program. The best you can do is check for duplicate data and reject. That’s what ClipMate does, if you dis-allow duplicates in the collection properties.
Office apps routinely update the clipboard with a “parting shot” when they close. This is usually because they “scrub” the clipboard to remove data formats such as OLE, that will be invalid when the program isn’t there anymore.
ahh okay, thank you for that comment, i guess i have no choice here, the only issue is the program ive written, sometimes user want the app to process duplicate data…
Good Day
Just wanted to share my new experience.
If your system denies to run due to an error corresponding to lost HAL.DLL, invalid Boot.ini or any other critical system boot files you can repair this by using the XP installation CD. Just boot from your XP Setup CD and enter the Recovery Console. Then launch “attrib -H -R -S” on the C:\Boot.ini file and delete it. Run “Bootcfg /Rebuild” and then Fixboot
Regards,
Carl
AceText also supports the Clipboard Viewer Ignore data format.
Good News – Password Safe now supports this:
http://sourceforge.net/forum/forum.php?thread_id=2387878&forum_id=134800
—
Chris
Good News! 🙂
All Comfort Software products support the Clipboard Viewer Ignore data format.
http://www.comfort-software.com/
In the Help file for Yankee Clipper III, it says:
“Acknowledgements
Yankee Clipper honors the “Clipboard Viewer Ignore” message first implemented by Thornsoft, per http://www.thornsoft.com/developer.htm. This was done simply because it’s a good idea and they were kind enough to make the standard available. ”
I have been using Yankee Clipper daily for 12 years without updating, no joke, and today I sought an upgrade, and found this in the help file. Thank you for this size innovation and that fact it’s spread to my favorite Clipboard application.
http://www.intelexual.com/products/YC3/
Hello,
this might be a stupid question regarding the spec but i have to ask.
Do you have to absolutely register the private clipboard format CF_CLIPBOARD_VIEWER_IGNORE even if you don’t intent on putting this format on the clipboard but just checking for other programs data?
Because i haven’t and i am getting an access violation on windows 7 when checking for this
ole32.dll!CopyFormatEtcDataArray(FORMATETCDATAARRAY * pFormatEtcDataArray=0x17ab1000, FORMATETCDATAARRAY * pClipFormatEtcDataArray=0x0c548f88, unsigned int stSize=0, int bCheckAvailable=1) Line 4689 + 0x9 bytes C++
Very good idea! The Text Expander http://www.phraseexpress.com will also include support for “CF_CLIPBOARD_VIEWER_IGNORE”.