⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oltools.cpp

📁 是用MAPI 实现对OUTLOOK 收发邮件的监控
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StdAfx.h"
#include "oltools.h"

COLTools::COLTools(void)
{
	m_ulRefCount = 0;
	m_ulContext  = 0;
	m_bSubmittingMessage = FALSE;
	m_pMyButton  = NULL;
}

COLTools::~COLTools(void)
{
}

STDMETHODIMP COLTools::QueryInterface(REFIID					riid,
									   void**					ppvObj)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	USES_CONVERSION;
	*ppvObj=NULL;
	IUnknown* punk=NULL;
	if (riid == IID_IUnknown)
	{
		punk = (IExchExt*)this;
	}
	else if (riid == IID_IExchExt)
	{
		punk = (IExchExt*)this;
	}
	else if (riid == IID_IExchExtSessionEvents)
	{
		punk = (IExchExtSessionEvents*)this;
	}
	else if (riid == IID_IExchExtMessageEvents)
	{
		punk = (IExchExtMessageEvents*)this;
	}
	else
	{
		return E_NOINTERFACE;
	}
	if (NULL!=punk)
	{
		*ppvObj = punk;
		AddRef();
	}
	return S_OK;
}

STDMETHODIMP_(ULONG) COLTools::AddRef()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	m_ulRefCount++;
	return m_ulRefCount;
}
STDMETHODIMP_(ULONG) COLTools::Release()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	ULONG ulCount = m_ulRefCount--;
	if (!ulCount)
	{
		delete this;
	}
	return ulCount;
}


STDMETHODIMP COLTools::Install(IExchExtCallback	*lpExchangeCallback,
								ULONG		mcontext,
 								ULONG		ulFlags)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	HRESULT hRet = S_FALSE;
	try
	{
		m_ulContext = mcontext;
		GetOutlookApp(lpExchangeCallback, m_OLAppPtr);
		switch(m_ulContext)
		{
			case EECONTEXT_TASK:
				{
					hRet = S_OK;
				}
				break;
			case EECONTEXT_SESSION:
				{
					hRet = S_OK;
				}
				break;
			case EECONTEXT_VIEWER:
				{
					hRet = S_OK;
//					No UI, I leave that for you.
//					Add a button and do some config, I'm sure you have a million good ideas....
//					InstallInterface(lpExchangeCallback);
				}
				break;			
			case EECONTEXT_SENDNOTEMESSAGE:
			case EECONTEXT_SENDPOSTMESSAGE:
			case EECONTEXT_SENDRESENDMESSAGE:
				{
					hRet = S_OK;
				}
				break;
			case EECONTEXT_REMOTEVIEWER:
			case EECONTEXT_SEARCHVIEWER:
			case EECONTEXT_ADDRBOOK:		
			case EECONTEXT_READNOTEMESSAGE:		
			case EECONTEXT_READPOSTMESSAGE:
			case EECONTEXT_READREPORTMESSAGE:
			case EECONTEXT_PROPERTYSHEETS:
			case EECONTEXT_ADVANCEDCRITERIA:
			default:
				{
					hRet = S_FALSE;
				}
				break;

		};
	}
	catch(...)
	{
	}
	return hRet;
}


HRESULT COLTools::GetOutlookApp(IExchExtCallback 		*pmecb,
			     Outlook::_ApplicationPtr 	&rOLAppPtr)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());
   try
   {
	  IOutlookExtCallback *pOutlook = NULL;
      HRESULT hRes = pmecb->QueryInterface(IID_IOutlookExtCallback,(void **) &pOutlook);
      if (pOutlook)
      {
         IUnknown *pUnk = NULL;
         pOutlook->GetObject(&pUnk);
         LPDISPATCH lpMyDispatch;
         if (pUnk != NULL)
         {
            hRes = pUnk->QueryInterface(IID_IDispatch, (void **) &lpMyDispatch);
			pUnk->Release();
         }
         if (lpMyDispatch)
         {
            OLECHAR * szApplication = L"Application";
			DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
			DISPID dspid;
			VARIANT vtResult;
			lpMyDispatch->GetIDsOfNames(IID_NULL, &szApplication, 1, LOCALE_SYSTEM_DEFAULT, &dspid);
			lpMyDispatch->Invoke(dspid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &vtResult, NULL, NULL);
			lpMyDispatch->Release();

			rOLAppPtr= vtResult.pdispVal;
            return S_OK;
         }
      }
   }
   catch(...)
   {
   }
   return S_FALSE;
}

