[GIS] AxMapControl: How to set map extent in ArcObjects

arcgis-10.0arcgis-enginearcobjectsc

I have a stand-alone application written in C#. I want to be able to save and restore the current map view (extent) that the user sees. I assumed that it would be as simple as saving the active view's extent and then later resetting it, but that doesn't seem to work.

Here is some code:

_myModel.Envelope = _axMapControl.ActiveView.Extent; // save it 

_axMapControl.Extent = _myModel.Envelope; // doesn't work
_axMapControl.ActiveView.Extent = _myModel.Envelope; // also doesn't work

_axMapControl.Refresh(); // doesn't do anything
_axMapControl.ActiveView.Refresh(); // doesn't do anything
_axMapControl.ActiveView.ContentsChanged();  // doesn't do anything

Note that after the assignments, both _axMapControl.Extent and _axMapControl.ActiveView.Extent values are unchanged.

Best Answer

Since there has been some activity on this question today, it reminded me that I never followed-up. Here is the implementation that I used:

To save ...

void _axMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
   UpdateMapViewState((IEnvelope) e.newEnvelope);
}


private void UpdateMapViewState(IEnvelope newEnvelope)
{
   IPoint pt = new Point();
   pt.X = ((newEnvelope.XMax - newEnvelope.XMin) / 2.0) + newEnvelope.XMin;
   pt.Y = ((newEnvelope.YMax - newEnvelope.YMin) / 2.0) + newEnvelope.YMin;

   _model.MapCenter = pt; 
   _model.MapScale = _axMapControl.MapScale;
}

To restore ...

_axMapControl.CenterAt(_model.MapCenter);
_axMapControl.MapScale = _model.MapScale;
_axMapControl.Refresh();
Related Question