Office嵌入工具栏的基本写法

VC++编写DLL,在Word 2003中临时地、流氓地加入工具栏一个。当用户单机按钮时,运行DLL中指定代码。本文没有讨论的技术:嵌入其他控件、创建安装程序。
效果是这样的:

程序文件在这里:
http://webdisk.cech.com.cn/download/file_share_3089502.html

[关键技术]
COM、ATL

[过程]
1、建立ATL工程,允许合并代理/存根代码
2、创建一个ATL简单对象,建议支持ISupportErrorInfo,没有也可以
3、为2添加实现接口:Microsoft Add-In Designer 的第一个
4、修改2的.rgs文件:

HKCU
{
Software
{
Microsoft
{
Office
{
Word
{
Addins
{
'wordAddin.tcWordButton'
{
val FriendlyName = s '[tc]WordButton'
val Description = s '[tc]的COM实验-wordAddin.tcWordButton'
val LoadBehavior = d '00000003'
val CommandLineSafe = d '00000000'
}
}
}
}
}
}
}

5、为OnConnection编写实现
(1)(不贴了)
(2)在stdafx.h中添加

#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL" \
rename_namespace("Office") \
rename("RGB", "RBGXL")
// named_guids
//exclude("IFont","IPicture") \
// rename("DocumentProperties", "DocumentPropertiesXL") \
using namespace Office;

#import "C:\Program Files\Microsoft Office\Office11\MSWORD.OLB" \
raw_interfaces_only \
rename("ExitWindows", "wordExitWindows") \
rename_namespace("MSWord")
using namespace MSWord;

6、添加SINK
(1)

BEGIN_SINK_MAP(CtcWordButton)
SINK_ENTRY_INFO(1,__uuidof(Office::_CommandBarButtonEvents),
/*dispid*/ 0x01, OnClickButton1, &OnClickButtonInfo1)
END_SINK_MAP()

(2)当然这个之前ATL_NO_VTABLE那里基类添加一句

public IDispEventSimpleImpl<1, CtcWordButton,&__uuidof(Office::_CommandBarButtonEvents)>

(3)public那里添加一句

typedef IDispEventSimpleImpl<<span style="color: green">/*nID =*/ 1,
CtcWordButton, &__uuidof(Office::_CommandBarButtonEvents)>
CommandButtonEvents1;

7、然后
(1)回到OnConnection

// 创建按钮的事件连接
m_spButton1 = spCmdButton1;
hr = CommandButtonEvents1::DispEventAdvise((IDispatch*)m_spButton1);

(2)到OnDisconnection

// 取消按钮的事件连接
HRESULT hr = CommandButtonEvents1::DispEventUnadvise(
(IDispatch*)m_spButton1);

8、为了显示透明图标
(1)在资源里面建立一个位图,假设叫IDB_BITMAP
(2)然后建立一个Mask位图,内容是背景黑色、前景白色的图片,假设叫IDB_BITMAP_MASK
9、图标有3种方法建立:
(1)利用剪贴版

::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
::CloseClipboard();
::DeleteObject(hBmp);
hr = spCmdButton1->PasteFace();

(2)利用内置图标

spCmdButton1->PutFaceId(1760);

(3)利用PutPicture

// 位图为32x32大小
// hBmpDesc是前景
HBITMAP hBmpDesc =(HBITMAP)::LoadImage(_Module_HINSTANCE, //_Module.GetResourceInstance(),
MAKEINTRESOURCE(IDB_BITMAP),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
PICTDESC pictDesc;
memset(&pictDesc, 0, sizeof(pictDesc));
pictDesc.cbSizeofstruct = sizeof(pictDesc);
pictDesc.picType = PICTYPE_BITMAP;
pictDesc.bmp.hbitmap = hBmpDesc;
CComPtr spPict;
hr = OleCreatePictureIndirect( &pictDesc, IID_IPictureDisp, TRUE, (void**)&spPict );
// hBmpMask是Mask
HBITMAP hBmpMask =(HBITMAP)::LoadImage(_Module_HINSTANCE, //_Module.GetResourceInstance(),
MAKEINTRESOURCE(IDB_BITMAP),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
PICTDESC pictMask;
memset(&pictMask, 0, sizeof(pictMask));
pictMask.cbSizeofstruct = sizeof(pictMask);
pictMask.picType = PICTYPE_BITMAP;
pictMask.bmp.hbitmap = hBmpMask;
CComPtr spPictMask;
hr = OleCreatePictureIndirect( &pictMask, IID_IPictureDisp, TRUE, (void**)&spPictMask );// 粘贴前设置显示风格
spCmdButton1->PutStyle(Office::msoButtonIconAndCaption);
spCmdButton1->PutPicture( spPict );
spCmdButton1->PutMask( spPictMask );

10、响应函数请这样声明

VOID __stdcall CtcWordButton::OnClickButton1
(IDispatch* /*Office::_CommandBarButton* */ Ctrl,
VARIANT_BOOL * CancelDefault)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值