Solved

MS Word Addin C++ COM w/o ATL - Determine what toolbar button was clicked??

Posted on 2007-03-27
3
417 Views
Last Modified: 2013-11-17
I am using C++ and COM without using ATL to write a Word 2003 addin.  I used the MSDN article below as the basis for my for my project.  

http://support.microsoft.com/support/kb/articles/Q230/6/89.ASP

The example only has a single toolbar button so I added several which all get added and initalized fine.  When I click on a button the Invoke method gets called as it should.  My question is how do I tell which button was clicked.  

Help, I am already bald enough...
0
Comment
Question by:craig20120
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18802664
>>My question is how do I tell which button was clicked.

Since the idea is to instantiate a button handler for each individual button, that should be easy to handle:

 // Now, set up an event sink for the Button...
    m_pButtonHandler = new CButtonHandler();  // <------ Option 1: Pass a button ID to the handler in the construcotr
    if (NULL == m_pButtonHandler) goto cleanup;
    m_pButtonHandler->AddRef();
    m_pButtonHandler->SinkEvents(vtButton.pdispVal);

Alternatively, you can do that in the 'Invoke()' handler:

/******************************************************************
 * CButtonHandler::Invoke()
 *
 *  The click event is fired by this Invoke call. There is only
 *  one event for this interface (0x00000001) and it takes two
 *  parameters:
 *
 *  LPDISPATCH   pCtrl   -- The primary interface of the button
 *                          being pressed.
 *
 *  VARIANT_BOOL bCancel -- Determines whether the default action
 *                          should be cancled.
 *
 ******************************************************************/
STDMETHODIMP CButtonHandler::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
    if (IID_NULL != riid)
        return DISP_E_UNKNOWNINTERFACE;

    if (dispIdMember != 0x00000001)
        return DISP_E_MEMBERNOTFOUND;

    if (NULL == pDispParams) return E_POINTER;
    if (pDispParams->cArgs != 2) return DISP_E_BADPARAMCOUNT;

    // <---------- Option 2: Obtain 'pCtrl' from 'pDispParams' so you can simply read the button text


 // We don't do anything special with this event, so we'll just pop up
 // a message box to let the user know we are here. For a production
 // addin, you would want to forward the event to a callback function
 // in your main addin class. There your addin could handle the event
 // in a more controled manner.
    MessageBox(NULL, "The Click event was fired!!", "My Addin", MB_SETFOREGROUND);

    return S_OK;
}
0
 

Author Comment

by:craig20120
ID: 18804082
I like option two the best and actually was actually trying to see how to get the pointer to the control before I posted this.  I figured it was in one of the arguments, but I am still not sure how to retrieve pCtrl and get the text.  I am somewhat new to COM programming.  ATL makes more sense to me then straight COM.  

Thanks!
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 18805034
Hm, like

IDispatch* pButton;

if (pDispParams->rgvarg[0].vt == VT_DISPATCH) {

  pButton = pDispParams->rgvarg[0].pdispVal;

} else {

   // big error, 1st VARIANTARG has to be of type 'VT_DISPATCH' here
}

See also http://msdn2.microsoft.com/en-us/ms891678.aspx ("VARIANT and VARIANTARG")
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question