How to synchronize the MapControl and PageLayoutControl


Summary

A common task for an ArcGIS Engine developer is to synchronize (in the same application) the map used by the MapControl with the focus map of the PageLayoutControl. This topic discusses the methods used to achieve this synchronization.

Synchronizing the MapControl and PageLayoutControl

A common task for an ArcGIS Engine developer is to synchronize (in the same application) the map used by the MapControl with the focus map of the PageLayoutControl. For example, an application may have one tab containing a MapControl and one tab containing a PageLayoutControl, both displaying the same map data. Any changes made to a map in one control must be reflected in the other control when its tab is activated.
Consider the following differences between the ArcMap desktop application and the ArcGIS Engine Controls:
  • The ArcMap application has two views: page layout view and data frame view (also known as map view). These two views share the same map object. For example, if some features are selected in map view, the same feature selection will be visible on the map in page layout view. However, the map object can only be active in one view at any one time; the IActiveView is either a PageLayout or a map.
  • The ArcGIS Engine MapControl and PageLayoutControl do not automatically know of each other's existence within an application; they do not share the same map object. For example, if the map extent is changed in the MapControl, the new map extent will not be reflected in the focus map of the PageLayoutControl.
The synchronization that ArcMap provides can be implemented with the MapControl and PageLayoutControl as follows:
  • Listen to the events fired by a map object when it is changed, both in the MapControl and PageLayoutControl and make the same change in the other control. For example, the IActiveViewEvents::ItemAdded event is fired when a new layer is added to the map in the MapControl. When the event is fired, the same layer needs to be programmatically added to the focus map within the PageLayoutControl. This method of synchronization is not recommended due to the sheer number of events that need to be listened to; to trap all possible changes that can be made to a map object.
  • If a change is made to the focus map of the PageLayoutControl, the ObjectCopy object can be used to copy (deep clone) the map object and the resulting map copy can be set into the MapControl. Each Map contained within the PageLayout encapsulated by the PageLayoutControl, resides within a separate MapFrame, and therefore has its IMap::IsFramed property set to True. A Map contained within the MapControl does not reside within a MapFrame. As such before overwriting the MapControl's map, the IMap::IsFramed property must be set to False. (Failure to do this will lead to corrupted map documents saved containing the contents of the MapControl). This method of synchronization is recommended when copying a map from the PageLayoutControl into the MapControl (see Copy the PageLayoutControl focus map and overwrite the MapControl map), but not in the other direction, when copying a map from the MapControl into the PageLayoutControl. The PageLayoutControl encapsulates the PageLayout object that manages both MapFrame objects containing map objects and objects implementing IMapSurround, such as north arrows and scale bars. Each map surround contains a pointer to the map. If the map is replaced with a map copied from the MapControl, the map surround frames no longer point to a valid map, which can lead to application errors.
  • The recommended method of synchronization to ensure the MapControl and PageLayoutControl reference the same map instance is to either call IPageLayout::ReplaceMaps on the PageLayoutControl or to set IMapControl2::Map on the MapControl. The IPageLayout::ReplaceMaps method expects an object implementing IMaps. As there is no object in the ArcGIS Engine implementing IMaps, you must create your own class implementing IMaps. To ensure there is only one ActiveView, you will need to call IActiveView::Deactivate on the control losing focus and IActiveView::Activate on the control gaining focus in the application. Failure to do this will result in the flickering of the MapControl or PageLayoutControl.






Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced