How to add a map surround to a page layout


This example adds legend map surround to a page layout and fills the legend with the layers of the map. Map surrounds are dynamically linked to their associated map; therefore, any changes to the map are reflected in the map surround.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the Click event of a UIButtonControl in ArcMap, and execute the command when in Page View:
[VBA]
Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView

Set pMxDoc = ThisDocument
Set pActiveView = pMxDoc.PageLayout

Dim pGraphicsContainer As IGraphicsContainer
Dim pMapFrame As IMapFrame
Set pGraphicsContainer = pMxDoc.PageLayout
Set pMapFrame = pGraphicsContainer.FindFrame(pMxDoc.FocusMap)

Dim pMapSurroundFrame As IMapSurroundFrame
Dim pUID As New UID
Dim pElement As IElement
pUID.Value = "esriCarto.Legend"
Set pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, Nothing)
pMapSurroundFrame.MapSurround.Name = "Legend"

Set pElement = pMapSurroundFrame
Dim pMainMapElement As IElement
Dim pMainEnv As IEnvelope
Set pMainMapElement = pMapFrame
Set pMainEnv = pMainMapElement.Geometry.Envelope

Dim pEnv As IEnvelope
Set pEnv = New Envelope
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 esriViewGraphics, Nothing, Nothing