8. Add a response event for the button I just added:
ATL provides two template classes for the idispatch interface of the COM Object: idispeventimpl <> and idispeventsimpleimpl <>. Select idispeventsimpleimpl <> because it does not need the type library information outside the quota, inherit a class from idispeventsimpleimpl <>:
Class atl_no_vtable caddin:
Public ccomobjectrootex <ccomsinglethreadmodel>,
.....
Public idispeventsimpleimpl <1, caddin,
& __ Uuidof (Office: _ commandbarbuttonevents>
The callback function of the event when the declaration button is clicked:
Void _ stdcall onclickbutton (idispatch */* Office: _ commandbarbutton **/Ctrl, variant_bool * canceldefault );
Use the _ atl_sink_info structure to describe the callback parameter information: Open the caddin. h file and add the following declaration at the top of the file:
Extern _ atl_func_info onclickbuttoninfo; (Be sure to declare it as an external variable)
Open the caddin. cpp file and add the following definitions to it:
_ Atl_func_info onclickbuttoninfo = {cc_stdcall, vt_empty, 2, {vt_dispatch, vt_byref | vt_bool }};
The following describes how to add a button and click an event:
Void _ stdcall caddin: onclickbutton (idispatch *
/* Office: _ commandbarbutton **/Ctrl,
Variant_bool * canceldefault)
{
Uses_conversion;
Ccomqiptr pcommandbarbutton (CTRL );
// The button that raised the event. Do something with this...
MessageBox (null, "clicked button1", "onclickbutton", mb_ OK );
}
Add the following information to the interface ing macro:
Begin_sink_map (caddin)
Sink_entry_info (1, _ uuidof (Office: _ commandbarbuttonevents ),
/* Dispid */0x01,
Onclickbutton, & onclickbuttoninfo)
End_sink_map ()
Finally, call dispeventadvise () and dispeventunadvise () to connect to and disconnect the message source in onconnection () of the caddin class respectively.
Here we have implemented a simple com plug-in to runProgramClick the new button on the toolbar to bring up the ("clicked button1") message box.
If you are familiar with VBA programming, you can use the written macro as the button to respond to events. You only need to call the put_onaction () method of the button:
Spcmdbutton-> put_onaction (olestr ("yourmacroname "));