Use Visual C to implement OLE clipboard Nanjing, Nanjing, 8-2, Room 702, Room 702 Wang Wei ---- One, Overview ---- There are two clipboard mechanisms in the Windows operating system: Windows standard clipboard and OLE clipboard mechanism. ---- Standard Windows Clipboard is a system service shared by all Windows applications, so it does not have its own handle or class. But you can manage the clipboard through a member function of the CWND class. ---- Since OLE (Object Linking and Embedding, Object Linking and Embedding, the WINDOWS operating system, the second clipboard mechanism - Alle Clipboard mechanism is displayed. Standard Windows Clipboard API (Application Programming Interface, Application Programming Interface) is still available, but he has been implemented by the OLE data transmission mechanism. OLE supports UDT (Uniform Data Transfer), and can implement the cut, copy, and paste of the clipboard by dragging operation. In addition to the performance of the standard Windows clipboard, the OLE Clipboard also supports transmission of user-defined clipboard formats, and can bind the OLE format (such as font, font, etc.) when transferring data. The OLE clipboard mechanism will become a more important data transmission mechanism. ---- This article will briefly describe the implementation of the standard Windows clipboard and focus on how to implement the OLE clipboard through Visual C . ---- II, choose the appropriate clipboard mechanism ---- When choosing what clipboard mechanism to use, the following principles should be followed: ---- If the application can have new performance in the future (for example now It is only necessary to transfer plain text, but there may be feature of additional transmission fonts in the future), then use the OLE clipboard. ---- If you are using an OLE application, or you want to use any OLE feature (such as drag and drop, etc.) then you should use the OLE clipboard mechanism. ---- If you provide OLE format (such as font, font, etc.), use the OLE clipboard mechanism. ---- Third, using the Windows Standard Clipboard ---- Most Windows Applications support clipping or copying data into the Windows clipboard and paste data from the clipboard to the destination. During this process, the clipboard data format has changed between a variety of applications. The system architecture only supports some limited classes to support some limited number of clipboard formats, the following table lists the formats supported by the Windows standard clipboard (first column "value" will be used in the program code below). Value meaning
CF_bitmap A handle (HBitmap) corresponding to bitmaps.
CF_DIB A BitmapInfo structure and
Follow the memory object of bitmap data.
CF_DIF Data Interaction Format
Cf_dspbitmap has a bitmap display format in a private format.
Cf_dspenhmetafile has a private format enhanced meta-file display format.
Cf_dspmetafilePict has a portfolio display format of a private format.
CF_DSPText has a text display format for private format.
Cf_enhmetafile The handle of a enhanced meta file (HENHMETAFILE structure).
CF_GDIOBJFIRST to CF_GDIOBJLAST application definition
A series of GDI objects integer.
CF_HDROP Handle of an HDROP type is used to identify a list of files.
CF_MetafilePict A handle of a meta file that uses the MetafilePict structure defined.
CF_OEMText The text format of a predefined character, each line is bound
A CR-LF character, and represents the end of the data with an empty character.
CF_OWNERDISPLAY Clipboard owner's display format, the owner of the clipboard must display and update the observer window of the clipboard,
And receive WM_ASKCBFORMATNAME, WM_HSCROLLLLPBOARD,
WM_PaintClipboard, WM_SIZECLIPBOARD,
WM_VSCROLLLLIPBOARD and other messages, the HMEM parameter must be NULL.
CF_PALETTE palette handle
CF_PRIVATEFIRST to CF_PRIVATELAST's integer value of the private clipboard format.
CF_RIFF can be submitted more complex audio data than the CF_WAVE standard wave form file format
CF_SYLK Microsoft's Sylk (Symbolic Link, Symbolic Link) Format
CF_Text text format
CF_WAVE uses a standard wavelert file format such as 11kHz or 22kHz, etc.
PCM (Pulse Code Modulation, Pulse Coding Modulator) Submits audio data.
CF_TIFF TIFF graphics format
CF_UNICODETEXT UNICODE text format (
Note: Only for Windows NT or Windows 2000 operating systems)
---- Table 1 Common standard clipboard format ---- To write a function that implements the cut and copy command, you must implement the selected operation in your application; to write a function that implements paste commands, Just request the clipboard to detect if it contains data that you can support your application. The following code implements the copy command, and other implementations can be impected, and there is no longer in this way. ---- Database & Sports Example: Void CMYVIEW :: OneTPY ()
{
IF (! OpenClipboard ())
{
AFXMessageBox ("Unable to turn on Clipboard");
Return;
}
/ / Delete the content of the current clipboard
IF (! EMPTYCLIPBOARD ())
{
AfxMessageBox ("Unable to Clear Clipboard");
Return;
}
/ / Get selected data
/ / Check if the format supported by the clipboard
IF (: setClipboardData (CF _ ??, hdata) == NULL)
// cf_ ?? Specifies the format of the data in the clipboard,
// Table 1 lists the standard clipboard format
{
AfxMessageBox ("cannot copy data to a clipboard");
CloseClipboard ();
Return;
}
// ...
CloseClipboard ();
}
---- Four, use the OLE clipboard mechanism - first give you some feelings about the OLE clipboard, while you need to do what you need for the ole clipboard: Microsoft Excel registers a worksheet. Custom format, this format is capable of providing more information than other standard formats (such as bitmaps or plain text, etc.). When this data is pasted into a program that supports worksheets (such as Lotus 1-2-3), all formulas and values in all original worksheets will be retained and may be updated as needed. Excel also stores data in an OLE format in the clipboard so that it can be embedded as an OLE object. Any OLE document package container (such as Microsoft Word) can paste the data as an embedded object into documentation (such as via "selective Paste", you can bind into the Excel worksheet object in Word). This embedded object can be modified by activating Microsoft Excel (can be implemented in Word). This worksheet can even be pasted into a plot program (such as a brush). Of course, at this time, you have no way to modify the data as like in a worksheet because it is already a picture. ---- From the above example, we should work roughly: register custom format, transport formats to clipboards and to achieve copy, cut, and paste. ---- Register Custom Format ---- The data in the OLE clipboard exists in a variety of formats. When a user selects data from the clipboard, the application should be able to choose from to which format paste data. Applications should provide information on most formats unless the user specifies that it is specified to use a particular format stick (such as only paste or only paste pictures, etc.). ---- Windows defines a lot of standard formats that can be transmitted by clipboard (see Table 1), OLE also defines a lot of special formats. Applications can register their own clipboard format by getting more detailed information. This can be implemented by using the Win32 API function REGISTERCLIPBOARDFORMAT; ---- RegisterClipboardFormat (LPSZFORMAT); --- Description LPZXFORMAT refers to a pointer to a string to name a custom format. This function returns a symbolic integer, which is the ID number of the format ---- After registering the custom format, you can use the return value of the RegisterClipboardFormat function to identify and use this format. ---- Transfer format to the clipboard --- To add more formats to the clipboard, you must inherit a class from the ColeClientItem or CauleServerItem and overload the ONGETCLIPBoardData function in this class. In this function, you should do it according to the following steps. ---- Place more format on the clipboard - 1. Create a COLLDATASOURCE object. ---- 2. Pass the data source to a function, use this function to add your data format to the supported format list by accessing the COLLDATASOURCE :: CacheglobalData function. ---- 3. Access Coledatasource :: CacheglobalData to add a standard format to your supported format. ---- Program Example: COLEDATASOURCE * CMYITEM :: ONGETCLIPBOARDDATA (Bool Bincludelink, LPPOINT PPTOFSET, LPSIZE PSIZE)
{
Assert_Valid (this);
IF (m_pservernode == null)
Return NULL;
COLEDATASOURCE * PDATASOURCE = New ColedataSource;
Try
{
GetnativeClipboardData (PDataSource);
GetClipboardData (PDataSource, Bincludelink,
PPTOFSET, PSIZE);
}
Catch_all (e)
{
DELETE PDATASOURCE;
Throw_last ();
}
END_CATCH_ALL
Assert_Valid (pdataser);
Return PDataSource;
}
---- Copy, Cut and Paste Data - Copy or Cut the Data on the Clipboard - 1. Determine 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, then a new class is inherited from CauleServerItem and creates the object. Otherwise, establish a COLLDATASOSOSOURCE object for the data. ---- 2. Access the CopyToClipboard member function of the selected object. ---- 3. If the user selects the cut command instead of copy, then those data are removed from your application. ---- Program example: void cmainview :: Onetitcut ()
{
Assert (m_pselection! = Null);
Try
{
m_pselection-> CopyToClipboard (TRUE);
Oneditclear ();
}
Catch_all (e)
{
AfxMessageBox (idp_clipboard_cut_failed);
}
END_CATCH_ALL
}
Void CMAINVIEW :: OneTCopy ()
{
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 ---- Paste data is more complicated than replication, as you need to select a paste format. ---- 1. In your view, implement OneDitPaste to handle the operation of the user from the Edit menu to select the paste command. ---- 2. In the OneDitPaste function, create a COLLDATAOBJECT object and access its AttachClipboard member function to bind this object to the clipboard. ---- 3. Access the COLEDATAOBJECT:: ISDataavailable function to check if you can use a special format. Of course, you can also use ColedataObject :: BeGineNumFormats to find other formats by looping until you find the most suitable format. ---- 4. Paste data. ---- Program Example: CRECTITEM * CMAINVIEW :: DOPASTEITEM (Bool Blink,
COLEDATAOBJECT * PDATAOBJECT, CPOINT * PPOINT,
CLIPFORMAT CFFFORMAT)
{
Beginwaitcursor ();
CRECTITEM * PITEM = GetDocument () -> CREATETEM ();
Ask_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 && CFFMAT == 0 &&
PDataObject-> Isdataavailable (cmaindoc :: m_cfprivate)))
{
DOPASTENATIVE (PDataObject, Ppoint, Pitem);
}
Else IF (ballowadjust)
{
Cpoint PTDef (10, -10);
DopastAndardard (Blink, PDataObject,
& PTDEF, PITEM, CFFORMAT;
}
Else
{
DopastAndardard (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_ALL
SetSelection (Pitem, True);
GetDocument () -> setmodifiedflag ();
GetDocument () -> UpdateAllViews (NULL, 0, PITEM);
ENDWAITCURSOR ();
Return Pitem;
}
Void CMAINVIEW :: OneTPASTE ()
{
COLEDATAOBJECT ClipboardData;
ClipboardData.attachClipboard ();
DOPASTEITEM (& ClipBoardData);
UpdateAllViews ();
}
---- Description The biggest advantage that the paste operation (such as an onEditPaste function) and the implementation paste (such as Dopastetem) is that the same paste code can be used when the data is dragged and dropped into your application. For example, you can access the DopasteItem function in the onDrop function to reuse the code. In addition, the DOPASTENATIVE and DOPASTestandard functions in the program code only illustrate one concept, so it is no longer implemented.