Summary of the use of OleContainer in delphi

1: Define the header of the stream, OleContainer requires a Header
type in the stream

//The structure of the stream Header

TStreamHeader = record

Signature: Integer; //$434F4442

DrawAspect: Integer; //1

DataSize: Integer; //stream.size;

end;

Note: Stream required by OleContainer = header + memory stream

2: Take the field from the database to the memory stream:

TBlobField(FADOQuery3.FieldByName('FContent')).SaveToStream(AMemoryStream);

keep:

TBlobField(FADOQuery3.FieldByName('FContent')).LoadFromStream(AMemoryStream);

3: Read from the stream and display:

procedure TFrameWord.DispFileContent(AMStream: TMemoryStream);

where

ft: string;

ms: TMemoryStream;

oMemoryStream : TMemoryStream;

Header : TStreamHeader;

begin

ms := AMStream;

if (ms = nil) or (ms.Size = 0) then Exit;

ms.Position := 0;

oMemoryStream := TMemoryStream.Create;

try

with Header do

begin

Signature := $434F4442;

DrawAspect := 1;

DataSize := ms.Size;

end;

ms.Position := 0;

oMemoryStream.WriteBuffer(Header,SizeOf(Header));

oMemoryStream.CopyFrom(ms, 0);

oMemoryStream.Position := 0;

OleContainer.LoadFromStream(oMemoryStream);

OleContainer.DoVerb(ovPrimary);

finally

oMemoryStream.Free;

end;

end;

4: Read from the file:

OleContainer.CreateObjectFromFile('D:\work\BidBuild_Client\doc\Introduction to Practical ASP Components.DOC', False);

OleContainer.DoVerb(ovPrimary);

5: Set read-only, and hide the tool menu and the editing function in the right-click menu:

Word document:

OleContainer.OleObject.Application.ActiveDocument.Protect(2);

OleContainer.OleObject.application.CommandBars['Standard'].Visible:=false;

OleContainer.OleObject.application.CommandBars['Formatting'].Visible:=false;

OleContainer.OleObject.application.CommandBars['Reviewing'].Visible:=false;

or:

OleContainer.OleObject.Application.ActiveDocument.Protect(2);

OleContainer.OleObject.Application.ActiveWindow.ActivePane.DisplayRulers := False;

Excel document:

OleContainer.OleObject.Application.ActiveSheet.Protect;

OleContainer.OleObject.application.CommandBars['Standard'].Visible:=false;

OleContainer.OleObject.application.CommandBars['Formatting'].Visible:=false;

OleContainer.OleObject.application.CommandBars['Reviewing'].Visible:=false;

Note: The above code is placed after OleContainer.DoVerb(ovPrimary)

6: close

if OleContainer.State <> osEmpty then

OleContainer.Close;

7: The main properties and methods of the OLEContainer control

1) AllowInPlace property AllowInPlace:Boolean;

This property is used to determine the way to start the OLE object service program. If it is false, then when the OLE object is activated during the operation, the entire OLE service program is opened, that is, a separate

Opens a window, and when true, merges the service's menu into the application.

2) AutoActive type TAutoActivate=(aaManual,aaGetFocus,aaDoubleClick);

property AutoActivate: TAutoActivate;

Find the way. When aaManual is used, to activate the OLE object, the method DoVerb(OnShow) must be called in the program

3) CanPaste property CanPaste:Boolean;

Read-only property, true if the contents of the clipboard are suitable for pasting into an OLE object, false otherwise

4) CopyOnsave property CopyOnsave:boolean;

If true, it means that the OLE object is temporarily written to a file, otherwise it means that it is all left inside.

5) Iconic property Iconic:Boolean;

Whether to display as an icon to save space on the screen

6) Linked property Linked:Boolean;

Read-only property, returns true to indicate that the OLE object is attached to the document, and returns false to indicate that the OLE object is embedded in the document.

7) Modify property modified:Boolean;

This property is set to true when the OLE object has changed (including the object being deleted or replaced by another OLE object)

8) NewInserted property NewInserted:Boolean;

Read-only, if the InsertObjectDialog function just called inserts an OLE object, return true. At this time, Doverb(OvShow) can be called to activate the OLE object

elephant.

9) OleClassName property OleClassName:string;

Read-only. Returns the class name of the OLE object. When there are multiple OLE objects in the program, this property can be used as their respective labels.

10)OleObject property oleobject:Variant;

Read-only, returns the OLE object in the OLE container, this property is very important, through this property you can access the OLE service program.

11) OleObjectInterface property OleObjectInterface:IOleObject;

Read-only. Returns the OleObject interface of the OLE object, which is required to directly call the OLE API.

12) OldStreamFormat property OldStreamFormat:Boolean;

If true, the OLE object is stored in OLE1 format, if false, it is stored in OLE2 format.

13) PrimaryVerb property PrimaryVerb: integer;

Read-only. Returns the index number (serial number) of the main operation among the operations that can be performed by the OLE object

