8.3.4.2 using OLE objects on the Clipboard
To paste an OLE object into an OLE application, you must register two new clipboard formats with the RegisterClipboardFormat function of Windows for linked objects and embedded objects. These formats will be used in the Fmtidt domain of the Boleformat record.
In this chapter routines, the program registers the Clipboard format for the OLE object in the OnCreate event, the following code is the OnCreate event for the main form:
Procedure Toleframeform.formcreate (Sender:tobject);
Begin
FEMBEDCLIPFMT: = RegisterClipboardFormat (' Embedded Object ');
FLINKCLIPFMT: = RegisterClipboardFormat (' Link Source ');
Fmts[0].fmtid: = fembedclipfmt;
Fmts[0].fmtmedium: = Bolemediumcalc (FEMBEDCLIPFMT);
Fmts[0].fmtislinkable: = False;
Strpcopy (Fmts[0].fmtname, '%s ');
Strpcopy (Fmts[0].fmtresultname, '%s ');
Fmts[1].fmtid: = flinkclipfmt;
Fmts[1].fmtmedium: = Bolemediumcalc (FLINKCLIPFMT);
Fmts[1].fmtislinkable: = True;
Strpcopy (Fmts[1].fmtname, '%s ');
Strpcopy (Fmts[1].fmtresultname, '%s ');
Registerformasoledroptarget (Self, FMTS)
End
The program passes to the Registclipbroardformat function a parameter describing the format that returns the value of a word type. This value uniquely identifies the newly registered format. FEMBDECLIPFMT,FLINKCLIPFMT is a private data member of the Toleformat class. The statement reads as follows:
TYPE
Toleforaneform = Class (tform)
...
Private
Fembedclipfmt:word;
Flinkclipfmt:word;
function Createchild:toleobjectform;
Public
Fmts:array[0..1] of Boleformat;
End
After you register the Clipboard format, you must also define the OLE format for object pasting. Each format is defined in the Boleformat record. The standard clipboard format may be registered in the program and pasted in this format. For example, the register text as the paste format, the Boleformat record as the Fmtid field is defined as the CF_TEXT,FMT Medium domain is defined as bole_med_hglobol. The Bolemediumcalc function calculates the Fmtmedium value based on the Clipboard format that you define. In the chapter routines, the program registers two formats, one being the format of the linked OLE object and the other embedded in the OLE object.
The Boleformat type is defined in the Boledefs cell, and the BOLEMEDIUMCALC function is defined in the Toctrl unit. Therefore, the interface part of the main window should be added to these two units.
Interface
Use...,boledefs,toctrl,
Before pasting an OLE object, the application must know if there is an OLE object on the Clipboard.
The pastespecialenabled function can determine whether the Paste dialog box is valid. If there is any form of FMTS definition on the Clipboard, Pastespecialenable returns a true value, and the Paste dialog box can be successfully invoked. Conversely, calling the Paste dialog box will not have any events.
The following code implements the function of the edit | Paste menu item:
Procedure Toleobjectform.pastespecial1click (Sender:tobject);
Var
Clipfmt:word;
Datahand:thandle;
Info:pointer;
Begin
If pastespecialenabled (Self, Oleframeform.fmts) Then
If Pastespecialdlg (Self, Oleframeform.fmts, 0,
CLIPFMT, Datahand, Info) Then
Initializeoleobject (Info)
End
The Edit | Paste Menu works only if the Paste dialog box is valid, and the following code implements this functionality:
Procedure Toleobjectform.edit1click (Sender:tobject);
Begin
pastespecial1.enabled: = pastespecialenabled (Self, Oleframeform.fmts)
End
8.3.5 Releasing OLE objects
Dragging an OLE object from an OLE server and placing it in an OLE application is a convenient way to link and embed objects. By dragging and dropping, users do not need to use the Insert dialog box or the Paste dialog box to define an OLE object. Instead, simply use the mouse button to "grab" the OLE object from the OLE server, drag it to the OLE application, and release the mouse button to enable the OLE object to be inserted.
8.3.5.1 register OLE release target form
In order to receive a freed OLE object, you must have a form that is registered as an OLE release target in Windows and can be implemented using the Registerformasoledroptarget function.
Registerformasoledroptarger (Form:tfrom; Const Fmts:array of Bolefrom).
Where form is the release target form of the OLE object, the subform is passed to the form parameter in the chapter routines.
FMTS is an array of object formats. It is an array of boleformat types. All data to be freed must be registered with the corresponding Boleformat element in the FMTS array.
In the chapter routines, the registered FMTS array is the same as the main form OnCreate event declaration array, that is, the join object format and the embedded object format. If you want to receive more types of release data, you must add additional elements to the FMTS array. For example, if the application is to receive the freed text, the FMTS needs to add a third element whose Fmtid domain is bole_med_hglobl for the Cf_text,bolemedium field.
You do not need to use the Boleformat fmtname,fmtresultname field during the drag-and-drop process, and you can not initialize two domains if the program is only dragging and dropping without object pasting.
You can call Registerformasoledroptorget in the OnCreate event of the main form.
Procedure Toleframeform,formcreate (Sender:tobject);
Begin ...
Register Formasoledroptarget (SELF,FMTS)
End