Use Visual C ++ to implement Ole clipboard

Source: Internet
Author: User

Room 702, No. 8-2 Suiyuan, Nanjing
Wang Ke

----I. Overview

---- There are two clipboard mechanisms in Windows: Windows standard clipboard and Ole clipboard.

---- The standard Windows clipboard is used by all Windows applications.ProgramShared system service, so it does not have its own handle or class. However, you can use the member function of the cwnd class to manage the clipboard.

---- Since the birth of OLE (Object Linking and Embedding, object link and embedding), the second clipboard mechanism-ole clipboard mechanism emerged in windows. The standard Windows clipboard API (Application Programming Interface) is still available, but it has been implemented by Ole data transmission. Ole supports UDT (uniform data transfer, unified data transmission), and supports clipboard cutting, copying, and pasting through drag-and-drop operations. In addition to the performance of the standard Windows clipboard, Ole clipboard also supports transferring User-Defined clipboard formats and can be bound to Ole formats (such as fonts and font sizes) when transferring data ). The OLE clipboard mechanism will become a more important data transmission mechanism.

---- This article briefly describes the implementation of the standard Windows clipboard and focuses on how to implement the OLE Clipboard through visual C ++.

----2. select an appropriate clipboard Mechanism

---- The following principles should be followed when selecting the clipboard mechanism to use:

---- If the application may have new performance in the future (for example, only plain text needs to be transferred now, but other features such as fonts may be required in the future), use the OLE clipboard.

---- If you are using an ole application or you want to use any Ole features (such as drag and drop), you should use the OLE clipboard mechanism.

---- If you provide Ole format (such as font and font size), use the OLE clipboard mechanism.

----Iii. Use Windows standard clipboard

---- Most Windows applications support cutting or copying data to the Windows clipboard and pasting data from the clipboard to the destination. In this process, the Clipboard data format has changed between multiple applications. The system architecture only supports some limited clipboard formats by implementing some limited classes. The following table lists the formats supported by the Windows standard clipboard (the first column "value" will be in the following programCode).

