TOleContainer是在CM_UIDEACTIVATE的消息处理中处理失去焦点的,因此只要把
这个消息截获,就可以保持激活了。下面是一例(长了点。。):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtnrs;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
OleContainer2: TOleContainer;
public
{ Public declarations }
end;
TOleContainerAc=class(TOleContainer)
private
FCannotDeactivate: Boolean;
procedure CMUIDeactivate(var Message: TMessage); message CM_UIDEACTIVATE;
published
property CannotDeactivate: Boolean read FCannotDeactivate write FCannotDeactivate;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TOleContainerAc }
procedure TOleContainerAc.CMUIDeactivate(var Message: TMessage);
begin
if not CannotDeactivate then
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OleContainer2 := TOleContainerAc.Create(Self);
OleContainer2.Parent := Self;
OleContainer2.AllowInPlace := True;
OleContainer2.Left := 10;
OleContainer2.Top := 10;
OleContainer2.Width := 300;
OleContainer2.Height := 300;
OleContainer2.Visible := True;
TOleContainerAc(OleContainer2).CannotDeactivate := True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
OleContainer2.CreateObject('Word.Document',False);
end;
end.