English

How To: Create a button in ArcMap that zooms a specified amount

Summary

This article shows how to create a new UIButtonControl that zooms in a specified amount.

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.
     
    Code:
    'Set the Zoom factor
    Dim zoomAmount As Integer
    zoomAmount = 2
    
    Dim pMxApp As IMxApplication
    Dim pMxDoc As IMxDocument
    Dim pDisp As IScreenDisplay
    Dim pPoint As IPoint
    Dim pCenterPoint As IPoint
    'Get the display
    Set pMxApp = Application
    Set pDisp = pMxApp.Display
    Set pMxDoc = Document
    
    'Get the current extent envelope (rectangle)
    'and another envelope to change
    Dim pCurrentEnv As IEnvelope
    Dim pEnv As IEnvelope
    Set pCurrentEnv = pMxDoc.ActiveView.Extent.Envelope
    Set pEnv = pMxDoc.ActiveView.Extent.Envelope
    
    'Set the height and width of the new
    'envelope to half the original
    
    pEnv.Height = pCurrentEnv.Height / zoomAmount
    pEnv.Width = pCurrentEnv.Width / zoomAmount
        
    'Get and set the center point of the new envelope
    'equal to the center point of the existing envelope.
    Set pPoint = New Point
    Set pCenterPoint = New Point
    pCenterPoint.X = pCurrentEnv.LowerLeft.X + _
         pCurrentEnv.Width / 2
    pCenterPoint.Y = pCurrentEnv.LowerLeft.Y + _
         pCurrentEnv.Height / 2
    pEnv.CenterAt pCenterPoint
    
    'Set the view extent to the new envelope
    'and refresh the view
    pMxDoc.ActiveView.Extent = pEnv
    pMxDoc.ActiveView.Refresh
  5. Add some data to the ArcMap display and click the new UIButtonControl to zoom in.