I. Text Content operations
The following code demonstrates how to copy text content to the clipboard:
Cstring source;
// Save the text content in the source variable
If (openclipboard ())
{
Hglobal clipbuffer;
Char * buffer;
Emptyclipboard ();
Clipbuffer = globalalloc (gmem_ddeshare, dource. getlength () + 1 );
Buffer = (char *) globallock (clipbuffer );
Strcpy (buffer, lpcstr (source ));
Globalunlock (clipbuffer );
Setclipboarddata (cf_text, clipbuffer );
Closeclipboard ();
}
The following code shows how to obtain text from the clipboard:
Char * buffer = NULL;
// Open the clipboard
Cstring fromclipboard;
If (openclipboard ())
{
Handle hdata = getclipboarddata (cf_text );
Char * buffer = (char *) globallock (hdata );
Fromclipboard = buffer;
Globalunlock (hdata );
Closeclipboard ();
}
Ii. WMF Data Operations
Reading and Writing image data on the clipboard is very useful and easy to implement. The following code shows how to copy an extended Metafile to the clipboard:
If (openclipboard ());
{
Emptyclipboard ();
// Create a Metafile DC
Cmetafiledc * CDC = new cmetafiledc ();
CDC-> createenhanced (getdc (), null, null, "the_name ");
// Call the plotting routine
// Close cmetafiledc and obtain its handle
Henhmetafile handle = CDC-> closeenhanced ();
// Copy to clipboard
Setclipboarddata (cf_enhmetafile, handle );
Closeclipboard ();
// Delete the DC
Delete CDC;
}
The following code obtains the Metafile from the clipboard and draws it to the client DC:
If (openclipboard ())
{
// Obtain the Clipboard data
Henmetafile handle = (henmetafile) getclipboarddata (cf_enhmetafile );
// Display
Cclientdc DC (this );
Crect client (0, 0, 200,200 );
DC. playmetafile (handle, client );
// Close the clipboard
Closeclipboard ();
}
3. Bitmap operations
Bitmap operations are a little more complex. The following example shows how to save the 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. SelectObject (junk );
Drawimage (& DC, cstring ("bitmap "));
// Copy the data to the clipboard
Setclipboarddata (cf_bitmap, junk-> m_hobject );
Closeclipboard ();
Delete junk;
}
The following code shows how to obtain bitmap data from the clipboard:
If (openclipboard ())
{
// Obtain the 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 (200,200, & DC, srccopy );
Closeclipboard ();
}
Iv. Set and use custom formats
You can use the registerclipboardformat () function to copy and paste any data type you need. For example, we have the following data type:
Struct myformatdata
{
Long val1;
Int val2;
};
To copy it to the clipboard, you 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 ();
}
Use the following code to read data:
Uint format = registerclipboardformat ("my_custom_format ");
Myformatdata data;
If (openclipboard ())
{
Handle hdata = getclipboarddata (format );
Myformatdata * buffer = (myformatdata *) globallock (hdata );
Data = * buffer;
Globalunlock (hdata );
Closeclipboard ();
}
5. Perception of changes in Clipboard content
You can use Windows messages to detect whether the clipboard content has changed. The Code is as follows:
In your initialization Code call:
Setclipboardviewer (); // Add our program to the clipboard observation chain
In your message map Add:
On_message (wm_drawclipboard, onclipchange) // Add message handle
Which is declared:
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 ();
}
6. Automatically paste data into another application window
You only need to get the handle of the corresponding window and send a message:
Sendmessage (m_htextwnd, wm_paste, 0, 0 );