ILegend2 legend = mapSurroundFrame.MapSurround as ILegend2; IBoundsProperties bounds = legend as IBoundsProperties; bounds.FixedAspectRatio = false;
When I run the code, I get "The method or operation is not implemented."
You need a concrete Legend object to call fixedAspectRatio.
What I haven't attempted yet but may try is using IQuerySize to get the updated size in points for the legend, convert to page layout units, and assign that as the new geometry for the map surround frame element.
// Update size of element containing the legend // av is IActiveView on an instance of the page layout IQuerySize querySize = mapSurroundFrame.MapSurround as IQuerySize; Double w = 0; Double h = 0; querySize.QuerySize(ref w, ref h); // Convert from points to page units, e.g., inches w = av.ScreenDisplay.DisplayTransformation.FromPoints(w); h = av.ScreenDisplay.DisplayTransformation.FromPoints(h); IEnvelope envelope = new EnvelopeClass(); // The legend is anchored at x=1 inch, y=2 inch on the layout envelope.PutCoords(1, 2, (1 + w), (2 + h)); IElement element = mapSurroundFrame as IElement; element.Geometry = envelope;
mapSurroundFrame.MapSurround returns an instance of the Legend coclass
"The MapSurround property allows you to retrieve or set the surround object (north arrow, legend, or scale bar) stored within the frame."You are getting a "surround object" that is stored within the frame, it could be any of the CoClasses that implement the IMapSurround Interface.
// Initialize a legend for the focus map IGraphicsContainer gc = ArcMap.Document.PageLayout as IGraphicsContainer; IActiveView av = gc as IActiveView; IMap map = av.FocusMap; IMapFrame mapFrame = gc.FindFrame(map) as IMapFrame; ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass(); uid.Value = "esriCarto.Legend"; IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame((ESRI.ArcGIS.esriSystem.UID)uid, null); // Size it IQuerySize querySize = mapSurroundFrame.MapSurround as IQuerySize; Double w = 0; Double h = 0; querySize.QuerySize(ref w, ref h); Double aspectRatio = w / h; IEnvelope envelope = new EnvelopeClass(); // The legend is anchored with bottom left corner at x=1, y=2 // Not sure why aspect ratio matters -- just following Esri's example envelope.PutCoords(1, 2, (1 + 1), (2 + 1 / aspectRatio)); IElement element = mapSurroundFrame as IElement; element.Geometry = envelope; // Get the legend object and do something with it ILegend2 legend = mapSurroundFrame.MapSurround as ILegend2; // ... some work ... now assume the legend size has changed // Update size of element containing the legend querySize.QuerySize(ref w, ref h); // Convert from points to page units, e.g., inches w = av.ScreenDisplay.DisplayTransformation.FromPoints(w); h = av.ScreenDisplay.DisplayTransformation.FromPoints(h); envelope.PutCoords(1, 2, (1 + w), (2 + h)); element.Geometry = envelope; // Now add the element to the layout. // If you add it earlier, from my experience, sometimes // the new geometry you assign doesn't stick gc.AddElement(element, 0);
If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
Your original post wanted to call: FixedAspectRatio
That method is not implemented in the ILegend, IBoundsProperties or the other Interfaces you have used to create an instance of a legend using IMapSurroundFrame.CreateSurroundFrame and passing the uid of a Legend.
I see a lot of people on this forum, especially in the .NET user group, assign interface types to other interface variables. I honestly don't know how that works or why more people don't run into problems doing that.
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
I also hate the way any time they need to add a new member to an interface, they just create a whole new interface ("IGeometry needs a new XYZ property? Say hello to IGeometry16!" ), which half the time doesn't even inherit from the previous interface!