Command Implementing Events


This sample illustrates how you can respond to events from your command. Here, the command responds to IDocumentEvents and IActiveViewEvents. It begins listening when the command is clicked (ICommand::OnClick), and stops listening when the document is closed (IDocumentEvents::CloseDocument).

As there is a definite point when the command is disconnected from the event sources, and as this point occurs before the application tries to destroy the command, there is no need for an intermediate sink object.

How to use:

    Register the command:

  1. Open this sample's workspace and change the paths to esriCore.olb and ArcCATIDs.h in StdAfx.h so that they point to your ArcGIS installation.
  2. Build this sample's DLL. This will also register the command in the appropriate component category.

    Use the command:

  1. In ArcMap's Customize dialog, under the Commands tab, select the 'Command Implementing Events' command from the Developer Samples category, and drag it on to a toolbar. Dismiss the Customize dialog.
  2. Click on the command. A message box comes up informing you that the command is listening to events.
  3. Trigger Active View and Document events by adding data, switching to layout view, etc.
  4. When you trigger an event by your actions in ArcMap, you will get a message box from the command with details of the event.
  5. When you close the document, the command disconnects from the event sources.


Difficulty: Intermediate

Visual C++
File Description
TestCommand.cpp Implementation file for the Command Implementing Events.
TestCommand.h Header file for the Command Implementing Events.
TestCommand.rgs Registry script file for the Command Implementing Events.
CommandImplementingEvents.dsp Project file.
CommandImplementingEvents.dsw Workspace file.
CommandImplementingEvents.dll Compiled DLL for project.
CommandImplementingEvents.cpp Implementation of DLL Exports.
CommandImplementingEvents.def Module Definition file, listing exported functions.
CommandImplementingEvents.idl The IDL file for the sample.
CommandImplementingEvents.rc Resource file.
CommandImplementingEvents.mak Make file for Project.
CommandImplementingEventsps.def Module definition file for proxy-stub Code. Make file for proxy-stub code.
resource.h Resource file.
StdAfx.h Header file that includes standard system include files.
StdAfx.cpp Source file that includes just the standard includes.

Key Interfaces: ICommand, IApplication, IDocument, IDocumentEvents, IActiveViewEvents