1

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?

3

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();
    }
}
0

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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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