English

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

Summary

Note:
This article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

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

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

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'
  1. Right-click the UIButtonControl and select View Source.
  2. Copy this code into the UIButtonControl's click event.
'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
  1. Add some data to the ArcMap display and click the new UIButtonControl to zoom in.