Method |
Description |
---|---|
OnConnection |
This method is called when the add-in is loaded in Visual Studio. |
OnStartupComplete |
This method is called when Visual Studio finishes loading. |
OnAddInsUpdate |
This method is called when an add-in loads or unloads from Visual Studio. |
OnBeginShutdown |
This method is called when Visual Studio is closed. |
OnDisconnection |
This method is called when the add-in is unloaded from Visual Studio. |
public IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1>
// _IDTExtensibility2 Methods
public:
STDMETHOD(OnConnection)(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
WordAddIn1
WordAddIn1
LoadBehavior | 描述 |
0 | 断开,不加载该控件 |
1 | 连接,加载该控件 |
2 | 启动时加载,主应用程序启动时加载并连接COM加载项 |
3 | 1与2 情况之和 |
8 | 触发加载方式 |
HKCU
{
NoRemove Software
{
NoRemove Microsoft
{
NoRemove Office
{
NoRemove Word
{
NoRemove Addins
{
ATLAddin.MSConnect
{
val Description = s 'ATLAddin Addin'
val FriendlyName = s 'ATLAddin Addin'
val LoadBehavior = d 3
}
}
}
}
}
}
}
public IDispatchImpl
// IRibbonExtensibility Methods
STDMETHOD(GetCustomUI)(BSTR RibbonID, BSTR * RibbonXml)
{
return E_NOTIMPL;
}
#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL" \
raw_interfaces_only, raw_native_types, no_namespace, named_guids, auto_search,\
exclude( "IAccessible"),exclude("DocumentProperties" )
// IRibbonExtensibility Methods
STDMETHOD(GetCustomUI)(BSTR RibbonID, BSTR * RibbonXml)
{
if(!RibbonXml)
return E_POINTER;
*RibbonXml = GetXMLResource(IDR_XML1);
return S_OK;
}
HRESULT HrGetResource( int nId,
LPCTSTR lpType,
LPVOID* ppvResourceData,
DWORD* pdwSizeInBytes)
{
HMODULE hModule = _AtlBaseModule.GetModuleInstance();
if (!hModule)
return E_UNEXPECTED;
HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(nId), lpType);
if (!hRsrc)
return HRESULT_FROM_WIN32(GetLastError());
HGLOBAL hGlobal = LoadResource(hModule, hRsrc);
if (!hGlobal)
return HRESULT_FROM_WIN32(GetLastError());
*pdwSizeInBytes = SizeofResource(hModule, hRsrc);
*ppvResourceData = LockResource(hGlobal);
return S_OK;
}
BSTR GetXMLResource( int nId)
{
LPVOID pResourceData = NULL;
DWORD dwSizeInBytes = 0;
HRESULT hr = HrGetResource(nId, TEXT( "XML"),
&pResourceData, &dwSizeInBytes);
if (FAILED(hr))
return NULL;
// Assumes that the data is not stored in Unicode.
CComBSTR cbstr(dwSizeInBytes, reinterpret_cast(pResourceData));
return cbstr.Detach();
}
SAFEARRAY* GetOFSResource( int nId)
{
LPVOID pResourceData = NULL;
DWORD dwSizeInBytes = 0;
if (FAILED(HrGetResource(nId, TEXT("OFS" ),
&pResourceData, &dwSizeInBytes)))
return NULL;
SAFEARRAY* psa;
SAFEARRAYBOUND dim = {dwSizeInBytes, 0};
psa = SafeArrayCreate(VT_UI1, 1, &dim);
if (psa == NULL)
return NULL;
BYTE* pSafeArrayData;
SafeArrayAccessData(psa, ( void**)&pSafeArrayData);
memcpy(( void*)pSafeArrayData, pResourceData, dwSizeInBytes);
SafeArrayUnaccessData(psa);
return psa;
}
BEGIN_COM_MAP(CMSConnect)
COM_INTERFACE_ENTRY(IMSConnect)
// COM_INTERFACE_ENTRY2(IDispatch, _IDTExtensibility2)
COM_INTERFACE_ENTRY2(IDispatch, IMSConnect)
COM_INTERFACE_ENTRY(_IDTExtensibility2)
COM_INTERFACE_ENTRY(IRibbonExtensibility)
END_COM_MAP()
STDMETHODIMP CMSConnect::onButton1_Click(IDispatch* ribbonControl)
{
// TODO: Add your implementation code here
MessageBoxW(NULL, L "Button1", L"ATL Addin" , MB_OK);
return S_OK;
}
STDMETHODIMP CMSConnect::onButton2_Click(IDispatch* ribbonControl)
{
// TODO: Add your implementation code here
MessageBoxW(NULL, L "Button2", L"ATL Addin" , MB_OK);
return S_OK;
}
STDMETHOD(Invoke)(DISPID dispidMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pdispparams, VARIANT *pvarResult, EXCEPINFO *pexceptinfo, UINT *puArgErr)
{
if(dispidMember == 1)
onButton1_Click(NULL);
else
onButton2_Click(NULL);
return S_OK;
}