HRESULT COLTools::InstallInterface(IExchExtCallback		*lpExchangeCallback)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	try
	{
		CString csTitle = "My addin"; // This will be the display-name of the toolbar

		// First we need to get the active Explorer, you could see this as the active window (it's more to it but let's keep it at that for now)
		Outlook::_ExplorerPtr spExplorer = NULL;
		m_OLAppPtr->ActiveExplorer(&spExplorer);
		if (spExplorer == NULL)
		{
			return S_FALSE;
		}

		// Then we need to have a pointer to the commandbars, that is the toolbars in the UI
		Office::_CommandBarsPtr spCmdBars = NULL;
		spExplorer->get_CommandBars(&spCmdBars);
		if (spCmdBars == NULL)
		{
			return S_FALSE;
		}

		try 
		{ 
			CComVariant vName(csTitle); // This is the caption of the toolbar
			CComVariant vTemp(TRUE);	
			CComPtr <Office::CommandBar> spToolbar;			
			spToolbar = spCmdBars->Add(vName,		// Caption
									   vtMissing,   // <ignore this for now>
									   vtMissing,   // <ignore this for now>
									   vTemp);      // The toolbar should be temporary, 
													// that way we have to add it every time we

			CComPtr <Office::CommandBarControl>  pButton	  = NULL; 
			CComPtr <Office::CommandBarControls> pBarControls = NULL; 

			pBarControls = spToolbar->GetControls();

			pButton = (Office::CommandBarControlPtr)(pBarControls->Add((long)Office::msoControlButton, 
																	   vtMissing, 
																	   vtMissing, 
																	   vtMissing, 
																	   vTemp));

			pButton->Caption  = "My button";
			pButton->OnAction = "MyButtonsAction";

			BOOL bSetImage = FALSE;
			// I've set this to FALSE, but if you want an image to appear on the button, 
			// you will need to have an image in the resources.
			// The only drawback of this is that there is only one way of 
			// putting an image on the button, and that is by going through the 
			// clipboard. And by doing so, the contents of the clipboard will be
			// lost, unless you implement some logic to store the clipboard-data
			// before doing this and then restoring the data once the image is on 
			// the button.
			if (bSetImage)
			{
				UINT uiBitmapId = 0;
				CComQIPtr <Office::_CommandBarButton> spCmdButton(pButton);

				// 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
				// Outlook's set of predefined bitmap, set button's FaceId to 	
				// the button whose bitmap you want to use
				HBITMAP hBmp =(HBITMAP)::LoadImage(AfxGetInstanceHandle(),
												   MAKEINTRESOURCE(uiBitmapId),
												   IMAGE_BITMAP,
												   0,
												   0,
												   LR_LOADMAP3DCOLORS);
				if (hBmp)
				{
					// put bitmap into Clipboard
					::OpenClipboard(NULL);
					::EmptyClipboard();
					::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);
					::CloseClipboard();
					::DeleteObject(hBmp);	

					// set style before setting bitmap
					spCmdButton->PutStyle(Office::msoButtonIconAndCaption);
					if (FAILED(spCmdButton->PasteFace()))
					{
						TRACE("Failed to paste face to button\n");
					}

					// Empty whatever we put in the clipboard
					::OpenClipboard(NULL);
					::EmptyClipboard();
					::CloseClipboard();
				}
			}
			// With the button physically added to the toolbar, we just need to 
			// create an object of COutlookButton to sink it's events.
			m_pMyButton = new COutlookButton(pButton);

			// The only thing left to do is to make the toolbar visible.
			spToolbar->PutVisible(VARIANT_TRUE); 
		} 
		catch(...)
		{
		}
   }
   catch(...)
   {
   }
   return S_FALSE;
}

STDMETHODIMP COLTools::OnDelivery(IExchExtCallback				*lpExchangeCallback)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	try
	{
		LPMESSAGE		lpMessage = NULL;
		LPMDB			lpMdb = NULL;
		if (SUCCEEDED(lpExchangeCallback->GetObject(&lpMdb, (LPMAPIPROP *)&lpMessage)))
		{

		}
	}
	catch(...)
	{
	}
	return S_FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -