The Unofficial Newsletter of Delphi Users - by Robert
Vivrette
Cut, Copy, & Paste... Improved!
by Lance Leonard, lleonard@alldev.com
In Issue #17 of UNDU, Brad Evans suggested the following for Cut, Copy, and Paste:
SendMessage(ActiveControl.Handle, WM_CUT, 0, 0);
He also noted that you need to change this slightly for MDI Applications. Actually, there is a simpler way that you don't have to worry about at all:
PostMessage(getFocus(), WM_COPY, 0, 0 );
This approach offers two advantages:
1. It doesn't matter what kind of form (or window, for that matter) is active. GetFocus is an API call that returns the handle of the active child control. So, it will work even if you have an OLE/ActiveX child control embedded on your form.
2. Since we're not concerned with the return value, PostMessage is a somewhat better solution because it places the Clipboard request at the bottom of the controls current event queue. If the control just happened to be in the middle of something when your user triggered the Clipboard command, PostMessage makes sure that task is complete.
SendMessage does have an advantage over PostMessage. Its result indicates whether or not the control accepted the message. Thus, you could extend the code to provide the user with feedback if the message wasn't accepted by the control.
There is an additional reason I'd like to add to Brad's thoughts about the advantages of such an approach. If the control supports selection, such as a TEdit or TMemo, you don't need to change the code in order to affect just the selected text.
Hope this helps...