home *** CD-ROM | disk | FTP | other *** search
- unit Ole2win;
-
- interface
-
- uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, StdCtrls,
- ToCtrl, BoleDefs, Buttons, Menus, ExtCtrls, Dialogs;
-
- type
- TOLEWin = class(TForm)
- MainMenu1: TMainMenu;
- Edit1: TMenuItem;
- Cut1: TMenuItem;
- Copy1: TMenuItem;
- Paste1: TMenuItem;
- InsertObject1: TMenuItem;
- PasteSpecial1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- Object1: TMenuItem;
- Object2: TMenuItem;
- ResetObject1: TMenuItem;
- PasteInObject1: TMenuItem;
- Links1: TMenuItem;
- File1: TMenuItem;
- New1: TMenuItem;
- Open1: TMenuItem;
- Save1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Help1: TMenuItem;
- ScrollBox1: TScrollBox;
- OleContainer1: TOleContainer;
- SpdBar: TPanel;
- MsgBar: TPanel;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure ResetObjectClick(Sender: TObject);
- procedure CloseBtnClick(Sender: TObject);
- procedure Copy1Click(Sender: TObject);
- procedure InsertObject1Click(Sender: TObject);
- procedure PasteSpecial1Click(Sender: TObject);
- procedure Links1Click(Sender: TObject);
- procedure Edit1Click(Sender: TObject);
- procedure Object1Click(Sender: TObject);
- procedure Cut1Click(Sender: TObject);
- procedure StatusLineEvent(Sender: TObject; Msg: String);
- procedure PasteInObject1Click(Sender: TObject);
- procedure NewOleWin1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure Open1Click(Sender: TObject);
- procedure Save1Click(Sender: TObject);
- procedure DoDrop(DragTgt, DragSource: TObject; X, Y: Integer);
- private
- { Private declarations }
- FLinkClipFmt : Word;
- FEmbedClipFmt : Word;
- procedure CreateOleObject (PInfo : Pointer);
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- end;
-
- var
- OLEWin: TOLEWin;
-
- implementation
-
- uses SysUtils;
-
- {$R *.DFM}
-
- constructor TOLEWin.Create(AOwner: TComponent);
- begin
- inherited Create (AOwner);
- FEmbedClipFmt := RegisterClipboardFormat ('Embedded Object');
- FLinkClipFmt := RegisterClipboardFormat ('Link Source');
- RegisterFormAsOleDropTarget (Self,
- [OleFormat (FEmbedClipFmt, '%s', '%s', FALSE),
- OleFormat (FLinkClipFmt, '%s', '%s', TRUE)]);
- OnDragDrop := DoDrop;
- end;
-
- procedure TOLEWin.ResetObjectClick(Sender: TObject);
- var
- InitInfo : Pointer;
- begin
- if ActiveControl.InheritsFrom (TOleContainer) then
- begin
- if InsertOleObjectDlg (Self, 0, InitInfo) = True then
- begin
- TOleContainer(ActiveControl).PInitInfo := InitInfo;
- ReleaseOleInitInfo (InitInfo);
- end;
- end;
- end;
-
- procedure TOLEWin.PasteInObject1Click(Sender: TObject);
- var
- InitInfo : Pointer;
- begin
- if ActiveControl.InheritsFrom (TOleContainer) then
- begin
- if PasteSpecialOleDlg (Self, 0, InitInfo) = True then
- begin
- TOleContainer(ActiveControl).PInitInfo := InitInfo;
- ReleaseOleInitInfo (InitInfo);
- end;
- end;
- end;
-
- procedure TOLEWin.CloseBtnClick(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TOLEWin.Copy1Click(Sender: TObject);
- begin
- if ActiveControl.InheritsFrom (TOleContainer) then
- begin
- TOleContainer (ActiveControl).CopyToClipboard (True);
- MessageDlg ('Copied to clipboard!', mtInformation, [mbOK], 0);
- end;
- end;
-
- procedure TOLEWin.InsertObject1Click(Sender: TObject);
- var
- PInfo: Pointer;
- begin
- if InsertOleObjectDlg (Self, 0, PInfo) then
- CreateOleObject (PInfo);
- ReleaseOleInitInfo (PInfo);
- end;
-
- procedure TOLEWin.CreateOleObject (PInfo : Pointer);
- var
- Ctrl : TOleContainer;
- X,Y : Integer;
- begin
- Ctrl := TOleContainer.Create (Self);
- X := 10;
- Y := 10;
- if ScrollBox1.ControlCount > 0 then
- begin
- X := ScrollBox1.Controls [ScrollBox1.ControlCount - 1].Left;
- Y := ScrollBox1.Controls [ScrollBox1.ControlCount - 1].Top +
- ScrollBox1.Controls [ScrollBox1.ControlCount - 1].Height + 20;
- end;
- Ctrl.SetBounds (X, Y, 230, 150);
- Ctrl.visible := True;
- Ctrl.enabled := True;
- Ctrl.AutoSize := True;
- Ctrl.OnStatusLineEvent := StatusLineEvent;
- Ctrl.PInitInfo := PInfo;
- Ctrl.Parent := ScrollBox1;
- ActiveControl := Ctrl;
- end;
-
- procedure TOLEWin.PasteSpecial1Click(Sender: TObject);
- var
- Ptr : PChar;
- Str : String;
- Fmt : Word;
- Hdl : THandle;
- PInfo : Pointer;
- begin
- if PasteSpecialDlg (Self,
- [OleFormat (FEmbedClipFmt, '%s', '%s', FALSE),
- OleFormat (FLinkClipFmt, '%s', '%s', TRUE)],
- 0, Fmt, Hdl, PInfo) then
- begin
- if (Fmt = CF_TEXT) then
- begin
- Ptr := GlobalLock (Hdl);
- Str := StrPas (Ptr);
- GlobalUnlock (Hdl);
- Str := Format('Text on the clipboard = %s', [Str]);
- MessageDlg (Str, mtInformation, [mbOK], 0);
- GlobalFree (Hdl);
- end
- else if (Fmt = CF_METAFILEPICT) then
- begin
- MessageDlg ('MetaFile on the clipboard.', mtInformation, [mbOK], 0);
- GlobalFree (Hdl);
- end
- else
- CreateOleObject (PInfo);
- ReleaseOleInitInfo (PInfo);
- end;
- end;
-
- procedure TOLEWin.Links1Click(Sender: TObject);
- begin
- LinksDlg (Self, 0);
- end;
-
- procedure TOLEWin.Edit1Click(Sender: TObject);
- begin
- Cut1.Enabled := ActiveControl.InheritsFrom (TOleContainer);
- Copy1.Enabled := ActiveControl.InheritsFrom (TOleContainer);
- InsertObject1.Enabled := True;
- PasteSpecial1.Enabled := PasteSpecialEnabled (Self,
- [OleFormat (FEmbedClipFmt, '%s', '%s', FALSE),
- OleFormat (FLinkClipFmt, '%s', '%s', TRUE)]);
- Links1.Enabled := LinksDlgEnabled (Self);
- end;
-
- procedure TOLEWin.Object1Click(Sender: TObject);
- begin
- ResetObject1.Enabled := ActiveControl.InheritsFrom (TOleContainer);
- PasteInObject1.Enabled := ActiveControl.InheritsFrom (TOleContainer) and
- PasteSpecialEnabled (Self,
- [OleFormat (FEmbedClipFmt, '%s', '%s', FALSE),
- OleFormat (FLinkClipFmt, '%s', '%s', TRUE)]);
- end;
-
- procedure TOLEWin.Cut1Click(Sender: TObject);
- begin
- if ActiveControl.InheritsFrom (TOleContainer) then
- begin
- TOleContainer (ActiveControl).CopyToClipboard (True);
- ActiveControl.Free;
- MessageDlg ('Cut to clipboard!', mtInformation, [mbOK], 0);
- end;
- end;
-
- procedure TOLEWin.StatusLineEvent(Sender: TObject;
- Msg: String);
- begin
- MsgBar.Caption := Msg;
- end;
-
- procedure TOLEWin.DoDrop(DragTgt, DragSource: TObject; X, Y: Integer);
- var
- Ctrl: TOleContainer;
- Ptr: PChar;
- Str: String;
- Point: TPoint;
- begin
- if DragSource.InheritsFrom (TOleDropNotify) then
- begin
- if TOleDropNotify(DragSource).PInitInfo <> Nil then
- begin
- Point.X := X;
- Point.Y := Y;
- Point := ClientToScreen(Point);
- Point := ScrollBox1.ScreenToClient(Point);
- Ctrl := TOleContainer.Create (TForm(DragTgt));
- Ctrl.SetBounds (Point.X, Point.Y, 100, 100);
- Ctrl.visible := True;
- Ctrl.enabled := True;
- Ctrl.AutoSize := True;
- Ctrl.Parent := ScrollBox1;
- Ctrl.PInitInfo := TOleDropNotify(DragSource).PInitInfo;
- end;
- end;
- end;
-
- procedure TOLEWin.NewOleWin1Click(Sender: TObject);
- var
- Child : TOLEWin;
- begin
- Child := TOLEWin.Create(Self);
- Child.Visible := True;
- end;
-
- procedure TOLEWin.Exit1Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TOLEWin.Open1Click(Sender: TObject);
- var
- Frm : TOLEWin;
- Stream: TStream;
- begin
- if OpenDialog1.Execute then
- begin
- Frm := TOLEWin.Create(Application);
- Stream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
- try
- Frm.OleContainer1.LoadFromStream(Stream);
- finally
- Stream.Free;
- end;
- Frm.Caption := ExtractFileName(OpenDialog1.FileName);
- Frm.OleContainer1.Modified := False;
- Frm.Visible := True;
- end;
- end;
-
- procedure TOLEWin.Save1Click(Sender: TObject);
- var
- Stream: TStream;
- begin
- if SaveDialog1.Execute then
- begin
- Stream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
- try
- OleContainer1.SaveToStream(Stream);
- finally
- Stream.Free;
- end;
- end;
- end;
-
- end.
-
-