Synchronizing the MapControl and PageLayoutControl
In an application, you 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 features are selected in map view, the same feature selection is visible on the map in page layout view. However, the map object can only be active in one view at a 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 in 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 is not 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 in the MapControl or 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 in the PageLayoutControl. This method of synchronization is not recommended due to the sheer number of events that need to be listened to for trapping 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 to the MapControl. Each map contained in the PageLayout encapsulated by the PageLayoutControl, resides in a separate MapFrame, and has its IMap.IsFramed property set to true. A Map contained in the MapControl does not reside in a MapFrame. Consequently, before overwriting the MapControl's map, the IMap.IsFramed property must be set to false. (Failure to do this leads 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 when copying a map from the MapControl to 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 methods 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, 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.
The sample Synchronized MapControl and PageLayoutControl application synchronizes the map used by the MapControl and the focus map of the PageLayoutControl using a ControlsSynchronizer class that is implemented as previously described. To use this class in custom applications, follow these steps:
- Create an instance of ControlsSynchronizer and pass in the MapControl and PageLayoutControl.
- Call the BindControls method.
- Call the AddFrameworkControl method to add any framework controls (ToolbarControl or TOCControl) to work with the MapControl and PageLayoutControl.
- Call ActivateMap when switching to the MapControl, and call ActivatePageLayout when switching to the PageLayoutControl.
Development licensing | Deployment licensing |
---|---|
Engine Developer Kit | ArcGIS for Desktop Basic |
ArcGIS for Desktop Standard | |
ArcGIS for Desktop Advanced | |
Engine |