Objective:
This article is limited to obtaining and setting the text content of the Clipboard, but it does not involve resources such as pictures;
Example:
One: Set Clipboard text content (support general symbols, special symbols not tested)
BOOL Setclipboardtext (LPCSTR Text,hwnd hwnd) { ASSERT (HWND); Open Clipboard if (!::openclipboard (hWnd)) return false; If the Clipboard has content, close the Clipboard if (! EmptyClipboard ()) { closeclipboard (); return false; } Gets the length of the text that needs to be set int len=strlen (text); Request Clipboard space handle Hclip=globalalloc (gmem_moveable| Gmem_ddeshare, (len+1) *sizeof (char*)); if (hclip==null) { closeclipboard (); return false; } Space to apply lock char* pbuf= (char*) GlobalLock (hclip); if (pbuf==null) { globalfree (hclip); CloseClipboard (); return false; } Copy the text content to the Clipboard memcpy ((char *) pbuf,text,len*sizeof (char*)); Pbuf[len]=null; Operation complete, Release lock GlobalUnlock (hclip); if (Null==setclipboarddata (cf_text,hclip)) { globalfree (hclip); CloseClipboard (); return false; } CloseClipboard (); return true; }
Two: Get the Clipboard text content (the std_string here is wrapped in string, overloading the "=" number)
Gets the Clipboard text content std_string Getclipboardtext (HWND hwnd) { ASSERT (HWND); Std_string Clipboardtext; Determines whether the data format of the Clipboard can be processed. if (! Isclipboardformatavailable (cf_text)) return clipboardtext; Open the Clipboard. if (!::openclipboard (hWnd)) return clipboardtext; Get Data HANDLE Hmem = GetClipboardData (cf_text); if (hmem! = NULL) { //Gets a string. LPSTR LPSTR = (LPSTR) globallock (HMEM); if (lpStr! = NULL) { clipboardtext=lpstr; Release lock Memory GlobalUnlock (HMEM); } } Close clipboard closeclipboard (); return clipboardtext; }
Cond......
VC gets Clipboard text content