Objective:
This article is limited to obtaining and setting the contents of the Clipboard, but not to the picture and other resources;
Example:
One: Set Clipboard text content (support for general symbols, special symbols not tested)
BOOL Setclipboardtext (LPCSTR Text,hwnd hwnd)
{
ASSERT (HWND);
Open the Clipboard
if (!::openclipboard (hWnd)) return
false;
If the Clipboard has content, close the Clipboard if
(! EmptyClipboard ())
{
closeclipboard ();
return false;
}
Gets the length
int len=strlen (text) for which the text needs to be set;
Request Clipboard Space
HANDLE Hclip=globalalloc (gmem_moveable| Gmem_ddeshare, (len+1) *sizeof (char*));
if (hclip==null)
{
closeclipboard ();
return false;
}
The space of the application is locked
char* pbuf= (char*) GlobalLock (hclip);
if (pbuf==null)
{
globalfree (hclip);
CloseClipboard ();
return false;
}
Copy text content to 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;
}
Second: Get the Clipboard text content (the std_string here is encapsulated by a string, overloaded with the "=" number)
Gets the Clipboard text content
std_string Getclipboardtext (hwnd hwnd)
{
ASSERT (HWND);
Std_string Clipboardtext;
Determines whether the Clipboard data format can be processed.
if (! Isclipboardformatavailable (Cf_text)) return
Clipboardtext;
Opens the Clipboard.
if (!::openclipboard (hWnd)) return
Clipboardtext;
Get Data
HANDLE Hmem = GetClipboardData (cf_text);
if (Hmem!= NULL)
{
//get string.
LPSTR LPSTR = (LPSTR) globallock (HMEM);
if (lpStr!= NULL)
{
clipboardtext=lpstr;
Release lock Memory
GlobalUnlock (HMEM);
}
Close clipboard
closeclipboard ();
return clipboardtext;
}
Cond......