📄 outlookaddin.h
字号:
/////////////////////////////////////////////////////////////////////////////
// 类名:COutlookAddin
// 功能:Outlook2K中插件开发
// 说明:提供五个导入类型库接口涵数(_IDTExtensibility2接口下):
// OnConnection()用来装缷插件处理;
// OnDisconnection()用来缷载插件处理;
// OnAddInsUpdate()插件更新时处理;
// OnStartupComplete()程序启动结束时处理
// OnBeginShutdown()程序开始关闭时处理
//
// 修改:徐景周(jingzhou_xu@163.net)
// 组织:未来工作室(Future Studio)
// 日期:2003.2.1
/////////////////////////////////////////////////////////////////////////////
#ifndef __OUTLOOKADDIN_H_
#define __OUTLOOKADDIN_H_
#include "resource.h" // main symbols
#import "D:\Program Files\Common Files\Designer\MSADDNDR.DLL" raw_interfaces_only, raw_native_types, no_namespace, named_guids
// 按钮事件响应信息定义
extern _ATL_FUNC_INFO OnClickButtonInfo;
/////////////////////////////////////////////////////////////////////////////
// COutlookAddin
class ATL_NO_VTABLE COutlookAddin :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<COutlookAddin, &CLSID_OutlookAddin>,
public ISupportErrorInfo,
public IDispatchImpl<IOutlookAddin, &IID_IOutlookAddin, &LIBID_OUTLADDINLib>,
public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,
public IDispEventSimpleImpl<1,COutlookAddin,&__uuidof(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl<2,COutlookAddin,&__uuidof(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl<3,COutlookAddin,&__uuidof(Office::_CommandBarButtonEvents)>
{
public:
typedef IDispEventSimpleImpl</*nID =*/ 1,COutlookAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton1Events;
typedef IDispEventSimpleImpl</*nID =*/ 2,COutlookAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton2Events;
typedef IDispEventSimpleImpl</*nID =*/ 3,COutlookAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandMenuEvents;
COutlookAddin()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_OUTLOOKADDIN)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(COutlookAddin)
COM_INTERFACE_ENTRY(IOutlookAddin)
//DEL COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
COM_INTERFACE_ENTRY2(IDispatch, IOutlookAddin)
COM_INTERFACE_ENTRY(_IDTExtensibility2)
END_COM_MAP()
BEGIN_SINK_MAP(COutlookAddin)
SINK_ENTRY_INFO(1, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01, OnClickButton1, &OnClickButtonInfo)
SINK_ENTRY_INFO(2, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01, OnClickButton2, &OnClickButtonInfo)
SINK_ENTRY_INFO(3, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01, OnClickMenu, &OnClickButtonInfo)
END_SINK_MAP()
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
void __stdcall OnClickButton1(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault);
void __stdcall OnClickButton2(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault);
void __stdcall OnClickMenu(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault);
// IOutlookAddin
public:
// _IDTExtensibility2
// 装缷插件时处理
STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{
CComPtr < Office::_CommandBars> spCmdBars;
// Outlook应用接口_Application
CComQIPtr <Outlook::_Application> spApp(Application);
ATLASSERT(spApp);
// 获取CommandBars接口
CComPtr<Outlook::_Explorer> spExplorer;
spApp->ActiveExplorer(&spExplorer);
HRESULT hr = spExplorer->get_CommandBars(&spCmdBars);
if(FAILED(hr))
return hr;
ATLASSERT(spCmdBars);
// 新增一个工具条及其上两个位图按钮
CComVariant vName("新增Outlook2K工具条插件");
CComPtr <Office::CommandBar> spNewCmdBar;
// 新增工具条位置
CComVariant vPos(1);
CComVariant vTemp(VARIANT_TRUE); // 临时
CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
// 用Add方法在指定位置新增一工具条并让spNewCmdBar指向它
spNewCmdBar = spCmdBars->Add(vName, vPos, vEmpty, vTemp);
// 获取新增工具条的CommandBarControls,从而在其上添加按钮
CComPtr < Office::CommandBarControls> spBarControls;
spBarControls = spNewCmdBar->GetControls();
ATLASSERT(spBarControls);
//MsoControlType::msoControlButton = 1
CComVariant vToolBarType(1);
//显示工具条
CComVariant vShow(VARIANT_TRUE);
CComPtr < Office::CommandBarControl> spNewBar;
CComPtr < Office::CommandBarControl> spNewBar2;
// 用CommandBarControls中的Add方法新增第一个按钮,并让spNewBar指向它
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);
// 用CommandBarControls中的Add方法新增第二个按钮,并让spNewBar2指向它
spNewBar2 = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar2);
// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等
CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar);
CComQIPtr < Office::_CommandBarButton> spCmdButton2(spNewBar2);
ATLASSERT(spCmdButton);
ATLASSERT(spCmdButton2);
// 设置位图按钮风格,位图为32x32大小,将其放入剪切板中用PasteFace()贴在指定按钮上
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("按钮1"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("按钮1提示信息"));
spCmdButton->PutTag(OLESTR("按钮1标志"));
// 显示新增工具条
spNewCmdBar->PutVisible(VARIANT_TRUE);
// 设置第二个工具条按钮风格
spCmdButton2->PutStyle(Office::msoButtonIconAndCaption);
// 第二个按钮指定位图为Outlook2K中预先定义的位图
spCmdButton2->PutFaceId(1760);
spCmdButton2->PutVisible(VARIANT_TRUE);
spCmdButton2->PutCaption(OLESTR("按钮2"));
spCmdButton2->PutEnabled(VARIANT_TRUE);
spCmdButton2->PutTooltipText(OLESTR("按钮2提示信息"));
spCmdButton2->PutTag(OLESTR("按钮2标志"));
spCmdButton2->PutVisible(VARIANT_TRUE);
m_spButton = spCmdButton;
m_spButton2 = spCmdButton2;
_bstr_t bstrNewMenuText(OLESTR("新增菜单条"));
CComPtr < Office::CommandBarControls> spCmdCtrls;
CComPtr < Office::CommandBarControls> spCmdBarCtrls;
CComPtr < Office::CommandBarPopup> spCmdPopup;
CComPtr < Office::CommandBarControl> spCmdCtrl;
CComPtr < Office::CommandBar> spCmdBar;
// 通过CommandBar获取Outlook主菜单
hr = spCmdBars->get_ActiveMenuBar(&spCmdBar);
if (FAILED(hr))
return hr;
// 获取菜单条的CommandBarControls
spCmdCtrls = spCmdBar->GetControls();
ATLASSERT(spCmdCtrls);
// 在第5个"工具"菜单下新增一菜单条
CComVariant vItem(5);
spCmdCtrl= spCmdCtrls->GetItem(vItem);
ATLASSERT(spCmdCtrl);
IDispatchPtr spDisp;
spDisp = spCmdCtrl->GetControl();
// 获取菜单条CommandBarPopup接口
CComQIPtr < Office::CommandBarPopup> ppCmdPopup(spDisp);
ATLASSERT(ppCmdPopup);
spCmdBarCtrls = ppCmdPopup->GetControls();
ATLASSERT(spCmdBarCtrls);
CComVariant vMenuType(1); // 控件类型 - menu
CComVariant vMenuPos(6);
CComVariant vMenuEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant vMenuShow(VARIANT_TRUE); // 菜单将显示
CComVariant vMenuTemp(VARIANT_TRUE); // 临时
CComPtr < Office::CommandBarControl> spNewMenu;
// 用Add方法创建新的菜单条
spNewMenu = spCmdBarCtrls->Add(vMenuType, vMenuEmpty, vMenuEmpty, vMenuEmpty, vMenuTemp);
ATLASSERT(spNewMenu);
spNewMenu->PutCaption(bstrNewMenuText);
spNewMenu->PutEnabled(VARIANT_TRUE);
spNewMenu->PutVisible(VARIANT_TRUE);
// 利用CommandBarButton来在菜单条前显示位图
CComQIPtr < Office::_CommandBarButton> spCmdMenuButton(spNewMenu);
ATLASSERT(spCmdMenuButton);
spCmdMenuButton->PutStyle(Office::msoButtonIconAndCaption);
// 同新增工具条第一个按钮位图相同方法
spCmdMenuButton->PasteFace();
// 显示菜单
spNewMenu->PutVisible(VARIANT_TRUE);
m_spMenu = spCmdMenuButton;
// 激活新增的工具条按钮及菜单条的事件连接点
hr = CommandButton1Events::DispEventAdvise((IDispatch*)m_spButton);
if(FAILED(hr))
return hr;
hr = CommandButton2Events::DispEventAdvise((IDispatch*)m_spButton2);
if(FAILED(hr))
return hr;
hr = CommandMenuEvents::DispEventAdvise((IDispatch*)m_spMenu);
if(FAILED(hr))
return hr;
bConnected = true;
return S_OK;
}
// 缷载插件时处理
STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
if(bConnected)
{
// 断开新增的工具条按钮及菜单条的事件连接点
HRESULT hr = CommandButton1Events::DispEventUnadvise((IDispatch*)m_spButton);
if(FAILED(hr))
return hr;
hr = CommandButton2Events::DispEventUnadvise((IDispatch*)m_spButton2);
if(FAILED(hr))
return hr;
hr = CommandMenuEvents::DispEventUnadvise((IDispatch*)m_spMenu);
if(FAILED(hr))
return hr;
bConnected = false;
}
return S_OK;
}
STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom)
{
return E_NOTIMPL;
}
private:
CComPtr<Office::_CommandBarButton> m_spButton; // 新建工具条按钮1
CComPtr<Office::_CommandBarButton> m_spButton2; // 新建工具条按钮2
CComPtr<Office::_CommandBarButton> m_spMenu; // 新建菜单条
bool bConnected;
};
#endif //__OUTLOOKADDIN_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -