English

How To: Programmatically select and place a marker in the GraphicsContainer

Summary

Users interested in programmatically selecting a marker from the StyleGallery can use this code sample. The code shows how to select a marker, set its size and color, and place the marker in the PageLayout.

Procedure

 

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    Note:
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy this code into the UIButtonControl's click event.
  5. Click AddData to load a layer of sample data.
  6. From the View Menu, select Layout View.
Code:
'''This subroutine retrieves a Marker from the Stylegallery without popping up any dialog boxes.
'''The For/Next loop selects the 39th marker and displays it on the pagelayout.

Private Sub UIButtonControl1_Click()
    
    Dim pStyleGallery As IStyleGallery
    Dim pEnum As IEnumStyleGalleryItem
    Dim pStyleGalleryItem As IStyleGalleryItem

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pStyleGallery = pMxDoc.StyleGallery
    
    Set pEnum = pStyleGallery.Items("Marker Symbols", "ESRI.Style", "Default")
    
    pEnum.Reset
    
    ' Based on its index position, this code selects amarker from the StyleGallery.
    ' To see the marker you want:
    '   Click on the Draw ToolBar and select Drawing.
    '   Select DefaultSymbolProperties then click Marker.
    '***** Change the number 39 (below) to the NorthArrow's position in the StyleSelector...
    Dim intX As Integer
    For intX = 1 To 39
        Set pStyleGalleryItem = pEnum.Next
    Next
    
    Dim pMarker As IMarkerSymbol
    Set pMarker = pStyleGalleryItem.Item

    ''This controls the SIZE and COLOR.
    ''**********
    Dim pRGBColor As IRgbColor
    Set pRGBColor = New RgbColor
    pRGBColor.Blue = 255
    
    pMarker.Color = pRGBColor
    pMarker.Size = 48
    ''************
    Call DrawSymbol(pMarker)
End Sub

Public Function TheGeometry() As IGeometry
'''This function creates a geometry that is used by the PageLayout as the
'''position to display the north arrow.
    
    Dim pPoint As IPoint
    Set pPoint = New Point
    ' The X and Y values represent inches.
    pPoint.X = 2
    pPoint.Y = 2
    Set TheGeometry = pPoint

End Function
Public Sub DrawSymbol(pMarker As IMarkerSymbol)
''This subroutine places the element in the PageLayout.

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    Dim pAV As IActiveView
    Set pAV = pMxDoc.ActiveView
    
    Dim pGC As IGraphicsContainer
    Set pGC = pAV
    
    Dim pMarkerElem As IMarkerElement
    Set pMarkerElem = New MarkerElement
    
    pMarkerElem.Symbol = pMarker
    
    Dim pElem As IElement
    Set pElem = pMarkerElem
    
    '''This calls the function to create a location for placing the marker..
     pElem.Geometry = TheGeometry
     
     pGC.AddElement pElem, 0
     pAV.Refresh
    
End Sub
  1. Click the button to run the code.