14) SizeMode type TSizeMode = (smClip, smCenter, smScale, smStretch, smAutoSize);

property SizeMode:TSizeMode;

smClip, the part beyond the container will be clipped.

smCenter, middle.

smScale, automatically adapts to the size of the container

smStretch, auto stretch

smAutoSize, the container automatically adjusts to fit the size of the OLE.

15) State type TObjectState=(osEmpty,osLoaded,osRunning,osOpen,osInPlaceActive,osUIActive);

property State: TObjectState;

Read-only, returns the state of the OLE object, which can be one of the following values.

osEmpty, there are no OLE objects in the container.

osLoaded, there are OLE objects in the container. But the OLE server is not running.

osRunning, the server is running.

osOpen, the object is running, and the OLE server is running alone.

osInPlaceActive, the object is running, the OLE service program menu will be merged into the client program to run.

osUIActive, the object is running, and the OLE service program menu has been merged into the client program to run.

Main methods of the OLEContainer control

1) ChangeIconDialog function ChangeIconDialog:Boolean;

Calling this function will open a dialog for changing the icon. When an OLE object is displayed as an icon, it is displayed with the icon selected by the user.

2) Close procedure procedure Close;

Close. If the OLE object has been modified, calling Close will first save the OLE object.

3) Copy process procedure copy;

Copy OLE object to clipboard

4) CteateLinkToFile方法 procedure CreateLinkToFile(FileName:string; Iconic:Boolean);

Creates an OLE object whose contents are read from the specified file, and the created OLE object is linked to the OLE container. If there is already an OLE object in the OLE container, the existing OLE object will be deleted, and unsaved changes will also be deleted. Obsolete. The Iconic parameter is set to True to indicate that the OLE object is displayed as an icon.

5) CreateObject 过程. procedure CreateObject(const OleClassName:string;Iconic:Boolean);

This process is used to create an OLE object. The OleClassName parameter specifies the identification name of the object. The created OLE object is embedded in the OLE container. If there is already an OLE object in the OLE container, the existing OLE object will be deleted and not saved. The modification is also invalidated. The Iconic parameter is set to True to indicate that the OLE object is displayed as an icon.

6) CreateObjectFromFile 过程 procedure CreateObjectFromFile(const FileName:string; Iconic:Boolean);

Creates an OLE object whose content is read from the specified file, and the created OLE object is embedded in the OLE container. If there is already an OLE object in the OLE container, the existing OLE object will be deleted, and unsaved changes will also be removed. Obsolete. The Iconic parameter is set to True to indicate that the OLE object is displayed as an icon.

7) CreateObjectFromInfo 方法, procedure CreateObjectFromInfo(const CreateInfo:TCreateInfo);

Create an OLE object whose content is read from the record specified by the CreateInfo parameter (this parameter is a record type that contains the information needed to create an OLE object)

8) DoVerb 方法. procedure DoVerb(Verb:Integer);

Used to operate on OLE objects, the verb parameter specifies the operation type.

9) ObjectPropertiesDialog函数 function objectPropertiesDialog:Boolean;

Used to open the Windows OLE Object Properties dialog box, used to modify the properties of the OLE object.

10) Run 方法.procedure Run;

It is used to run the OLE server program, but does not activate the OLE object itself. When the server program is running, activating the OLE object will be very fast.

11) PasteSpecialDialog方法 function PasteSpecialDialog:Boolean;

This method opens the Windows Paste Special dialog.

8: OLE method to insert pictures in Word

I use olecontainer to operate word in delphi, to insert pictures into it, use the pasteboard. But the position after the picture is placed is the beginning of this line, as follows:

{ paste a picture into word }

ClipBoard.Assign(aImage.Picture);

{ The following is the vba code--you can use the function of recording macro in word to obtain -------------------- bagin }

{Add x.OleObject.application before using vba eg: add worksheet in excel }

{ oc.OleObject.application.sheets.Add; }

intInlineShapesIndex:=ocDoc.OleObject.application.ThisDocument.InlineShapes.Count+1;

intShapesIndex:=ocDoc.OleObject.application.ThisDocument.Shapes.Count+1;

//Add an image from the clipboard

ocDoc.OleObject.application.Selection.Paste;

// convert to graphics

ocDoc.OleObject.application.ThisDocument.InlineShapes(intInlineShapesIndex).ConvertToShape;

//Set the text wrapping method

ocDoc.OleObject.application.ThisDocument.Shapes(intShapesIndex).WrapFormat.Type := 'wdWrapThrough';

//Set the white color in the image to transparent

ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).PictureFormat.TransparentBackground := 'msoTrue';

ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).PictureFormat.TransparencyColor:='RGB(255,255,255)';

ocDoc.OleObject.application.Selection.InlineShapes(intShapesIndex).Fill.Visible := 'msoFalse';

{ The above is the vba code--you can use the function of recording macro in word to obtain ----------------------end }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325015518&siteId=291194637