关于 IDispEventSimpleImpl的问题

helloooo 2007-03-12 02:51:11
小弟做了个excel插件
在插件上有4个按纽,点击响应函数分别为 OnClick1 OnClick2 OnClick3 OnClick4
为什么如下的代码使用时出现这样的情况:
按第1,2个按钮正常,分别执行 OnClick1 OnClick2
但按第3个按纽,执行的确是 OnClick2 然后再执行OnClick3 再执行OnClick4
按第4个按钮执行的也很奇怪

代码如下,各位达大帮帮小弟啊

...全文
563 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysjyniiq 2011-08-06
  • 打赏
  • 举报
回复
粗看一下,貌似没有致命错误
关键看
hr = CommandButtonXEvents::DispEventAdvise(XXX);
这里的XXX确认提供了正确连接点
zhangfengmm 2011-08-03
  • 打赏
  • 举报
回复
谢谢你的提问,问我解决这个IDispEventSimpleImpl处理的问题,我按照你的方法修改了一下没有出现问题,可能就是一些细节的疏忽吧
jimly 2008-05-07
  • 打赏
  • 举报
回复
PutTag()的参数改成不同的试试,好象是这个原因
littlegang 2007-03-25
  • 打赏
  • 举报
回复
看看代码似乎没什么错?
helloooo 2007-03-13
  • 打赏
  • 举报
回复
helloooo 2007-03-12
  • 打赏
  • 举报
回复
// 装缷插件时处理
STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{
CComPtr < Office::_CommandBars> spCmdBars;

// Excel应用接口_Application
CComQIPtr<MSExcel::_Application> spApp(Application);
ATLASSERT(spApp);

// 获取CommandBars接口
HRESULT hr = spApp->get_CommandBars(&spCmdBars);
if(FAILED(hr))
return hr;
ATLASSERT(spCmdBars);

// 新增一个工具条及其上一个位图按钮
CComVariant vName("新增Excel2000 插件");
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;

m_spApp = spApp;
/////////////////////////////////// button 1
// 用CommandBarControls中的Add方法新增第一个按钮,并让spNewBar指向它
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);

// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等
CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar);
ATLASSERT(spCmdButton);
// 粘贴前设置显示风格
spCmdButton->PutStyle(Office::msoButtonCaption);
// 设置位图按钮风格,位图为32x32大小,将其放入剪切板中用PasteFace()贴在指定按钮上

spCmdButton->PutVisible(VARIANT_TRUE);
spCmdButton->PutCaption(OLESTR("打开XML"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("kk按钮1提示信息"));
spCmdButton->PutTag(OLESTR("kk按钮1标志"));

// 显示新增工具条
spNewCmdBar->PutVisible(VARIANT_TRUE);

m_spButton1 = spCmdButton;

// 激活新增的工具条按钮的事件连接点
hr = CommandButton1Events::DispEventAdvise((IDispatch*)m_spButton1;
spNewBar.Release();
spCmdButton.Release();
if(FAILED(hr))
return hr;
/////////////////////////////////// button 1
/////////////////////////////////// button 2
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);

// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等
spCmdButton = spNewBar;
ATLASSERT(spCmdButton);
// 粘贴前设置显示风格
spCmdButton->PutStyle(Office::msoButtonCaption);

spCmdButton->PutVisible(VARIANT_TRUE);
spCmdButton->PutCaption(OLESTR("打开工程"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("kk按钮2提示信息"));
spCmdButton->PutTag(OLESTR("kk按钮2标志"));

// 显示新增工具条
spNewCmdBar->PutVisible(VARIANT_TRUE);

m_spButton2 = spCmdButton;

// 激活新增的工具条按钮的事件连接点
hr = CommandButton2Events::DispEventAdvise((IDispatch*)m_spButton2);
spNewBar.Release();
spCmdButton.Release();
if(FAILED(hr))
return hr;
/////////////////////////////////// button 2
/////////////////////////////////// button 3
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);

// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等
spCmdButton = spNewBar;
ATLASSERT(spCmdButton);
// 粘贴前设置显示风格
spCmdButton->PutStyle(Office::msoButtonCaption);

spCmdButton->PutVisible(VARIANT_TRUE);
spCmdButton->PutCaption(OLESTR("匹配"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("kk按钮2提示信息"));
spCmdButton->PutTag(OLESTR("kk按钮2标志"));

// 显示新增工具条
spNewCmdBar->PutVisible(VARIANT_TRUE);

m_spButton3= spCmdButton;

// 激活新增的工具条按钮的事件连接点
hr = CommandButton3Events::DispEventAdvise((IDispatch*)m_spButton3);
spNewBar.Release();
spCmdButton.Release();
if(FAILED(hr))
return hr;
/////////////////////////////////// button 3
/////////////////////////////////// button 4
spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);
ATLASSERT(spNewBar);

// 为每一个按钮指定_CommandBarButton接口,从面可以指定按钮的显示风格等
spCmdButton = spNewBar;
ATLASSERT(spCmdButton);
// 粘贴前设置显示风格
spCmdButton->PutStyle(Office::msoButtonCaption);

spCmdButton->PutVisible(VARIANT_TRUE);
spCmdButton->PutCaption(OLESTR("导出"));
spCmdButton->PutEnabled(VARIANT_TRUE);
spCmdButton->PutTooltipText(OLESTR("kk按钮2提示信息"));
spCmdButton->PutTag(OLESTR("kk按钮2标志"));

// 显示新增工具条
spNewCmdBar->PutVisible(VARIANT_TRUE);

m_spButton4 = spCmdButton;

// 激活新增的工具条按钮的事件连接点
hr = CommandButton4Events::DispEventAdvise((IDispatch*)m_spButton4);
spNewBar.Release();
spCmdButton.Release();
if(FAILED(hr))
return hr;
/////////////////////////////////// button 4

if(!ConnectAppEvents(TRUE))
return S_FALSE;

return S_OK;
}


