INF: Method for Sending Text to the Clipboard

ID Number: Q35100

2.03 2.10 3.00

WINDOWS

Summary:

Sending text to the Clipboard is usually a cumbersome process of

allocating and locking global memory, copying the text to that memory,

and sending the Clipboard the memory handle. This method involves many

pointers and handles and makes the entire process difficult to use and

understand.

Clipboard I/O is easily accomplished with an edit control. If a

portion of text is highlighted, an application can send the edit

control a WM_COPY or WM_CUT message to copy or cut the selected text

to the Clipboard. In the same manner, text can be pasted from the

Clipboard by sending a WM_PASTE message to an edit control.

The following example demonstrates how to use an edit control

transparently within an application to simplify sending and retrieving

text from the Clipboard. Note that this code will not be as fast as

setting or getting the Clipboard data explicitly, but it is easier

from a programming standpoint, especially if the text to be sent is

already in an edit control. Note also that the presence of the edit

window will occupy some additional memory.

More Information:

For simplified Clipboard I/O, do the following:

1. Declare a global HWND, hEdit, which will be the handle to the edit

control.

2. In WinMain, use CreateWindow() to create a child window edit

control. Use the style WS_CHILD, and give the control dimensions

large enough to hold the most text that may be sent to or received

from the Clipboard. CreateWindow() returns the handle to the edit

control that should be saved in hEdit.

3. When a Cut or Copy command is invoked, use SetWindowText() to place

the desired string in the edit control, then use SendMessage() to

select the text and copy or cut it to the Clipboard.

4. When a Paste command is invoked, use SetWindowText() to clear the

edit control, then use SendMessage() to paste text from the

Clipboard. Finally, use GetWindowText() to copy the text in the

edit control to a string buffer.

The actual coding for this procedure is as follows:

.

.

.

#define ID_ED 100

HWND hEdit;

.

.

.

/* In WinMain: hWnd is assumed to be the handle of the parent window, */

/* hInstance is the instance handle of the parent. */

/* The "EDIT" class name is required for this method to work. ID_ED */

/* is an ID number for the control, used by Get/SetDlgItemText. */

hEdit=CreateWindow("EDIT",

NULL,

WS_CHILD | BS_LEFTTEXT,

10, 15, 270, 10,

hWnd,

ID_ED,

hInstance,

NULL);

.

.

.

/* In the procedure receiving CUT, COPY, and PASTE commands: */

/* Note that the COPY and CUT cases perform the same actions, only */

/* the CUT case clears out the edit control. */

/* Get the string length */

short nNumChars=strlen(szText);

case CUT:

/* First, set the text of the edit control to the desired string */

SetWindowText(hEdit, szText);

/* Send a message to the edit control to select the string */

SendMessage(hEdit, EM_SETSEL, 0, MAKELONG(0, nNumChars));

/* Cut the selected text to the clipboard */

SendMessage(hEdit, WM_CUT, 0, 0L);

break;

case COPY:

/* First, set the text of the edit control to the desired string */

SetWindowText(hEdit, szText);

/* Send a message to the edit control to select the string */

SendMessage(hEdit, EM_SETSEL, 0, MAKELONG(0, nNumChars));

/* Copy the text to the clipboard */

SendMessage(hEdit, WM_COPY, 0, 0L);

break;

case IDM_PASTE:

/* Check if there is text available */

if (IsClipboardFormatAvailable(CF_TEXT))

{

/* Clear the edit control */

SetWindowText(hEdit, "\0");

/* Paste the text in the clipboard to the edit control */

SendMessage(hEdit, WM_PASTE, 0, 0L);

/* Get the test from the edit control into a string. */

/* nNumChars represents the number of characters to get */

/* from the edit control. */

GetWindowText(hEdit, szText, nNumChars);

}

else

MessageBeep(0); /* Beep on illegal request */

break;