Select to view content in your preferred language

legend in page layout

943
2
12-07-2010 07:59 PM
YashikaSareen
Emerging Contributor
Hi

I want to add a legend to the page layout in arcmap.I got the code as:

IMap map = activeView.FocusMap;
pageLayout = new PageLayoutClass();
IGraphicsContainer graphicsContainer = pageLayout as IGraphicsContainer;
IFrameElement frameElement = graphicsContainer.FindFrame(map);
IMapFrame mapFrame = (IMapFrame)frameElement;
IMapSurroundFrame mapSurroundFrame = new MapSurroundFrameClass();
IMapSurround pMapsurround;
UID elementUID = new UIDClass();

//The value determines the type of MapSurroundFrame being added.
elementUID.Value = "esriCarto.Legend";
MessageBox.Show(elementUID.Value.ToString());
//The CreateSurroundFrame method takes the UID of the element and an optional style.
mapSurroundFrame = mapFrame.CreateSurroundFrame(elementUID,null); //pMapsurround = mapSurroundFrame.MapSurround;

mapSurroundFrame.MapSurround.Name = "Legend";

//Cast the MapSurroundFrame as an element so it can be inserted into the page layout.
IElement doc_Element = mapSurroundFrame as IElement;
IElement mainMap_Element = mapFrame as IElement;
IGeometry geometry = mainMap_Element.Geometry;
IEnvelope mainMap_Envelope = geometry.Envelope;
MessageBox.Show("start3");
IEnvelope envelope = new EnvelopeClass();
double xMin = mainMap_Envelope.XMax + 1.5;
double yMin = mainMap_Envelope.YMin + 1.5;
double xMax = mainMap_Envelope.XMax - 1.5;
double yMax = mainMap_Envelope.YMax - 1.5;
envelope.PutCoords(xMin, yMin, xMax, yMax);

doc_Element.Geometry = envelope as IGeometry;
doc_Element.Activate(activeView.ScreenDisplay);
graphicsContainer.AddElement(doc_Element, 0);

activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);


but while implementing the above code i get error in highlighted line
mapSurroundFrame = mapFrame.CreateSurroundFrame(elementUID,null);

the code breaks here resulting in no output.Please help!
Yashika
0 Kudos
2 Replies
Venkata_RaoTammineni
Regular Contributor
Hi,

Please find the code...working

  public override void OnClick()
        {
            try
            {
                IMxDocument pMxDoc = m_application.Document as IMxDocument;

                IActiveView pActiveView;

                pActiveView = pMxDoc.PageLayout as IActiveView;

                IGraphicsContainer pGraphicsContainer;

                IMapFrame pMapFrame;

                pGraphicsContainer = pMxDoc.PageLayout as IGraphicsContainer;

                pMapFrame = pGraphicsContainer.FindFrame(pMxDoc.FocusMap) as IMapFrame;

                IMapSurroundFrame pMapSurroundFrame;

                UID pUID = new UIDClass();

                IElement pElement;

                pUID.Value = "esriCarto.Legend";

                pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null);

                pMapSurroundFrame.MapSurround.Name = "Legend";

                pElement = pMapSurroundFrame as IElement;

                IElement pMainMapElement;

                IEnvelope pMainEnv;

                pMainMapElement = pMapFrame as IElement;

                pMainEnv = pMainMapElement.Geometry.Envelope;

                IEnvelope pEnv = new EnvelopeClass();

                pEnv.PutCoords(pMainEnv.XMax + 1.5, pMainEnv.YMin + 1.5, pMainEnv.XMax - 1.5, pMainEnv.YMax - 1.5);

                pElement.Geometry = pEnv;

                pElement.Activate(pActiveView.ScreenDisplay);

                pGraphicsContainer.AddElement(pElement, 0);

                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                pActiveView.Refresh();
            }
            catch (Exception exe) { }
        }

Thanks and Regards,

Venkat
0 Kudos
YashikaSareen
Emerging Contributor
Done its working now...i didn't set the page layout...
0 Kudos