Catching menu clicks in C++?

G

Guest

I'm (trying to) program an addin in C++ (dev studio.net) for Outlook 2000
(and xp and 2003)

How do I catch the event when the user clicks a button within the menu? Is
there a way to DispEventAdvise for the Office::CommandBarControls or the
Office::CommandBarPopup which "owns" the menu items?

My addin has a toolbar, which has a menu (Office::msoControlPopup). I've
filled it with a dynamic number of buttons - that's something I'm going to
read in from a list. I can't inherit from IDispEventSimpleImpl for each menu
item, because I don't know how many buttons at compile time. But, is that
the only way to do it?

I saw an example for a "wrapper class" on outlookcode.com - it sounds like
the right idea for what I'm trying but it's in VB rather than C++.
 
D

Dmitry Streblechenko \(MVP\)

Doing it in C++ is more natural then in VB (I don't use either) -
dynamically instantiate a wrapper class for each added button and add it to
a list. Am I misunderstanding your question?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
G

Guest

Using a wrapper for each and keeping them in a list sounds great, but the
only way I've gotten event handling to work with wrappers is the problem I
think.

This is a summary of what I figured out. All the C++ examples I've found
only showed dealing with one event source at a time, so I got this working by
trial and error.

I'm new to Outlook programming, so I hope that there's a better way. Can
you point out a "newbie" problem with what I've got here?

thanks

class ATL_NO_VTABLE CConnect :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CConnect, &CLSID_Connect>,
public IDispatchImpl<AddInDesignerObjects::_IDTExtensibility2,
&AddInDesignerObjects::IID__IDTExtensibility2,
&AddInDesignerObjects::LIBID_AddInDesignerObjects, 1, 0>,
public IDispEventSimpleImpl<1,CConnect,
&__uuidof(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl<2,CConnect,
&__uuidof(Office::_CommandBarButtonEvents)>
{


typedef IDispEventSimpleImpl</*nID =*/ 1, CConnect,
&__uuidof(Office::_CommandBarButtonEvents)> CommandSandy21Events;
typedef IDispEventSimpleImpl</*nID =*/ 2, CConnect,
&__uuidof(Office::_CommandBarButtonEvents)> CommandSandy22Events;

BEGIN_SINK_MAP(CConnect)
SINK_ENTRY_INFO(1, __uuidof(Office::_CommandBarButtonEvents),0x01,
OnClickButtonSandy21, &OnClickButtonInfo)
SINK_ENTRY_INFO(2, __uuidof(Office::_CommandBarButtonEvents),0x01,
OnClickButtonSandy22, &OnClickButtonInfo)

END_SINK_MAP()

//from cpp:
hr = CommandSandy22Events::DispEventAdvise((IDispatch*)m_spButtonSandy22,
&__uuidof(Office::_CommandBarButtonEvents));
hr = CommandSandy21Events::DispEventAdvise((IDispatch*)m_spButtonSandy21,
&__uuidof(Office::_CommandBarButtonEvents));
 
G

Guest

Yes, those are all the examples I used. But I had to experiment, and that's
when things got strange. All I want is to handle a bunch of (menu) buttons...

I would *prefer* not to inherit from IDispEventSimpleImpl<2,CConnect,
It seems stupid to me too.

What's the right way to do this? Where is my example code wrong? I can
send/post the whole thing header/source, but I'm trying to keep it simple :)
 
D

Dmitry Streblechenko \(MVP\)

If I remember correctly (again, I don't use VC++), the sinks checks that you
don't use it twice to sink events from two COM objects.
As I said in my previous post, do not use the same instance to sink events
from two COM objects. Each COM object must get its own *instance* of the
sink, even if it is implemented by the same C++ class.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


sandy91 said:
Yes, those are all the examples I used. But I had to experiment, and that's
when things got strange. All I want is to handle a bunch of (menu) buttons...

I would *prefer* not to inherit from IDispEventSimpleImpl<2,CConnect,
it told me at runtime during the second call to DispEventAdvise that
m_dwEventCookie was already being used (failed assert about 0xFEFEFEFE). I
figured "m_dwEventCookie comes from IDispEventSimpleImpl, therefore I need
two instances of IDispEventSimpleImpl"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top