public void AddLegend(ESRI.ArcGIS.Carto.IPageLayout pageLayout, ESRI.ArcGIS.Carto.IMap map, System.Double posX, System.Double posY, System.Double legW)
{
 
  if(pageLayout== null || map == null)
  {
    return;
  }
  ESRI.ArcGIS.Carto.IGraphicsContainer graphicsContainer = pageLayout as ESRI.ArcGIS.Carto.IGraphicsContainer; // Dynamic Cast
  ESRI.ArcGIS.Carto.IMapFrame mapFrame = graphicsContainer.FindFrame(map) as ESRI.ArcGIS.Carto.IMapFrame; // Dynamic Cast
  ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
  uid.Value = "esriCarto.Legend";
  ESRI.ArcGIS.Carto.IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame((ESRI.ArcGIS.esriSystem.UID)uid, null); // Explicit Cast

  //Get aspect ratio
  ESRI.ArcGIS.Carto.IQuerySize querySize = mapSurroundFrame.MapSurround as ESRI.ArcGIS.Carto.IQuerySize; // Dynamic Cast
  System.Double w = 0;
  System.Double h = 0;
  querySize.QuerySize(ref w, ref h);
  System.Double aspectRatio = w / h;

  ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
  envelope.PutCoords(posX, posY, (posX * legW), (posY * legW / aspectRatio));
  ESRI.ArcGIS.Carto.IElement element = mapSurroundFrame as ESRI.ArcGIS.Carto.IElement; // Dynamic Cast
  element.Geometry = envelope;
  graphicsContainer.AddElement(element, 0);
}

posted on 2013-03-03 21:26  啄木鸟家园  阅读(3584)  评论(0编辑  收藏  举报