手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

用Delphi制作Office的Com AddIn

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
最近想做一个像金山词霸那样在Word上面增加一个按钮的东西
在网上找了一会儿,竟然没有Delphi的例子,没办法只好自己搞定,

1. 新建一个Active Library
2. 新建一个COM Object,在Class Name填一个名字,如Test。
点一下Implemented Interface后面的List按钮。再点一下对话框中的Add Library按钮,
选择“Program Files\Common Files\Designer”目录下的msaddndr.dll文件。
然后在列表中找到msaddndr.dll里面的_IDTExtensibility2接口点击确定。
3. 现在Com AddIn部分已经完成,现在要在Word里面加一个CommandBar和一个按钮,并且让按钮响应事件。

4. 创建一个TcommandBarButton的OleServer类以连接到CommandButton并响应事件。代码:如下
定义部分
TCommandBarButtonClick = procedure(const Ctrl: OleVariant; var CancelDefault: OleVariant) of Object;
TCommandBarButton = class(TOleServer)
private
FIntf: CommandBarButton;
FOnClick: TCommandBarButtonClick;
function GetDefaultInterface: CommandBarButton;
procedure SetOnClick(const Value: TCommandBarButtonClick);
protected
procedure InitServerData; override;
procedure InvokeEvent(DispID: TDispID; var Params: TVariantArray); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect; override;
procedure ConnectTo(svrIntf: CommandBarButton);
procedure Disconnect; override;
property DefaultInterface: CommandBarButton read GetDefaultInterface;
published
property OnClick : TCommandBarButtonClick read FOnClick write SetOnClick;
end;
实施部分
{ TCommandBarButton }

procedure TCommandBarButton.Connect;
var
punk: IUnknown;
begin
if FIntf = nil then
begin
punk := GetServer;
ConnectEvents(punk);
Fintf:= punk as CommandBarButton;
end;
end;

procedure TCommandBarButton.ConnectTo(svrIntf: CommandBarButton);
begin
Disconnect;
FIntf := svrIntf;
ConnectEvents(FIntf);
end;

constructor TCommandBarButton.Create(AOwner: TComponent);
begin
inherited;

end;

destructor TCommandBarButton.Destroy;
begin

inherited;
end;

procedure TCommandBarButton.Disconnect;
begin
if Fintf <> nil then
begin
DisconnectEvents(FIntf);
FIntf := nil;
end;
end;

function TCommandBarButton.GetDefaultInterface: CommandBarButton;
begin
if FIntf = nil then
Connect;
Assert(FIntf <> nil, ''''DefaultInterface is NULL. Component is not connected to Server. You must call ''''''''Connect'''''''' or ''''''''ConnectTo'''''''' before this operation'''');
Result := FIntf;
end;

procedure TCommandBarButton.InitServerData;
const
CServerData: TServerData = (
ClassID: ''''{55F88891-7708-11D1-ACEB-006008961DA5}'''';
IntfIID: ''''{000C030E-0000-0000-C000-000000000046}'''';
EventIID: ''''{000C0351-0000-0000-C000-000000000046}'''';
LicenseKey: nil;
Version: 500);
begin
ServerData := @CServerData;
end;

procedure TCommandBarButton.InvokeEvent(DispID: TDispID;
var Params: TVariantArray);
begin
case DispID of
-1: Exit; // DISPID_UNKNOWN
1: if Assigned(FOnClick) then
FOnClick(Params[0], Params[1]);
end; {case DispID}
end;

procedure TCommandBarButton.SetOnClick(
const Value: TCommandBarButtonClick);
begin
FOnClick := Value;
end;

5. 继续完成Ttest类
在类定义里面增加两项
private
FCommandBarButton : TCommandBarButton;
procedure FClick(const Ctrl: OleVariant; var CancelDefault: OleVariant);

在OnConnection写下面代码
procedure TTest.OnConnection(const Application: IDispatch;
ConnectMode: ext_ConnectMode; const AddInInst: IDispatch;
var custom: PSafeArray);
//这是从资源中读取一个Bitmap并复制到粘贴板
procedure CopyBitMapToClipBoard;
var
aStream : TStream;
aBitMap : Graphics.TBitmap;
begin
with TClipboard.Create do
begin
try
aStream := TResourceStream.CreateFromID(HInstance, 1, RT_RCDATA);
aBitMap := Graphics.TBitmap.Create;
aBitMap.LoadFromStream(aStream);
Assign(aBitMap);
finally
aStream.Free;
aBitMap.Free;
Free;
end;
end;
end;
var
App : WordApplication;
aCommandBar : CommandBar;
aButton : _CommandBarButton;
begin
App := WordApplication(Application);
aCommandBar := App.CommandBars.Add(''''Test'''', msoBarTop, False, True);
aButton := aCommandBar.Controls.Add(msoControlButton, EmptyParam, EmptyParam, EmptyParam, True) as _CommandBarButton;
aButton.Set_Style(msoButtonIconAndCaption);
aButton.Set_Caption(''''Test'''');
//CopyBitMapToClipBoard; //这两句话是给按钮设定一个外部图标,
//aButton.PasteFace; //你要增加一个rcdata的bitmap资源bitmap大小为16*16,具体怎么做请参考其他文档
aButton.Set_Tag(''''test111'''');
FCommandBarButton := TCommandBarButton.Create(nil);
FCommandBarButton.ConnectTo(aButton);
FCommandBarButton.OnClick := FClick;
aCommandBar.Set_Visible(True);
end;

在OnDisconnection写下面代码

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!