www.pudn.com > Excel_example.rar > Excel2000Addin.h


///////////////////////////////////////////////////////////////////////////// 
// 类名:CExcel2000Addin 
// 功能:Excel20002K/XP中插件开发 
// 说明:提供五个导入类型库接口涵数(_IDTExtensibility2接口下): 
//       OnConnection()用来装缷插件处理; 
//		 OnDisconnection()用来缷载插件处理; 
//		 OnAddInsUpdate()插件更新时处理; 
//       OnStartupComplete()程序启动结束时处理 
//		 OnBeginShutdown()程序开始关闭时处理 
// 
// 编写:徐景周(jingzhou_xu@163.net) 
// 组织:未来工作室(Future Studio) 
// 日期:2003.4.10 
///////////////////////////////////////////////////////////////////////////// 
#ifndef __EXCEL2000ADDIN_H_ 
#define __EXCEL2000ADDIN_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; 
///////////////////////////////////////////////////////////////////////////// 
// CExcel2000Addin 
class ATL_NO_VTABLE CExcel2000Addin :  
	public CComObjectRootEx, 
	public CComCoClass, 
	public ISupportErrorInfo, 
	public IDispatchImpl, 
	public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>, 
	public IDispEventSimpleImpl<1,CExcel2000Addin,&__uuidof(Office::_CommandBarButtonEvents)> 
{ 
public: 
	typedef IDispEventSimpleImpl CommandButton1Events; 
 
	CExcel2000Addin() 
	{ 
		// 初始值 
		m_spApp = NULL; 
		m_spButton = NULL; 
	} 
 
DECLARE_REGISTRY_RESOURCEID(IDR_EXCEL2000ADDIN) 
 
DECLARE_PROTECT_FINAL_CONSTRUCT() 
 
BEGIN_COM_MAP(CExcel2000Addin) 
	COM_INTERFACE_ENTRY(IExcel2000Addin) 
//DEL 	COM_INTERFACE_ENTRY(IDispatch) 
	COM_INTERFACE_ENTRY(ISupportErrorInfo) 
	COM_INTERFACE_ENTRY2(IDispatch, IExcel2000Addin) 
	COM_INTERFACE_ENTRY(_IDTExtensibility2) 
END_COM_MAP() 
 
// 响应事件映射 
BEGIN_SINK_MAP(CExcel2000Addin) 
	SINK_ENTRY_INFO(1,__uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01,OnClickButton1, &OnClickButtonInfo) 
END_SINK_MAP() 
 
public: 
	void __stdcall OnClickButton1(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault); 
	 
// ISupportsErrorInfo 
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid); 
 
// IExcel2000Addin 
public: 
// _IDTExtensibility2 
	// 装缷插件时处理 
	STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom) 
	{ 
		CComPtr < Office::_CommandBars> spCmdBars;  
 
		// Excel应用接口_Application 
		CComQIPtr spApp(Application); 
		ATLASSERT(spApp); 
		 
		// 获取CommandBars接口 
		HRESULT hr = spApp->get_CommandBars(&spCmdBars); 
		if(FAILED(hr)) 
			return hr; 
		ATLASSERT(spCmdBars); 
 
		// 新增一个工具条及其上一个位图按钮 
		CComVariant vName("新增Excel2000工具条插件"); 
		CComPtr  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;  
		 
		// 用CommandBarControls中的Add方法新增第一个按钮,并让spNewBar指向它 
		spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);  
		ATLASSERT(spNewBar); 
		 
		// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等 
		CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar); 
		ATLASSERT(spCmdButton); 
		 
		// 设置位图按钮风格,位图为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); 
		 
		m_spButton = spCmdButton; 
 
		// 激活新增的工具条按钮的事件连接点 
		m_spApp = spApp; 
	    hr = CommandButton1Events::DispEventAdvise((IDispatch*)m_spButton); 
		if(FAILED(hr)) 
			return hr; 
 
		::EmptyClipboard(); 
 
		return S_OK; 
	} 
 
	// 缷载插件时处理 
	STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom) 
	{ 
		 
		// 断开新增的工具条按钮的事件连接点 
		HRESULT hr = CommandButton1Events::DispEventUnadvise((IDispatch*)m_spButton); 
			if(FAILED(hr)) 
				return hr; 
		 
		m_spButton = NULL; 
		m_spApp = NULL; 
		 
		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 m_spButton;			// 新建工具条按钮1 
 
	CComPtr m_spApp; 
}; 
 
#endif //__EXCEL2000ADDIN_H_