Copying Text to the Clipboard

To copy a short string of text to the clipboard, follow these steps:

1.Copy the string to global memory.

2.Open the clipboard.

3.Clear the clipboard.

4.Give the global memory handle to the clipboard.

5.Close the clipboard.

You copy text to the clipboard when the user chooses the Copy command
from the Edit menu. To process the menu input and copy the text string to
the clipboard, add a WM_COMMAND case to the window function. Add the following statements:

case WM_COMMAND:

switch (wParam) {

case IDM_COPY:

if (!(hData = GlobalAlloc(GMEM_MOVEABLE, GlobalSize (hText)))) {

OutOfMemory();

return (TRUE);

}

if (!(lpData = GlobalLock(hData))) {

GlobalFree(hData);

OutOfMemory();

return (TRUE);

}

if (!(lpszText = GlobalLock (hText))) {

OutOfMemory();

return (TRUE);

}

lstrcpy(lpData, lpszText);

GlobalUnlock(hData);

GlobalUnlock (hText);

/* Clear the current contents of the clipboard, and set

* the data handle to the new string.

*/

if (OpenClipboard(hWnd)) {

EmptyClipboard();

SetClipboardData(CF_TEXT, hData);

CloseClipboard();

}

hData = NULL;

break;

}

The GlobalAlloc function allocates enough memory to hold the string. The GMEM_MOVEABLE flag specifies moveable memory. The clipboard can take either fixed or moveable memory, but should not be given discardable memory. Moveable memory is the most efficient.

NOTE:

You should always check the return value when allocating or locking memory; a NULL return value indicates an out-of-memory condition.

You must lock movable memory in order to retrieve the memory address. Use the Windows lstrcpy function instead of the C run-time strcpy function, since strcpy cannot handle mixed pointers (lpszText is a short pointer and lpData is a long pointer). The clipboard requires the string to have a terminating null character. Finally, the memory must be unlocked before it can be copied to the clipboard.

Each time you copy the string to the clipboard, this code allocates another global memory block. The reason is that once you have passed a data handle to the clipboard, the clipboard takes ownership of it. This means that you can no longer use the handle other than to view contents, and you must not attempt to free the handle or change its contents.

Copy the global memory handle to the clipboard by following these steps:

1.Open the clipboard.

2.Empty the clipboard.

3.Set the data handle.

4.Close the clipboard.

The following statements carry out these steps:

1 if (OpenClipboard(hWnd)) {

2 EmptyClipboard();

3 SetClipboardData(CF_TEXT, hData);

CloseClipboard();

}

4 hData = NULL;

In this example:

1 The OpenClipboard function opens the clipboard for the specified window. OpenClipboard will fail if another window already has the clipboard open.
2 The EmptyClipboard function clears all existing handles in the clipboard and assigns ownership of the clipboard to the window that has it open. An application must empty the clipboard before copying data to it.
3 The SetClipboardData function copies the memory handle to the clipboard and identifies the data format, CF_TEXT. The clipboard is then closed by the CloseClipboard function.
4 Since the clipboard now owns the global memory identified by hData, it is convenient to set this handle to zero to prevent attempts to free or change the memory.