Dela via


ATLEventHandling Sample: Handles Word Events Using IDispEventImpl and IDispEventSimpleImpl

The ATLEventHandling sample demonstrates the use of IDispEventImpl and IDispEventSimpleImpl to handle events fired by Microsoft Word.

The sample consists of an ATL project that includes two COM classes: CSimple and CNotSoSimple. These classes represent COM objects that display a message box in response to document change events fired by Microsoft Word. CSimple shows how to handle events using ATL's IDispEventSimpleImpl class template, and CNotSoSimple shows how to handle events using ATL's IDispEventImpl class template. The code in the two classes is identical except for the differences caused by the use of the different ATL templates.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

Note

This sample requires that Microsoft Office 2000 be installed. If you want to use this sample with other versions of Microsoft Office, you must specify the path to the mso dll file referenced in the StdAfx.h file under the ATLEventHandling directory. For example, Office 2000 makes use of mso9.dll and Office XP makes use of mso.dll.

To build and run this sample

  1. Open the solution file ATLEventHandling.sln.

  2. If you are using Excel 2000, modify the following line in StdAfx.h:

    #define OFFICE_VER OFFICEXP
    

    to instead be:

    #define OFFICE_VER OFFICE2000
    
  3. From the Build menu, click Build Solution.

  4. Run MFCClient.exe.

You'll see a dialog box with a drop-down list allowing you to run the test using IDispEventImpl or IDispEventSimpleImpl. Select one of these items, and then click the Start button. If Microsoft Word is installed on your computer, a new instance will be loaded and the test component will start to receive events sent by Word whenever the active document changes.

You can trigger events by creating new documents, loading documents, or switching to a different document using Word's Window menu. Each time the document changes, a message box will be displayed indicating which component is receiving the events and the name of the newly active document. You can disconnect the event handler from the instance of Word by using the Stop button on the MFCClient dialog box, by changing the selection of the drop-down list, or by closing Word.

Close the sample application using the OK or Cancel command button or the dialog box's Close button.

Keywords

This sample uses the following keywords:

_ATL_FUNC_INFO, BEGIN_SINK_MAP, END_SINK_MAP, SINK_ENTRY_EX, SINK_ENTRY_INFO, IDispEventImpl, IDispEventSimpleImpl, IDispEventSimpleImpl::DispEventAdvise, IDispEventSimpleImpl::DispEventUnadvise

See Also

Other Resources

ATL Samples