📄 outlookbutton.cpp
字号:
#include "stdafx.h"
#include "OLTools.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COutlookButton::COutlookButton(CComPtr <Office::CommandBarControl> pButton)
{
m_cRef = 0;
m_dwCookie = 0;
m_pButton = NULL;
m_pCP = NULL;
try
{
if (SetupConnection(pButton))
{
m_pButton = pButton;
}
}
catch(...)
{
}
}
COutlookButton::~COutlookButton()
{
ShutDown();
}
STDMETHODIMP_(ULONG) COutlookButton::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) COutlookButton::Release(void)
{
if( m_cRef > 0 )
{
if (0 != --m_cRef)
return m_cRef;
delete this;
}
return 0;
}
STDMETHODIMP COutlookButton::QueryInterface(REFIID riid, void** ppv)
{
if (NULL == ppv)
return E_POINTER;
*ppv = NULL;
HRESULT hr = S_OK;
if ((__uuidof(Office::_CommandBarButtonEvents) == riid) ||
(IID_IUnknown == riid) ||
(IID_IDispatch == riid))
*ppv = static_cast<IDispatch*>(this);
else
hr = E_NOINTERFACE;
if (NULL != *ppv)
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return hr;
}
STDMETHODIMP COutlookButton::GetTypeInfoCount(UINT* pctinfo)
{
if (NULL == pctinfo)
return E_POINTER;
*pctinfo = 0;
return S_OK;
}
STDMETHODIMP COutlookButton::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
{
if (NULL == ppTInfo)
return E_POINTER;
*ppTInfo = NULL;
return E_FAIL;
}
STDMETHODIMP COutlookButton::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
if (IID_NULL != riid)
return DISP_E_UNKNOWNINTERFACE;
if (NULL == rgDispId)
return E_POINTER;
if (NULL == rgszNames)
return E_POINTER;
if (cNames != 1)
return E_INVALIDARG;
*rgDispId = 0x00000000;
return S_OK;
}
STDMETHODIMP COutlookButton::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
if (IID_NULL != riid)
return DISP_E_UNKNOWNINTERFACE;
// The only dispID supported is 1, which triggers when the "button" is clicked
if (dispIdMember != 0x00000001)
return DISP_E_MEMBERNOTFOUND;
if (NULL == pDispParams)
return E_POINTER;
if (pDispParams->cArgs != 2)
return DISP_E_BADPARAMCOUNT;
if( pDispParams->rgvarg[1].vt == VT_DISPATCH )
{
// We can now get at the "button" which triggered this event.
LPDISPATCH pButton = pDispParams->rgvarg[1].pdispVal;
MessageBox(NULL,"The button was clicked", "Button-event", MB_OK);
}
return S_OK;
}
BOOL COutlookButton::SetupConnection(IDispatch *pDisp)
{
IConnectionPointContainer *pCPC = 0;
HRESULT hr = S_OK;
try
{
// Set up the connection and call Advise.
if (FAILED((hr=pDisp->QueryInterface(IID_IConnectionPointContainer,(void **)&pCPC))))
return FALSE;
if (FAILED((hr=pCPC->FindConnectionPoint(__uuidof(Office::_CommandBarButtonEvents),&m_pCP))))
{
pCPC->Release();
return FALSE;
}
if (FAILED((hr=m_pCP->Advise(static_cast<IDispatch*>(this), &m_dwCookie))))
{
pCPC->Release();
return FALSE;
}
pCPC->Release();
}
catch(...)
{
}
return TRUE;
}
void COutlookButton::ShutDown()
{
try
{
m_pCP->Unadvise(m_dwCookie);
m_pCP->Release();
}
catch(...)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -