Clipboard operation of VC

Source: Internet
Author: User
Tags ole

Clipboard operation of VC

 

1. Text Content operations
2. WMF Data Operations
3. Bitmap operations
4. Set the custom format
5. detect changes in Clipboard content
6. Automatically paste data to another application window

I. Text Content operations
The following code demonstrates how to copy text content to the clipboard (unicode encoding first converts to ASCII ):

Cstring source;
// Save the text content 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 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 );

 

Traditional windows clipboard Programming

Copy data to the clipboard:
1. Call openclipboard () to set the data source window.
2. Call emptyclipboard () to clear the previous data in the clipboard.
3. Call setclipboarddata () to store the data on the clipboard.
4. Call closeclipboard () to allow other windows to access the clipboard.
Obtain the data on the clipboard:
1. Call openclipboard () to access the clipboard.
2. Call getclipboarddata () to obtain data.
3. Call closeclipboard () to release the clipboard.

When the delayed supply technology is used, the source data uses null as the data handle to call setclipboarddata (). When the data user getclipboarddata (), Windows sends wm_renderformat and wm_renderformats messages to the data publisher, the data provider responds to the message and generates data.

Limitation: the global memory is used for transmission. When the data volume is large, the system must use the virtual memory management mechanism to manage the data, which has a great impact on the switching efficiency.

//------------------------------------------------------------
Ole clipboard

Between the application and the standard clipboard, extended from the standard clipboard, supplemented the OLE data transmission mechanism, backward compatible with the standard clipboard.
The OLE clipboard uses the idataobject interface for transmission. Related APIs:
. Olesetclipboard (): place an idataobject interface pointer on the clipboard.
. Olegetclipboard (): obtains an idataobject interface pointer from the clipboard.
. Oleflushclipboard (): clears the OLE clipboard and releases the idataobject interface pointer.
. Oleiscurrentclipboard (): determines whether the specified object is currently on the clipboard.
How to Use the OLE clipboard:
1. The data creator program places the data to the clipboard and implements idataobject. The data creator uses olesetclipboard () to get a copy of idataobject and puts it on the clipboard.
2. when the clipboard has an idataobject pointer, Ole uses the clipboard like a common application. ole calls openclipboard to declare the clipboard owner. Ole clipboard uses the delayed supply mode. ole clipboard will create a hidden window as the clipboard owner (the hwnd parameter is required for openclipboard) -- create in oleinitialize.
3. ole enumerates the idataobject format, and CALLS setclipboard () for each data format provided in the Global handle (). the standard clipboard does not support file and Structure Transfer, so you can only place the global handle on the clipboard.
4. the data consumer accesses the clipboard. when it does not know Ole information, use the standard getclipboarddata () method to obtain data-the data is provided by the delay supply method. the OLE clipboard queries the idataobject interface, and then calls the getdata () method on the interface to obtain data. if the data consumer supports Ole, it can use olegetclipboard () to obtain the idataobject pointer and use getdata () to obtain data.

Idataobject support for MFC

. Coledatasource: a complete COM object that implements the idataobject interface. It is often used on the data provider side.
. Coledataobject: encapsulates an idataobject pointer to provide C ++ interfaces for developers. It is often used by the Data consumer.

A. Transmit data through the clipboard

1. Place the data in the clipboard.
Obtain the Data Pointer and create an instance of the coledatasource. Use this object to save the data. For example:
{
Lpctstr source = getstring ();
Coledatasource * pcods;
Hglobal H = globalalloc (ghnd | gmem_share, (_ tcslen (source) + 1) * sizeof (tchar ));
_ Tcscpy (lpstr (globalllock (h), source );
Globalunlock (h); // This statement must exist when global memory is used.
Pcods-> cacheglobaldata (cf_text, H );
Pcods-> setclipboard ();
// Does the global memory not be released? When you place the data to the OLE clipboard, Ole releases it.
// Do not want to use global memory? Use coledatasource: cachedata () to put the data source object, but two parameters to depict the data must be provided.
}
2. paste data from the clipboard
Declare a coledataobject instance and call coledataobject: attachclipboard (). you can use coledataobject: isdataavailable () to obtain the data format. However, coledataobject: beginenumformats ()/coledataobject: getnextformat () to enumerate all formats.
After the format is determined, use the following functions of the coledataobject to obtain the data:
. Coledataobject: getdataobject (); // The most common, processing all formats.
. Coledataobject: getfiledata (); // when the data storage medium is a file, the file is released by the user.
. Colddataobject: getglobaldata (); // the data is stored in the global memory.
Example:
{
Coledataobject Odo;
Odo. attachclipboard ();
If (Odo. isdataavailable (ct_text )){
Handle H = Odo. getglobaldata (cf_text );
// Use data
Globalunlock (h );
Globalfree (h );
}
Odo. Release ();
}
B. Supply delay.
Implemented by delayrenderdata (), delayrenderfiledata (), and onrenderdata () in colddatasource. onrenderdata () needs to be implemented in the coledatasource derived class. Similar functions include onrenderfiledata and onrenderglobaldata ().

//-----------------------------------------------------------

OLE drag and drop

Three interfaces are used: idataobject, idropsource (coledropsource implementation), and idroptarget (coledroptarget implementation ).

Control Key: [none] = mobile data; [control] = copy data; [control-shift] = create a shortcut; [alt] = move data

Start dragging: similar to placing data on the clipboard, it calls colddatasource: odragdrop () instead of coledatasource: setclipboard ();
End playback: Two coledroptarget and cview classes are required to place the target.
1. Register cview as a coledroptarget class. Declare a coledroptarget variable in the class and call coledroptarget: Register (this) in oncreate );
2. Reload the ondragenter (), ondragover (), ondragleave (), and ondrop () of the cview class ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.