Adding an event handler for the button (outlook)

Source: Internet
Author: User
Tags microsoft outlook
Document directory
  • Idispeventsimpleimpl <>
  • A simple shortcut:
  • Sink entry:
  • Setting up our callback:
Adding an event handler for the button created in previous tutorial

In the previous tutorials, we have seen how to add a toolbar and thus create a button in Microsoft Outlook. now, in this tutorial, we will see how to invoke an event handler for the button, or in terms of COM, how to advise the button event !.

The idea behind this tutorial is simple, we need to display a message box saying, "button clicked", when we click on the button created. for this, we have to capture the button click event, I. E ., commandbar events. the DISP interface responsible for this is_CommandBarButtonEvents. So now, if we implement this sink interface that will be called by the event source, then our job is over.

Idispeventsimpleimpl <>

So, here we have to create a connection point between our button and interface in simple words.IDispEventSimpleImpl<>Interface provides the connection points for an atl com object. It is also used to implement an event dispinterface. Now, we have to derive our class from this interface. This is done as follows:

class ATL_NO_VTABLE CAddin :     public CComObjectRootEx<CComSingleThreadModel>,    public CComCoClass<CAddin, &CLSID_Addin>,    public IDispatchImpl<IAddin, &IID_IAddin, &LIBID_OUTLOOKADDINLib>,    public IDispatchImpl<_IDTExtensibility2,         &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,    //the derivation is done here    public IDispEventSimpleImpl<1,CAddin,         &__uuidof(Office::_CommandBarButtonEvents)> Parameters:
  1. The first parameter is a unique identifier for the source object.
  2. The second parameter is the user's class, so here we haveCAddin.
  3. The third parameter is pointer to IID of the event dispinterface by this class.
  4. Here our dispinterface isOffice::_CommandBarButtonEvents. For Help, please refer Microsoft Outlook object model.
A simple shortcut:

Now, we do a simple procedure cut to make our job easier. we add this code before the constructor in our file:

public://the added code is here typedef IDispEventSimpleImpl</*nID =*/ 1,     CAddin, &__uuidof(Office::_CommandBarButtonEvents)> CommandButton1Events; CAddin() { }

As we can see, this can help in advising our sink since we are doingtypedefHere. If you get confused here, please do feel free to proceed to the next step.

Sink entry:

In order to move with sinks, we have to have entries for our event notifications to be handled by their proper functions as we have for messages in MFC and win32.

So, we need to enter the sink entry as follows afterEND_COM_MAP()Macro.

BEGIN_SINK_MAP(CAddin)SINK_ENTRY_INFO(1, __uuidof(Office::_CommandBarButtonEvents),0x01,                                OnClickButton, &OnClickButtonInfo)END_SINK_MAP() in SINK_ENTRY_INFO

We have specified our unique identifier, IID identifying the dispatch interface, dispid identifying the specified event, name of the event handler function, and the last parameter corresponds to type information. this is provided in the formATL_FUNC_INFOStructure. We do it inAddin. cppFile.

_ATL_FUNC_INFO OnClickButtonInfo =   {CC_STDCALL,VT_EMPTY,2,{VT_DISPATCH,VT_BYREF | VT_BOOL}}; 
Setting up our callback:
  1. We define our callback as follows:
    void __stdcall       OnClickButton(IDispatch * /*Office::_CommandBarButton**/ Ctrl,       VARIANT_BOOL * CancelDefault); 
  2. Next wocould be our declaration of ourOnClickButtonFunction which is added at the topAddin. h.
    extern _ATL_FUNC_INFO OnClickButtonInfo;
  3. The next and final step is to write our function! So, here is the function, inAddin. cpp.
    void __stdcall CAddin::OnClickButton(IDispatch* Ctrl,                                 VARIANT_BOOL * CancelDefault){    MessageBox(NULL, "U Have Clicked Me,                             Friend", "OnClickButton", MB_OK);}

That's it... we have completed our second tutorial of getting a message box when we press the button... hmm... it looks so simple and sure it is.

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.