Method for Sending Text to the Clipboard

Last reviewed: November 2, 1995
Article ID: Q35100
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

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;


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrClp


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.