This document is archived and information here might be outdated.  Recommended version.


Add Legend Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Add Legend Snippet

Add a Legend to the Page Layout from the Map.

[C#]
///<summary>Add a Legend to the Page Layout from the Map.</summary>
///
///<param name="pageLayout">An IPageLayout interface.</param>
///<param name="map">An IMap interface.</param>
///<param name="posX">A System.Double that is X coordinate value in page units for the start of the Legend. Example: 2.0</param>
///<param name="posY">A System.Double that is Y coordinate value in page units for the start of the Legend. Example: 2.0</param>
///<param name="legW">A System.Double that is length in page units of the Legend in both the X and Y direction. Example: 5.0</param>
/// 
///<remarks></remarks>
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);
}
[Visual Basic .NET]
'''<summary>Add a Legend to the Page Layout from the Map.</summary>
'''
'''<param name="pageLayout">An IPageLayout interface.</param>
'''<param name="map">An IMap interface.</param>
'''<param name="posX">A System.Double that is X coordinate value in page units for the start of the Legend. Example: 2.0</param>
'''<param name="posY">A System.Double that is Y coordinate value in page units for the start of the Legend. Example: 2.0</param>
'''<param name="legW">A System.Double that is length in page units of the Legend in both the X and Y direction. Example: 5.0</param>
''' 
'''<remarks></remarks>
Public Sub AddLegend(ByVal pageLayout As ESRI.ArcGIS.Carto.IPageLayout, ByVal map As ESRI.ArcGIS.Carto.IMap, ByVal posX As System.Double, ByVal posY As System.Double, ByVal legW As System.Double)

  If pageLayout Is Nothing OrElse map Is Nothing Then

    Return

  End If

  Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer=TryCast(pageLayout, ESRI.ArcGIS.Carto.IGraphicsContainer) ' Dynamic Cast
  Dim mapFrame As ESRI.ArcGIS.Carto.IMapFrame=TryCast(graphicsContainer.FindFrame(map), ESRI.ArcGIS.Carto.IMapFrame) ' Dynamic Cast
  Dim uid As ESRI.ArcGIS.esriSystem.IUID=New ESRI.ArcGIS.esriSystem.UIDClass
  uid.Value="esriCarto.Legend"
  Dim mapSurroundFrame As ESRI.ArcGIS.Carto.IMapSurroundFrame=mapFrame.CreateSurroundFrame(CType(uid, ESRI.ArcGIS.esriSystem.UID), Nothing) ' Explicit Cast

  'Get aspect ratio
  Dim querySize As ESRI.ArcGIS.Carto.IQuerySize=TryCast(mapSurroundFrame.MapSurround, ESRI.ArcGIS.Carto.IQuerySize) ' Dynamic Cast
  Dim w As System.Double=0
  Dim h As System.Double=0
  querySize.QuerySize(w, h)
  Dim aspectRatio As System.Double=w / h

  Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope=New ESRI.ArcGIS.Geometry.EnvelopeClass
  envelope.PutCoords(posX, posY, (posX * legW), (posY * legW / aspectRatio))
  Dim element As ESRI.ArcGIS.Carto.IElement=TryCast(mapSurroundFrame, ESRI.ArcGIS.Carto.IElement)
  element.Geometry=envelope
  graphicsContainer.AddElement(element, 0)

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System