How To: Programmatically select and place a marker in the GraphicsContainer
Note: This article pertains to ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
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.
Note: Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward
- Start ArcMap.
- 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'
- Right-click the UIButtonControl and select View Source.
- Copy this code into the UIButtonControl's click event.
- Click AddData to load a layer of sample data.
- From the View Menu, select Layout View.
'''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
- Click the button to run the code.