Joining the Clipboard Chain Best practices

2019-05-14 01:41发布

问题:

Further to my post on custom format clipboard, I am considering the possibility of writing my own custom clipboad monitoring component.

Prior to the statement:

ClipboardWindow:=SetClipboardViewer(Form1.Handle);

I have seen in a sample code I studied the following snippet:

OpenClipboard(Form1.Handle); 
EmptyClipboard; 
CloseClipboard;

whereas others don't include a cleaning code at all. I am confused.

I believe Clipbrd.TClipboard.Clear just does the same the VCL way.

My question is:

When clearing the clipboard before joining the clipboard chain is mandatory ?

回答1:

No, there is no need to clear the clipboard. Indeed, you shouldn't. Other clipboard monitors will needlessly react to the update, and the user may want to paste that thing that you just destroyed. Additionally, there is a lot more to clipboard chain monitoring than just adding yourself to the chain. You must pass the events along to the next window (result handle from SetClipboardViewer), and you must, without failure, remove yourself from the chain when your app exits. Also, you need to avoid blocking the clipboard unnecessarily. Typically, this means waiting to register for the clipboard events until you're ready to actually process events. For example, don't make it the first thing in your startup, if you're going to subsequently open a dialog asking the user where he wants to store the data, if he has a license key, etc..

I have tips, as well as common pitfalls here: http://www.clipboardextender.com/developing-clipboard-aware-programs-for-windows/6



回答2:

  1. The rule is as simple as possible: if you want to delete the clipboard content (so other apps can't use it) delete it. if not, keep it.

  2. You don't know if your use wants to keep the data OR You want to implement something fancy? Do you know those applications (Paint Shop Pro is one of them) that are asking: "You left a large image (10MB of data) in clipboard. Do you want to keep it or clear it?" You could do something similar. :)