HKCU { Software { Microsoft { Office { Word { Addins { ''WordAddin.Addin'' { val FriendlyName = s ''WORD Custom Addin'' val Description = s ''Word Custom Addin'' val LoadBehavior = d ''00000003'' val CommandLineSafe = d ''00000001'' } } } } } } } 5、重新编译(build)该工程注册我们的插件。
// now we add a new toolband to Word // to which we''ll add 2 buttons CComVariant vName("WordAddin"); CComPtr spNewCmdBar;
// position it below all toolbands //MsoBarPosition::msoBarTop = 1 CComVariant vPos(1); CComVariant vTemp(VARIANT_TRUE); // menu is temporary CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
//Add a new toolband through Add method // vMenuTemp holds an unspecified parameter //spNewCmdBar points to the newly created toolband spNewCmdBar = spCmdBars->Add(vName, vPos, vEmpty, vTemp);
//now get the toolband''s CommandBarControls CComPtr < Office::CommandBarControls> spBarControls; spBarControls = spNewCmdBar->GetControls(); ATLASSERT(spBarControls);
_bstr_t bstrNewCaption(OLESTR("Item1")); _bstr_t bstrTipText(OLESTR("Tooltip for Item1"));
// get CommandBarButton interface for each toolbar button // so we can specify button styles and stuff // each button displays a bitmap and caption next to it CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar); CComQIPtr < Office::_CommandBarButton> spCmdButton2(spNewBar2);
// to set a bitmap to a button, load a 32x32 bitmap // and copy it to clipboard. Call CommandBarButton''s PasteFace() // to copy the bitmap to the button face. to use // Word''s set of predefined bitmap, set button''s FaceId to //the // button whose bitmap you want to use HBITMAP hBmp =(HBITMAP)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDB_BITMAP1),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
// put bitmap into Clipboard ::OpenClipboard(NULL); ::EmptyClipboard(); ::SetClipboardData(CF_BITMAP, (HANDLE)hBmp); ::CloseClipboard(); ::DeleteObject(hBmp);
// set style before setting bitmap spCmdButton->PutStyle(Office::msoButtonIconAndCaption); hr = spCmdButton->PasteFace(); if (FAILED(hr)) return hr;
spCmdButton->PutVisible(VARIANT_TRUE); spCmdButton->PutCaption(OLESTR("Item1")); spCmdButton->PutEnabled(VARIANT_TRUE); spCmdButton->PutTooltipText(OLESTR("Tooltip for Item1")); spCmdButton->PutTag(OLESTR("Tag for Item1")); spCmdButton2->put_OnAction(OLESTR("MYMacro1"));
//show the toolband spNewCmdBar->PutVisible(VARIANT_TRUE); spCmdButton2->PutStyle(Office::msoButtonIconAndCaption);
//specify predefined bitmap spCmdButton2->PutFaceId(1758); spCmdButton2->PutVisible(VARIANT_TRUE); spCmdButton2->PutCaption(OLESTR("Item2")); spCmdButton2->PutEnabled(VARIANT_TRUE); spCmdButton2->PutTooltipText(OLESTR("Tooltip for Item2")); spCmdButton2->PutTag(OLESTR("Tag for Item2")); spCmdButton2->PutVisible(VARIANT_TRUE); spCmdButton2->put_OnAction(OLESTR("MYMacro2"));
// a CommandBarPopup interface is the actual menu item CComQIPtr < Office::CommandBarPopup> ppCmdPopup(spDisp); ATLASSERT(ppCmdPopup); spCmdBarCtrls = ppCmdPopup->GetControls(); ATLASSERT(spCmdBarCtrls);
CComVariant vMenuType(1); // type of control - menu CComVariant vMenuPos(6); CComVariant vMenuEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR); CComVariant vMenuShow(VARIANT_TRUE); // menu should be visible CComVariant vMenuTemp(VARIANT_TRUE); // menu is temporary
CComPtr < Office::CommandBarControl> spNewMenu;
// now create the actual menu item and add it spNewMenu = spCmdBarCtrls->Add(vMenuType, vMenuEmpty, vMenuEmpty, vMenuEmpty, vMenuTemp); ATLASSERT(spNewMenu); spNewMenu->PutCaption(bstrNewMenuText); spNewMenu->PutEnabled(VARIANT_TRUE); spNewMenu->PutVisible(VARIANT_TRUE);
//we''d like our new menu item to look cool and display // an icon. Get menu item as a CommandBarButton CComQIPtr < Office::_CommandBarButton> spCmdMenuButton(spNewMenu); ATLASSERT(spCmdMenuButton); spCmdMenuButton->PutStyle(Office::msoButtonIconAndCaption);
// we want to use the same toolbar bitmap for menuitem too. // we grab the CommandBarButton interface so we can add // a bitmap to it through PasteFace(). spCmdMenuButton->PasteFace();
// show the menu spNewMenu->PutVisible(VARIANT_TRUE);