This article mainly introduces the following clipboard operations in VC++/MFC:
1, the text content of the operation
2. Operation of WMF data
3, the bitmap operation
4. Set Use custom format
5, the perception of clipboard content changes
6. Automatically paste data into another application window
I. Operation of text content
The following code demonstrates how to copy text content to the Clipboard (Unicode encoding is first converted to ASCII):
CString source;
The text content is saved in the source variable
if (OpenClipboard ())
{
Hglobal Clipbuffer;
char * buffer;
EmptyClipboard ();
Clipbuffer = GlobalAlloc (gmem_ddeshare, source. GetLength () +1);
Buffer = (char*) globallock (Clipbuffer);
strcpy (buffer, LPCSTR (source));
GlobalUnlock (Clipbuffer);
SetClipboardData (Cf_text,clipbuffer);
CloseClipboard ();
}
The following code shows how to get textual content from the Clipboard:
char * buffer = NULL;
Open clipboard
CString Fromclipboard;
if (OpenClipboard ())
{
HANDLE hdata = GetClipboardData (cf_text);
char * buffer = (char*) globallock (hdata);
Fromclipboard = buffer;
GlobalUnlock (Hdata);
CloseClipboard ();
}
Ii. operation of WMF data
Reading and writing image data on the Clipboard is a useful feature, and it is simple to implement. The following code shows how to copy an extended metafile to the clipboard:
if (OpenClipboard ());
{
EmptyClipboard ();
To create a metafile DC
CMETAFILEDC * CDC = new CMetaFileDC ();
Cdc->createenhanced (GetDC (), Null,null, "the_name");
Calling a drawing routine
Close the CMetaFileDC and get its handle
Henhmetafile handle = cdc->closeenhanced ();
Copy to Clipboard
SetClipboardData (Cf_enhmetafile,handle);
CloseClipboard ();
Remove DC
Delete CDC;
}
The following code demonstrates obtaining a metafile from the Clipboard and drawing it onto a client DC:
if (OpenClipboard ())
{
Get Clipboard data
Henmetafile handle = (henmetafile) getclipboarddata (cf_enhmetafile);
Show
CCLIENTDC DC (this);
CRect client (0,0,200,200);
dc. PlayMetaFile (handle,client);
Close clipboard
CloseClipboard ();
}
Third, the operation of the bitmap
Bitmap operations are slightly more complex, and the following example shows how to save a bitmap on the Clipboard:
if (OpenClipboard ())
{
EmptyClipboard ();
CBitmap * junk = new CBitmap ();
CClientDC CDC (this);
CDC DC;
dc. CreateCompatibleDC (&CDC);
CRect client (0,0,200,200);
Junk->createcompatiblebitmap (&cdc,client. Width (), client. Height ());
dc. Selectobje
CT (junk);
DrawImage (&dc,cstring ("Bitmap"));
Copy Data to Clipboard
SetClipboardData (Cf_bitmap,junk->m_hobject);
CloseClipboard ();
Delete junk;
}
The following code shows how to obtain bitmap data from the Clipboard:
if (OpenClipboard ())
{
Get Clipboard data
Hbitmap handle = (HBITMAP) getclipboarddata (CF_BITMAP);
CBitmap * BM = cbitmap::fromhandle (handle);
CClientDC CDC (this);
CDC DC;
dc. CreateCompatibleDC (&CDC);
dc. SelectObject (BM);
Cdc. BitBlt (0,0,200,200,&dc,0,0,srccopy);
CloseClipboard ();
}
Iv. setting up and using custom formats
Using the RegisterClipboardFormat () function, you can copy and paste any data type you want. For example, we have one of the following data types:
struct Myformatdata
{
Long Val1;
int val2;
};
We're going to copy it to the Clipboard and we can use the following code:
UINT format = RegisterClipboardFormat ("My_custom_format");
if (OpenClipboard ())
{
Myformatdata data;
Data.val1 = 100;
Data.val2 = 200;
Hglobal Clipbuffer;
EmptyClipboard ();
Clipbuffer = GlobalAlloc (gmem_ddeshare, sizeof (Myformatdata));
Myformatdata * buffer = (myformatdata*) globallock (Clipbuffer);
>
Save to Memory
*buffer = data;
Save to Clipboard
GlobalUnlock (Clipbuffer);
SetClipboardData (Format,clipbuffer);
CloseClipboard ();
}
Read the data using the following code:
UINT format = RegisterClipboardFormat ("My_custom_format");
Myformatdata data;
if (OpenClipboard ())
{
HANDLE hdata =getclipboarddata (format);
Myformatdata * buffer = (myformatdata*) globallock (hdata);
data = *buffer;
GlobalUnlock (Hdata);
CloseClipboard ();
}
V. Perception of changes in clipboard content
The contents of the Clipboard can be sensed by Windows messages as follows:
In your initialization code call:
SetClipboardViewer (); Add our programs to the Clipboard Watch chain
In your message map add:
On_message (Wm_drawclipboard, Onclipchange)//Add message handle
Which is declared as:
afx_msg void Onclipchange ();
Finally Implement:
void Cdetectclipboardchangedlg::onclipchange ()
{
CTime time = Ctime::getcurrenttime ();
Setdlgitemtext (Idc_changed_date,time. Format ("%a,%b%d,%Y--%h:%m:%s"));
Displayclipboardtext ();
}
Automatically pasting data into another application window
Just get the handle to the appropriate window and send a message to it