Pages

Friday, December 14, 2001

PeopleCode Clipboard Function

This function allows access to Clipboard where data can be pasted into a wide range of applications. My client uses it to output data into email to be sent off to other departments. For output that does not require extensive formatting, it's a quick & easy alternative to existing options, such as Crystal or nVision. For example, in a grid/scroll, where datacannot be seen on one screen, you can now select only columns (or other criteria) that are of interest and copy them to clipboard. This allows data to be selectively extracted/copied from a grid.
The following code declares the Clipboard function:
Declare Function OpenClipboard Library "USER32"
(long Value As number) Returns boolean;
Declare Function EmptyClipboard Library "USER32"
() Returns boolean;
Declare Function SetClipboardData Library "USER32"
(long Value As number, long Value As number) Returns long As number;
Declare Function CloseClipboard Library "USER32"
() Returns boolean;
Declare Function GlobalAlloc Library "kernel32"
(integer Value As number, long Value As number) Returns long As number;
Declare Function GlobalFree Library "kernel32"
(long Value As number) Returns long As number;
Declare Function lstrcpy Library "kernel32"
(long Value As number, string Ref As string) Returns long As number;
Declare Function GlobalLock Library "kernel32"
(long Value As number) Returns long As number;
Declare Function GlobalUnlock Library "kernel32"
(long Value As number) Returns long As number;Function djSendClip(&STR) Returns boolean;
&CLIPOK = False;
/* Clipboard format is text only */
&CF_TEXT = 1;
/* memory type is global fixed*/
&GMEM_FIXED = 0;
/* get 1 byte more than the text length */
&LEN = Len(&STR) + 1;
/* get a memory block from global heap , return global Handle*/
&H_GMEM = GlobalAlloc(&GMEM_FIXED, &LEN);
If &H_GMEM <> 0 Then
/*  lock memory address, retrun pointer to memory (same as H_GEMM for GMEM_FIXED) */
&LP_GMEM = GlobalLock(&H_GMEM);
If &LP_GMEM <> 0 Then
   /* copy string to global memory area, retrun long pointer to global string */
   &LPSTR = lstrcpy(&LP_GMEM, &STR);
   If &LPSTR <> 0 Then
      &B_RC = OpenClipboard(0);
      If &B_RC = True Then
         &B_RC = EmptyClipboard();
         If &B_RC = True Then
            &H_RC = SetClipboardData(&CF_TEXT, &LP_GMEM);
            If &H_RC <> 0 Then
               &CLIPOK = True;
            End-If;
         End-If;
         &B_RC = CloseClipboard();
      End-If;
   End-If;
   &RC = GlobalUnlock(&LP_GMEM);
End-If;
&RC = GlobalFree(&H_GMEM);
End-If;
If &CLIPOK = False Then
MessageBox(48, "SendClip", 0, 0, "Failed to copy to clipboard");
End-If;
Return &CLIPOK;
End-Function;
The following example uses the function above to copy a string to clipboard:
&XX = djSendClip("copy data to clipboard");If &XX Then
WinMessage("Success");
Else
WinMessage("Fail");
End-If;



(this article was originally published on 12/14/01 at www.slerp.com. PSTools version 6,7)