Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In my print preview dialog I have a PageLayoutControl loading the same map as the main view's MapControl. I want the map in the PageLayoutControl to center at the same position as the MapControl. Using Extent is not a solution because the PageLayoutControl has a fixed ratio (square). (I tried using the same extent, but it didn't work at all.)

I tried the following, but CenterAt() didn't do the trick.

  IEnvelope extent = _mapControl.Extent;
  double lXMin, lXMax, lYMin, lYMax;
  lXMin = extent.XMin;
  lXMax = extent.XMax;
  lYMin = extent.YMin;
  lYMax = extent.YMax;

  IPoint centerPoint = new PointClass();
  centerPoint.SpatialReference = _mapControl.SpatialReference;
  centerPoint.X = lXMin + ((lXMax - lXMin) / 2);
  centerPoint.Y = lYMin + ((lYMax - lYMin) / 2);
  _pageLayoutControl.CenterAt(centerPoint);

Neither did Extent.CenterAt() help:

 _pageLayoutControl.Extent.CenterAt(centerPoint);

I don't have MapServer, so I can't use ICenterAndScale. I didn't find a MapDescription in the PageLayoutControl at all. Any hints? Thanks in advance.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Try the following when you switch from the MapControl to the PageLayoutControl or when you show the PageLayout. The application I tested on was using a tab control so...

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPageIndex == 1)
    {
        IPageLayout pageLayout = (IPageLayout)axPageLayoutControl1.ActiveView;
        syncExtent(pageLayout, (IEnvelope)axMapControl1.ActiveView.Extent);
    }
}

private void syncExtent(IPageLayout pageLayout, IEnvelope env)
{
    IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;
    graphicsContainer.Reset();
    IElement element = graphicsContainer.Next();
    while (element != null)
    {
        if (element is IMapFrame)
        {
            IMapFrame mapFrame = (IMapFrame)element;
            IActiveView tmpActiveView = (IActiveView)mapFrame.Map;
            tmpActiveView.Extent = env;
        }
        element = graphicsContainer.Next();
    }
}

Hope this helps!

share|improve this answer
add comment

If you need close integration between the MapControl and PagelayoutControl you should look at the sample in Concept & Samples section in the documentation, See How to synchronize the MapControl and PageLayoutControl

share|improve this answer
    
Thanks. I tried this but I had problems on resetting the MapControl.Map back to it's initial state, when the user changed something (extent, scale...) in the print preview's PageLayoutControl. –  gumo Sep 23 '11 at 10:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.