How To: Programmatically call the StyleSelector and place selected NorthArrow on PageLayout
This VBA code sample illustrates how to call the StyleSelector programmatically to select a North Arrow. The code then places the selected North Arrow on the PageLayout.
- 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 below code into the UIButtonControl's click event.
Code: '''This code will: '''1:Call up the StyleSelector. '''2:Allow User to select a NorthArrow '''3:Place selected arrow in PageLayout (1,1). (NorthArrow will draw in Red...) Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pAV As IActiveView Dim pGC As IGraphicsContainer Set pGC = pMxDoc.ActiveView Dim pStyleSelector As IStyleSelector Set pStyleSelector = New NorthArrowSelector pStyleSelector.DoModal 0 '''You must use 0 below. I THINK this is because once a NorthArrow is selected '''it is in the first position (0). Dim pUnk As IUnknown Set pUnk = pStyleSelector.GetStyle(0) Dim pArrow As INorthArrow Set pArrow = pUnk '''QI to above class's main interface.... '''A MapSurround is a term referring to "things" that can sit around a '''map. (ScaleBars, Legends, NorthArrows, etc) Dim pMapSurround As IMapSurround Set pMapSurround = pArrow '''Color the NorthArrow red...(just to prove we have access to its properties.... Dim pClr As IRgbColor Set pClr = New RgbColor pClr.Red = 255 pArrow.Color = pClr '''In order to be displayed on a pagelayout, all MapSurrounds must sit in '''a frame (MapsurrounfFrame). Dim pMapSurroundFrame As IMapSurroundFrame '''*********THIS IS NECCESARY******************** '''If this block of code is excluded, the NorthArrow will have re-sizing issues. '''The CreateSurroundFrame ties the MapFrame to the MapsurroundFrame which will '''allow for a correct repositioning when moving/re-sizing. Dim pMapFrame As IMapFrame Set pMapFrame = pGC.FindFrame(pMxDoc.Maps.Item(0)) Dim pUID As UID Set pUID = New UID pUID.Value = "esricore.MarkerNorthArrow" Set pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, pArrow) '''*********************************************** Set pAV = pMxDoc.ActiveView '''QI and set an IElement interface. This interface give you acces to a '''Geometry property. This geometry property (in this case) takes rectangle/envelope '''that represents a location on the PageLayout where the NorthArrow will be stored... Dim pElem As IElement Set pElem = pMapSurroundFrame '''The function named 'TheGeometry' returns an envelope. pElem.Geometry = TheGeometry '''Finally add the element to the graphicsContainer pGC.AddElement pElem, 0 pMxDoc.ActiveView.Refresh
- Copy the below code into the same module as the code for the UIButtonControl.
Code: Public Function TheGeometry() As IGeometry '''This function creates a piece of geometry that is used by the PageLayout as a '''position to display the North Arrow. Dim pEnv As IEnvelope Set pEnv = New Envelope Dim pt1 As IPoint Set pt1 = New Point pt1.x = 1 pt1.Y = 1 Dim pt2 As IPoint Set pt2 = New Point pt2.x = 1.5 pt2.Y = 1.5 pEnv.LowerLeft = pt1 pEnv.UpperRight = pt2 Set TheGeometry = pEnv End Function
- From the ArcMap Menu, select Layout View because the code expects the PageLayout to be the ActiveView.
- Click the button to run the code.