Use VC ++ ATL to develop the Office com plug-in (3)

Source: Internet
Author: User
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 "));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.