用VC6.0编写Word插件(Office2K、XP、03)
2010-07-15 20:45:04 来源:WEB开发网加完以上代码以后一定要编译一下,看看是否能够成功。引入这两个文件的原因,主要是为了引入一些变量类型,为后面的创建UI作准备。
最后一步,编写代码。在OnConnection加入如下代码:
CComPtr < Office::_CommandBars> spCmdBars;
CComQIPtr <Word::_Application> spApp(Application);
ATLASSERT(spApp);
HRESULT hr = spApp->get_CommandBars(&spCmdBars);
if(FAILED(hr))
return hr;
ATLASSERT(spCmdBars);
CComVariant vName("MyAddin");
CComPtr <Office::CommandBar> spNewCmdBar;
CComVariant vPos(1);
CComVariant vTemp(VARIANT_TRUE);
CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
spNewCmdBar = spCmdBars->Add(vName, vPos, vEmpty, vTemp);
CComPtr < Office::CommandBarControls> spBarControls;
spBarControls = spNewCmdBar->GetControls();
ATLASSERT(spBarControls);
CComVariant vToolBarType(1);
CComVariant vShow(VARIANT_TRUE);
CComPtr < Office::CommandBarControl> spNewBar;
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);
CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar);
ATLASSERT(spCmdButton);
HBITMAP hBmp =(HBITMAP)::LoadImage(_Module.GetResourceInstance(),
MAKEINTRESOURCE(IDB_BITMAP),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
::CloseClipboard();
::DeleteObject(hBmp);
spCmdButton->PutStyle(Office::msoButtonIconAndCaption);
hr = spCmdButton->PasteFace();
if (FAILED(hr))
return hr;
spCmdButton->PutVisible(VARIANT_TRUE);
spCmdButton->PutCaption(OLESTR("myAddin"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("test1"));
spCmdButton->PutTag(OLESTR("test1"));
spNewCmdBar->PutVisible(VARIANT_TRUE);
m_spCmdButton = spCmdButton;
这样,再次打开word,就可以看到如图一所示的界面效果了。
更多精彩
赞助商链接