如何正确使用TForm中的TOleContainer中的Word,布局方式?

时间:2012-07-03 20:14:11

标签: delphi layout ms-word ole

我想知道在TForm中嵌入和控制MS Word的建议方法是什么?目前,我(1)在TForm上放了两个TPanel。 alBottom TPanel有一个TButton,alClient TPanel有alNone个TOleContainer。 (2)在TMainForm.FormCreate事件处理程序中设置布局。

问题是MS Word喜欢占用其父表单的所有空间。只有如下所示的第四种方式似乎给出了可接受的布局。基于反复试验,似乎有必要使用子表单而不是TPanel来托管TOleContainer。 (另外,Windows.SetParent似乎是必要的。)我想知道子表单是否是正确的方法?

PS:Delphi XE,Word 2010,Windows 7

PS:与“托管外部应用程序”相关的网页:

Binh Ly's site

Deborah's site

How to shell to another app and have it appear in a delphi form

TOleContainer Revisited

Open word document in delphi?

Delphi & Word (SimpChn)

PS:与“面板形式(子表格)”相关的网页:

how to make a transparent form inside Panel?

Delphi - OleContainer - PowerPoint - AutoPlay

FreePascal/Lazarus MultiDoc

TForm in a panel

How to create a delphi form containing multiple 'child' forms that can be moved/sized and show activated

示例代码

unit uMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, ExtCtrls, StdCtrls;

type
  TMainForm = class(TForm)
    PanelOle: TPanel;
    PanelBtn: TPanel;
    OleContainer1: TOleContainer;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.FormCreate(Sender: TObject);
var
  OleForm: TForm;
begin
////
//// 1
////
//  OleContainer1.Parent := PanelOle;
//  OleContainer1.Align := alClient;
//
////
//// 2
////
//  Windows.SetParent(OleContainer1.Handle, PanelOle.Handle);
//  OleContainer1.Align := alClient;
//
////
//// 3
////
//  OleForm := TForm.Create(Self);
//  OleForm.Parent := PanelOle;
//  OleForm.Align := alClient;
//  OleForm.Visible := True;
//  OleContainer1.Parent := OleForm;
//  OleContainer1.Align := alClient;
//
////
//// 4 Works!
////
//  OleForm := TForm.Create(Self);
//  Windows.SetParent(OleForm.Handle, PanelOle.Handle);
//  OleForm.Align := alClient;
//  OleForm.Visible := True;
//  OleContainer1.Parent := OleForm;
//  OleContainer1.Align := alClient;
//
////
//// 5
////
//  OleForm := TForm.Create(Self);
//  OleForm.Parent := PanelOle;
//  OleForm.Align := alClient;
//  OleForm.Visible := True;
//  Windows.SetParent(OleContainer1.Handle,OleForm.Handle);
//  OleContainer1.Align := alClient;
//
////
//// 6
////
//  OleForm := TForm.Create(Self);
//  Windows.SetParent(OleForm.Handle, PanelOle.Handle);
//  OleForm.Align := alClient;
//  OleForm.Visible := True;
//  Windows.SetParent(OleContainer1.Handle,OleForm.Handle);
//  OleContainer1.Align := alClient;

end;

procedure TMainForm.Button1Click(Sender: TObject);
var
// Declare the item to be a generic OleVariant to force late binding
  Ds: OleVariant;
  D: OleVariant;
begin
  OleContainer1.CreateObjectFromFile('sample.docx', False);

  OleContainer1.Run;

  OleContainer1.AutoActivate := aaManual;
  OleContainer1.DoVerb(ovShow);  // not in FormCreate, in or after FormShow

  Ds := OleContainer1.OleObject.Application.Documents;
  Caption := IntToStr(Ds.Count);
end;

end.

1 个答案:

答案 0 :(得分:1)

子表单是一种正确的方法。我们在生产环境中使用这种方法并且它起作用。我们在一个小组中托管了我们的“子”表格。但是,我们使用标志修改了TOleContainer和TOleForm,无论是使用父窗体还是最顶层窗体:

procedure TOurOleContainer.InitObject;
...
begin
  if FDrawInTopForm then
    DocForm := GetParentForm(Self)
  else
    DocForm := TCustomForm(Parent);
...

FDrawInTopForm是我们介绍的属性。我们还修改了:

function GetVCLFrameForm(Form: TCustomForm; DrawInTopForm: Boolean): IVCLFrameForm;
begin
  if Form.OleFormObject = nil then TOleForm.Create(Form, DrawInTopForm); 
  Result := Form.OleFormObject as IVCLFrameForm;
end;

不幸的是,由于与客户的协议,我无法在此发布完整的解决方案。