// 按钮事件响应信息定义
_ATL_FUNC_INFO OnClickButton_Info = {CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}};


void __stdcall CTranExcelAddin::OnClick1(IDispatch* /*Office::_CommandBarButton* */ Ctrl,VARIANT_BOOL * CancelDefault)
{
。。。
}
helloooo 2007-03-12
  • 打赏
  • 举报
回复

#define SINK_BUTTON_EVENTS1 1
#define SINK_BUTTON_EVENTS2 2
#define SINK_BUTTON_EVENTS3 3
#define SINK_BUTTON_EVENTS4 4

#define SINK_EXCELAPP_EVENTS 10

class CTranExcelAddin;

typedef IDispEventSimpleImpl<SINK_BUTTON_EVENTS1, CTranExcelAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton1Events;
typedef IDispEventSimpleImpl<SINK_BUTTON_EVENTS2, CTranExcelAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton2Events;
typedef IDispEventSimpleImpl<SINK_BUTTON_EVENTS3, CTranExcelAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton3Events;
typedef IDispEventSimpleImpl<SINK_BUTTON_EVENTS4, CTranExcelAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton4Events;


typedef IDispEventSimpleImpl<SINK_EXCELAPP_EVENTS, CTranExcelAddin, &IID_IExcelAppEventSink> ExcelAppEvents;

// button事件响应信息定义
extern _ATL_FUNC_INFO OnClickButton_Info;


/////////////////////////////////////////////////////////////////////////////
// CTranExcelAddin
class ATL_NO_VTABLE CTranExcelAddin :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CTranExcelAddin, &CLSID_TranExcelAddin>,
public ISupportErrorInfo,
public IConnectionPointContainerImpl<CTranExcelAddin>,
public IDispatchImpl<ITranExcelAddin, &IID_ITranExcelAddin, &LIBID_TRANSPLUGINLib>,
public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,
public CommandButton1Events,
public CommandButton2Events,
public CommandButton3Events,
public CommandButton4Events,
public ExcelAppEvents,
public TTransDataMgr<CTranExcelAddin>
{
public:
CTranExcelAddin()
{


// 响应事件映射
BEGIN_SINK_MAP(CTranExcelAddin)
//button event
SINK_ENTRY_INFO(SINK_BUTTON_EVENTS1,__uuidof(Office::_CommandBarButtonEvents),0x01,OnClick1, &OnClickButton_Info)
SINK_ENTRY_INFO(SINK_BUTTON_EVENTS2,__uuidof(Office::_CommandBarButtonEvents),0x01,OnClick2, &OnClickButton_Info)
SINK_ENTRY_INFO(SINK_BUTTON_EVENTS3,__uuidof(Office::_CommandBarButtonEvents),0x01,OnClick3, &OnClickButton_Info)
SINK_ENTRY_INFO(SINK_BUTTON_EVENTS4,__uuidof(Office::_CommandBarButtonEvents),0x01,OnClick4, &OnClickButton_Info)

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