English

How To: Create a locator rectangle in a data frame using VBA and ArcObjects

Summary

This article shows how to make a locator rectangle in a data frame showing the visible extent of another data frame.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument module will only run in the current map document. If you want to store the code in all your map documents, open the Normal.mxt ThisDocument code module.

  4. Copy the following into the code module.

    Code:
    Sub GetRect()
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Dim pPageLayout As IPageLayout

    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    Set pPageLayout = pDoc.PageLayout

    Dim pGC As IGraphicsContainer

    Dim pStatesFrame As IMapFrame
    Dim pCitiesFrame As IMapFrame
    Dim pElem As IElement

    ' Get the Map Frames
    Set pGC = pPageLayout
    Set pStatesFrame = pGC.FindFrame(pDoc.Maps.Item(0))
    Set pCitiesFrame = pGC.FindFrame(pDoc.Maps.Item(1))

    ' Create the New Locator Rectangle
    ' The new rectangle shows the extent of the cities layer
    ' on the states layer
    Dim pLocRec As ILocatorRectangle
    Set pLocRec = New LocatorRectangle
    Set pLocRec.MapFrame = pCitiesFrame

    ' Add it to the States Data Frame
    pStatesFrame.AddLocatorRectangle pLocRec
    Debug.Print pStatesFrame.LocatorRectangleCount

    ' Refresh the view
    pDoc.ActiveView.Refresh

    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.