📄 ppt2kaddin.h
字号:
// PPT2KAddin.h : Declaration of the CPPT2KAddin
#ifndef __PPT2KADDIN_H_
#define __PPT2KADDIN_H_
#include "IEComCtrlSink.h"
#include "resource.h" // main symbols
#import "D:\Program Files\Common Files\designer\MSADDNDR.TLB" raw_interfaces_only, raw_native_types, no_namespace, named_guids
// 按钮事件响应信息定义
extern _ATL_FUNC_INFO OnClickButtonInfo;
/////////////////////////////////////////////////////////////////////////////
// CPPT2KAddin
class ATL_NO_VTABLE CPPT2KAddin :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CPPT2KAddin, &CLSID_PPT2KAddin>,
public ISupportErrorInfo,
public IDispatchImpl<IPPT2KAddin, &IID_IPPT2KAddin, &LIBID_PPTADDINLib>,
public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,
public IDispEventSimpleImpl<1,CPPT2KAddin,&__uuidof(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl<2,CPPT2KAddin,&__uuidof(Office::_CommandBarButtonEvents)>
{
public:
typedef IDispEventSimpleImpl</*nID =*/ 1,CPPT2KAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButtonEvents;
typedef IDispEventSimpleImpl</*nID =*/ 2,CPPT2KAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandMenuEvents;
// IE监听对象数组,将数组大小暂定为200
CIEComCtrlSink* myIE[200];
int IECount;
CPPT2KAddin()
{
IECount=200;
for(int i=0;i<IECount;i++)
myIE[i]=NULL;
}
~CPPT2KAddin()
{ //析构IE监听管理对象
for(int i=0;i<IECount;i++){
delete myIE[i];
}
}
DECLARE_REGISTRY_RESOURCEID(IDR_PPT2KADDIN)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CPPT2KAddin)
COM_INTERFACE_ENTRY(IPPT2KAddin)
//DEL COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
COM_INTERFACE_ENTRY2(IDispatch, IPPT2KAddin)
COM_INTERFACE_ENTRY(_IDTExtensibility2)
END_COM_MAP()
// 响应事件映射
BEGIN_SINK_MAP(CPPT2KAddin)
SINK_ENTRY_INFO(1,__uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01,OnButtonInsertInternetRes, &OnClickButtonInfo)
SINK_ENTRY_INFO(2, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/ 0x01, OnMenuInsertInternetRes, &OnClickButtonInfo)
END_SINK_MAP()
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
public:
void __stdcall OnButtonInsertInternetRes(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault);
void __stdcall OnMenuInsertInternetRes(IDispatch * /*Office::_CommandBarButton**/ Ctrl,VARIANT_BOOL * CancelDefault);
// IPPT2KAddin
public:
// _IDTExtensibility2
// 装缷插件时处理
STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{
CComPtr < Office::_CommandBars> spCmdBars;
// PPT应用接口_Application
CComQIPtr < MSPPT::_Application> spApp(Application);
ATLASSERT(spApp);
// 获取CommandBars接口
HRESULT hr = spApp->get_CommandBars(&spCmdBars);
if(FAILED(hr))
return hr;
ATLASSERT(spCmdBars);
// 新增一个工具栏并在其上添加一个位图按钮
CComVariant vName("插入Internet资源插件");
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;
// 用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_IE),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("Internet资源..."));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("点击打开IE,从IE中插入Internet资源"));
spCmdButton->PutTag(OLESTR("按钮1标志"));
// 显示新增工具栏
spNewCmdBar->PutVisible(VARIANT_TRUE);
m_spButton = spCmdButton;
//添加菜单命令
bstr_t bstrNewMenuText(OLESTR("Internet资源..."));
CComPtr < Office::CommandBarControls> spCmdCtrls;
CComPtr < Office::CommandBarControls> spCmdBarCtrls;
CComPtr < Office::CommandBarPopup> spCmdPopup;
CComPtr < Office::CommandBarControl> spCmdCtrl;
CComPtr < Office::CommandBar> spCmdBar;
hr = spCmdBars->get_ActiveMenuBar(&spCmdBar);
if (FAILED(hr))
return hr;
// 获取菜单栏的CommandBarControls
spCmdCtrls = spCmdBar->GetControls();
ATLASSERT(spCmdCtrls);
// 在“插入”菜单中的第4个菜单下新增一菜单命令
CComVariant vItem(4);
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;
m_spApp = spApp;
//初始化完成插入资源到PowerPoint中的类的Application指针.
CIEComCtrlSink::officeCtrl.m_pPPT =m_spApp;
// 激活新增的工具栏按钮及菜单条的事件连接点
hr = CommandButtonEvents::DispEventAdvise((IDispatch*)m_spButton);
if(FAILED(hr))
return hr;
hr = CommandMenuEvents::DispEventAdvise((IDispatch*)m_spMenu);
if(FAILED(hr))
return hr;
::EmptyClipboard();
return S_OK;
}
// 缷载插件时处理
STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
// 断开新增的工具栏按钮的事件连接点
HRESULT hr = CommandButtonEvents::DispEventUnadvise((IDispatch*)m_spButton);
if(FAILED(hr))
return hr;
hr = CommandMenuEvents::DispEventUnadvise((IDispatch*)m_spMenu);
if(FAILED(hr))
return hr;
m_spButton = NULL;
m_spMenu=NULL;
m_spApp = NULL;
CIEComCtrlSink::officeCtrl.m_pPPT =m_spApp;
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; // 新建工具栏按钮
CComPtr<MSPPT::_Application> m_spApp; //PowerPoint 2000应用程序对象
CComPtr<Office::_CommandBarButton> m_spMenu; // 新建菜单栏
};
#endif //__PPT2KADDIN_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -