TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Easy Clipboard Support
 
   
 Posted: 14 August 2000
 
   
Discovered by Maggie Owens 
 Applies to: All versions
 
       
   

Question: How can I quickly add good Clipboard support to my application?

Answer: Use Windows messages to take advantage of the Clipboard support already built into the components you're using. For example, the following code shows simple implementations for the standard Cut, Copy, and Paste operations:

   procedure ClipCopy;
   begin
      postMessage( getFocus, WM_COPY, 0, 0 );
   end; { clipCopy }

   procedure ClipCut;
   begin
      postMessage( getFocus, WM_CUT, 0, 0 );
   end; { clipCut }

   procedure ClipPaste;
   begin
      postMessage( getFocus, WM_PASTE, 0, 0 );
   end; { clipPaste }

How it works

You're probably aware that most of Windows' activities are triggered by messages. This emulates that process by generating the message associated with these operations. When the application receives one of these messages, it passes it to the active control. Most VCL components contain default Clipboard support and this can be overridden by a component developer to provide enhanced support or capabilities.

Please note that we've used postMessage() to place the message at the bottom of the application's message queue. This conveniently avoids any problems that might arise if another process is taking place when the user triggers the event. For example, the data may be being refreshed. While not a huge problem, there are times when it's dangerous to start a new process while another is running.

Messages let you take advantage of that built-in support, regardless of the component. By taking this approach, you take advantage of any special features implemented by the component developer.

We like to call this "drop-and-forget" code because it just works.

Please note that there is a downside to this implementation; there's no way to know whether or not the active control supports the Clipboard using postMessage(). If this is important to you, then use sendMessage() instead. while postMessage() returns a value, it only indicates whether or not the message was accepted by the message queue itself. SendMessage() returns a value indicating whether or not the message was handled.

Sources:

  • As far as we know, the original credit for this technique goes to Maggie Owens, who discovered it while working on her excellent dbNavigator replacement. An article submitted to UNDU neglected to mention that and we apologize for the oversight. We've documented it here for our own reference and to make it available to the broadest possible audience.
  • We also found an article discussing this technique on another site.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]