Value description: cf_bitmap: a bitmap handle (hbitmap ). Cf_dib a memory object that contains the bitmapinfo structure and follows the bitmap data. Cf_dif Data Interaction format cf_dspbitmap has a private format for bitmap display. Cf_dspenhmetafile has an Enhanced Metafile display format in private format. Cf_dspmetafilepict has a private metadata display format. Cf_dsptext has a private text display format. Cf_enhmetafile: the handle of an Enhanced Metafile (henhmetafile structure. The integer value of a series of GDI objects defined by the cf_gdiobjfirst to the cf_gdiobjlast application software. Cf_hdrop: A hdrop handle used to identify a column of files. Cf_metafilepict is the handle of a metadata file defined using the metafilepict structure. The text format of cf_oemtext pre-defined characters, each line is bound with a CR-LF character, and an empty character represents the end of the data. Cf_ownerdisplay the display format of the clipboard owner. The clipboard owner must display and update the clipboard's Viewer window, and receive messages such as wm_askcbformatname, watermark, wm_paintclipboard, wm_sizeclipboard, and wm_vscrollclipboard. The handle of the cf_palette palette cf_privatefirst to the integer value of the cf_privatelast private clipboard format. Cf_riff can submit audio data that is more complex than the cf_wave standard wave table file format cf_sylk Microsoft's sylk (symbolic link, symbolic link) cf_text format cf_wave uses a standard wave table file format, such as 11khz or 22khz PCM (pulse code modulation, pulse encoding modulation), to submit audio data. Cf_tifftiff graphic format cf_unicodetextunicode text format (Note: Only applicable to Windows NT or Windows 2000 operating systems)

---- Standard clipboard format commonly used in table 1

---- To compile a function that implements cut and copy commands, you must implement the selected operation in your application. To compile a function that implements the paste command, you need to request the clipboard to check whether it contains the data that your application can support. The following code implements the Copy command, and Other implementations can be modeled.

---- Program example:

 
Void cmyview: oneditcopy () {If (! Openclipboard () {afxmessagebox ("Clipboard cannot be opened"); Return ;}// Delete the current clipboard content if (! Emptyclipboard () {afxmessagebox ("Clipboard cannot be cleared"); return;} // obtain selected data // check whether it is in the format supported by the clipboard if (:: setclipboarddata (CF _??, Hdata) = NULL) // CF _?? The data format in the clipboard is specified. // Table 1 lists the standard clipboard formats {afxmessagebox ("data cannot be copied to the Clipboard"); closeclipboard (); return ;} //... closeclipboard ();}

----4. Use the OLE clipboard Mechanism

---- Here is an example of how to give you a perceptual knowledge about the OLE clipboard and what you need to do for the OLE clipboard: Microsoft Excel registers a custom format for the worksheet, this format provides more information than other standard formats (such as bitmap or plain text. When this data is pasted into a program that supports worksheets (such as Lotus 1-2-3), all formulas and values in the original worksheet are retained, it may also be updated as needed. Excel also stores data in the clipboard in OLE format, so that it can be embedded as an OLE object. Any Ole document package container (such as Microsoft Word) can paste the data into the document as an embedded object (for example, through "selective pasting ", can be pasted into the Excel worksheet object in Word ). This embedded object can be modified by activating Microsoft Excel (you can double-click the object in Word ). This worksheet can even be pasted into a drawing program (such as a paint brush ). Of course, you cannot modify the data in the worksheet in any way, because it is already an image.

---- In the above example, we should do roughly the following: register a custom format, transfer the format to the clipboard, and copy, cut, and paste it.

---- Register the custom format

---- The data in the OLE clipboard exists in multiple formats. When a user chooses to paste data from the clipboard, the application should be able to choose the format used to paste data. Applications should provide information in most formats, unless you specify to paste in a specific format (for example, only text or image paste ).

---- Windows defines many standard formats that can be transmitted through the clipboard (see table 1), and Ole also defines many special formats. Applications can register their own clipboard formats by obtaining more detailed information. This can be achieved by using the Win32 API function registerclipboardformat:

---- Registerclipboardformat (lpszformat );

---- It indicates that lpzxformat is a pointer to a string and is used to name a custom format. This function returns an unsigned integer, which is an idnumber in the format.

---- After registering a custom format, you can use the return value of the registerclipboardformat function to identify and use the format.

---- Transfer the format to the clipboard

---- To add more formats to the clipboard, you must inherit a class from coleclientitem or coleserveritem and reload the ongetclipboarddata function in the class. In this function, follow these steps.

---- Place more formats on the clipboard

---- 1. Create a coledatasource object.

---- 2. Pass the data source to a function. Use this function to access the coledatasource: cacheglobaldata function to add your data format to the supported format list.

---- 3. Access the coledatasource: cacheglobaldata to add a standard format to each supported format.

---- Program example:

 
Coledatasource * cmyitem: loads (bool bincludelink, lppoint pptoffset, lpsize psize) {assert_valid (this); If (m_pservernode = NULL) return NULL; coledatasource * pdatasource = new coledatasource; try {getnativeclipboarddata (pdatasource); getclipboarddata (pdatasource, bincludelink, pptoffset, psize);} catch_all (e) {Delete pdatasource; throw_last ();} cancel (pdatasource ); return pdatasource ;}

---- Copy, cut, and paste data

---- Copy or cut data to the clipboard

---- 1. Determine whether the data to be copied is a local data or an embedded object or link.

---- If the data is an embedded object or link, create a coleclientitem pointer pointing to the selected data.

---- If the data is localized and the application is a server, inherit a new class from the coleserveritem and create the object. Otherwise, a coledatasource object is created for the data.

---- 2. Access the copytoclipboard member function of the selected object.

---- 3. If you select the cut command instead of copying, delete the data from your application.

---- Program example:

 
Void cmainview: oneditcut () {assert (m_pselection! = NULL); try {m_pselection-> copytoclipboard (true); oneditclear ();} catch_all (e) {afxmessagebox (plaintext);} end_catch_all} void cmainview: oneditcopy () {assert (m_pselection! = NULL); try {m_pselection-> copytoclipboard (true);} catch_all (e) {afxmessagebox (idp_clipboard_copy_failed);} end_catch_all}

---- Paste data from the clipboard

---- Pasting data is more complex than copying data because you need to select the format of pasting.

---- 1. In your view, oneditpaste is implemented to process the operation for users to select and Paste commands from the editing menu.

---- 2. In the oneditpaste function, create a coledataobject object and access its attachclipboard member function to bind the object to the clipboard.

---- 3. Access the coledataobject: isdataavailable function to check whether a special format can be used. Of course, you can also use coledataobject: beginenumformats cyclically to find other formats until you find the most suitable format.

---- 4. paste data.

---- Program example:

Crectitem * cmainview: dopasteitem (bool blink, coledataobject * pdataobject, cpoint * Ppoint, clipformat cfformat) {beginwaitcursor (); crectitem * pitem = getdocument ()-> createitem (); assert_valid (pitem); bool ballowadjust = (Ppoint = NULL )? True: false; coledataobject clipboardData; If (pdataobject = NULL) {clipboardData. attachclipboard (); pdataobject = & clipboardData;} Try {If (cfformat = cmaindoc: m_cfprivate) {dopastenative (pdataobject, Ppoint, pitem);} else if (! Blink & cfformat = 0 & pdataobject-> isdataavailable (cmaindoc: m_cfprivate) {dopastenative (pdataobject, Ppoint, pitem);} else if (ballowadjust) {cpoint ptdef (10,-10); dopastestandard (blink, pdataobject, & ptdef, pitem, cfformat);} else {dopastestandard (blink, pdataobject, Ppoint, pitem, cfformat);} If (ballowadjust) {getdocument ()-> adjustitemposition (pitem) ;}} catch_all (E) {trace0 ("failed to embed/link an OLE object \ n"); pitem-> Delete (); pitem = NULL;} end_catch_allsetselection (pitem, true); getdocument () -> setmodifiedflag (); getdocument ()-> updateallviews (null, 0, pitem); endwaitcursor (); Return pitem;} void cmainview: oneditpaste () {coledataobject clipboardData; clipboardData. attachclipboard (); dopasteitem (& clipboardData); updateallviews ();}

---- indicates that the pasting operation (such as the oneditpaste function) is separated from the pasting function (such as dopasteitem). The biggest advantage is that when data is dragged to your application, you can use the same paste code. For example, you can access the dopasteitem function in the ondrop function to reuse code. In addition, the dopastenative and dopastestandard functions in the program code only describe one concept, so they are not implemented.

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.