Click here to Skip to main content
6,596,602 members and growing! (20,892 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » ATL » General     Intermediate

Microsoft Outlook Add-in Tutorial - 2: Addding Events to Button

By chakkaradeepcc

This adds an Event Handler to the Button added in first tutorial.
VC6Win2K, WinXP, ATL, Dev
Posted:23 May 2004
Updated:11 Jun 2004
Views:57,555
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.45 Rating: 3.20 out of 5
3 votes, 25.0%
1
1 vote, 8.3%
2
3 votes, 25.0%
3
2 votes, 16.7%
4
3 votes, 25.0%
5

Introduction

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. The 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 have CAddin.
  3. The third parameter is pointer to IID of the event dispinterface by this class.
  4. Here our dispinterface is Office::_CommandBarButtonEvents. For help, please refer Microsoft Outlook Object Model.

A Simple Shortcut:

Now, we do a simple shortcut 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 doing typedef here. 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 after END_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 form of ATL_FUNC_INFO structure. We do it in Addin.cpp file.

_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 would be our declaration of our OnClickButton function which is added at the top of Addin.h.
    extern _ATL_FUNC_INFO OnClickButtonInfo;
  3. The next and final step is to write our function! So, here is the function, in Addin.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.

Since this is my first step in writing tutorials, please do feel free to send your reviews to chakkaradeepcc@yahoo.com.

Credits:

  1. This tutorial was based on the idea of Amit Dey in his tutorial, Building an Office2K COM add-in with VC++/ATL.

    So all my thanks goes to Mr. Amit Dey who was 'the guru' for me to Add-ins.

  2. And I mention Igor Tandek of Microsoft Corporation for his great help in making me understand the Inner Concepts of Add-ins along with ATL COM.

A small request to Amit Dey....any contact number or address or mail-id?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

chakkaradeepcc


Member
Chakkaradeep, known as Chaks to his friends, hails from the Indian subcontinent.Having done his Masters in Software Engineering, he knows his way around a computer and has sound knowledge of Microsoft technologies.

Chaks is currently working as a Microsoft Developer @ New Zealand.

You can reach him via his blog - http://chakkaradeep.wordpress.com
Occupation: Software Developer
Location: New Zealand New Zealand

Other popular ATL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
QuestionMore buttons make the addin class derivation more longer. Can we avoid deriving addin class from each button? Pinmemberravistrs3:26 5 Feb '09  
QuestionHow to get the DISPID of an event? Pinmemberlakshmi.dj@gmail.com2:07 7 Jan '09  
AnswerRe: How to get the DISPID of an event? PinmemberMember 44032804:39 15 Jan '09  
GeneralDLL giving errors PinmemberCansu15:08 13 Nov '07  
GeneralPopup PinmemberLazyKancha0:49 31 Mar '06  
GeneralRe: Popup Pinmemberchaitanya shah20:58 9 Jul '07  
Generaluse VARIANT_BOOL * CancelDefault? Pinmemberlandmark_csl21:40 8 Mar '06  
GeneralRe: use VARIANT_BOOL * CancelDefault? Pinmemberchaitanya shah21:04 9 Jul '07  
Generalhow to add action event Pinmemberdevom20:15 29 Dec '04  
GeneralRe: how to add action event Pinmemberchaitanya shah21:07 9 Jul '07  
Generalhow to implement dispatch sink interface PinmemberVladimir Forsh0:14 15 Jun '04  
GeneralRe: how to implement dispatch sink interface Pinmemberchaitanya shah21:06 9 Jul '07  
GeneralSorry for the inconvenience PinsussChakkaradeep23:09 4 Jun '04  
GeneralWrong Link to Zip-File PinmemberKurt Muellner21:59 25 May '04  
GeneralRe: Wrong Link to Zip-File Pinmemberchaitanya shah21:09 9 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by chakkaradeepcc
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project