Method for Sending Text to the ClipboardLast reviewed: November 2, 1995Article ID: Q35100 |
The information in this article applies to:
SUMMARYSending 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 INFORMATIONFor simplified Clipboard I/O, do the following:
. . . #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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |