legend looks bad

Discussion created by amie1304 on Jul 28, 2010
I'm writing code to show legend in the layout view with vba, but it looks disorder. what should I do to make it nice?

Private Sub AddLegend()

Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Dim pEnv As IEnvelope
Dim pID As New UID
Dim pMapSurround As IMapSurround
Dim pMarkerNorthArrow As IMarkerNorthArrow
Dim pCharacterMarkerSymbol As ICharacterMarkerSymbol
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.pageLayout
Set pEnv = New Envelope
'Add a legend
'In this case just use the default legend
pEnv.PutCoords 28.3, 5.6, 40.5, 13.8
pID.Value = "esriCarto.Legend"
Set pMapSurround = CreateSurround(pID, pEnv, "Legend", pMxDoc.FocusMap, pMxDoc.pageLayout)
'Refresh the graphics
pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing

End Sub

Private Function CreateSurround(pID As UID, pEnv As IEnvelope, strName As String, pMap As IMap, pPageLayout As IPageLayout) As IMapSurround
Dim pGraphicsContainer As IGraphicsContainer
Dim pActiveView As IActiveView
Dim pMapSurroundFrame As IMapSurroundFrame
Dim pMapSurround As IMapSurround
Dim pMapFrame As IMapFrame
Dim pElement As IElement
'MapSurrounds are held in a MapSurroundFrame
'MapSurroundFrames are related to MapFrames
'MapFrames hold Maps
Set pGraphicsContainer = pPageLayout
Set pMapFrame = pGraphicsContainer.FindFrame(pMap)
Set pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pID, Nothing)
pMapSurroundFrame.mapSurround.Name = strName

'Set the geometry of the MapSurroundFrame to give it a location
'Activate it and add it to the PageLayout's graphics container
Set pElement = pMapSurroundFrame
Set pActiveView = pPageLayout
pElement.Geometry = pEnv
pElement.Activate pActiveView.ScreenDisplay

'Allow the legend frame size to be altered after the legend has been
'added to the GraphicsContainer
Dim PTrack As ITrackCancel
Set PTrack = New CancelTracker
pElement.Draw pActiveView.ScreenDisplay, PTrack

pGraphicsContainer.AddElement pElement, 0
'Re-apply the change to the Legend MapSurroundFrame Geometry
pElement.Geometry = pEnv
Set CreateSurround = pMapSurroundFrame.mapSurround

End Function