0% found this document useful (0 votes)
48 views

VBA+AO常用代码

This document contains code snippets for customizing ArcMap tools and functions for zooming, panning, rotating, and adding data layers. It includes implementations for zoom in/out tools using rubber bands, fixed zoom centered on click point, zoom to selected features or layer extent, pan west, full extent zoom, and an add data dialog box. The code demonstrates how to access the map document and active view to manipulate the map extent and refresh the display upon user input.

Uploaded by

api-27152328
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

VBA+AO常用代码

This document contains code snippets for customizing ArcMap tools and functions for zooming, panning, rotating, and adding data layers. It includes implementations for zoom in/out tools using rubber bands, fixed zoom centered on click point, zoom to selected features or layer extent, pan west, full extent zoom, and an add data dialog box. The code demonstrates how to access the map document and active view to manipulate the map extent and refresh the display upon user input.

Uploaded by

api-27152328
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

VBA+ AO 常用代码

ArcMap 定制
Zoom Pan

Zoomin
功能:放大,通过一跟踪矩形
具体实现:新添一 UIcontrol:zoomin

Private Sub zoomin_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long,
ByVal y As Long)
Dim mxdoc As IMxDocument
Dim pa As IActiveView
Dim pdis As IScreenDisplay
Dim pr As IRubberBand
Dim pEnv As IEnvelope

Set mxdoc = Application.Document


Set pa = mxdoc.FocusMap
Set pdis = pa.ScreenDisplay
Set pr = New RubberEnvelope
Set pEnv = pr.TrackNew(pdis, Nothing)

pa.Extent = pEnv
pa.Refresh
End Sub
FixedZoomin
功能:鼠标点击放大,以点击为中心,放大到原来的 2 倍
具体实现:新添一 UIcontrol:Fixedzoomin

Private Sub fixedzoomin_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As


Long, ByVal y As Long)
Dim mxdoc As IMxDocument 'zoomin clickpoint center,same as
zoomout
Dim pa As IActiveView
Dim pmap As IMap
Set mxdoc = Application.Document
Set pa = mxdoc.FocusMap
Set pmap = mxdoc.FocusMap

Dim pev As IEnvelope


Dim pp As IPoint
Set pp = pa.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

Set pev = pa.Extent


If CDbl(pp.x) <= pev.XMin + pev.Width / 2 And CDbl(pp.y) <= pev.YMin + pev.Height / 2 Then
pev.Width = 2 * (pp.x - pev.XMin)
pev.Height = 2 * (pp.y - pev.YMin)
ElseIf CDbl(pp.x) < pev.XMin + pev.Width / 2 And CDbl(pp.y) > pev.YMin + pev.Height / 2 Then
pev.Width = 2 * (pp.x - pev.XMin)
pev.Height = 2 * (pev.YMax - pp.y)
ElseIf CDbl(pp.x) > pev.XMin + pev.Width / 2 And CDbl(pp.y) > pev.YMin + pev.Height / 2 Then
pev.Width = 2 * (pev.XMax - pp.x)
pev.Height = 2 * (pev.YMax - pp.y)
ElseIf CDbl(pp.x) > pev.XMin + pev.Width / 2 And CDbl(pp.y) < pev.YMin + pev.Height / 2 Then
pev.Width = 2 * (pev.XMax - pp.x)
pev.Height = 2 * (pp.y - pev.YMin)
End If
pev.CenterAt pp
'pev.Expand 0.5, 0.5, True

pa.Extent = pev
pa.Refresh

End Sub
Zoomout
功能:缩小
具体实现:新添一 UIcontrol:zoomout

Private Sub zoomout_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long,
ByVal y As Long)
Dim mxdoc As IMxDocument
Dim pa As IActiveView
Dim pdis As IScreenDisplay
Dim pr As IRubberBand
Dim pEnv As IEnvelope
Dim pp As IPoint

Set mxdoc = Application.Document


Set pa = mxdoc.FocusMap
Set pdis = pa.ScreenDisplay
Set pr = New RubberEnvelope
Set pEnv = pr.TrackNew(pdis, Nothing)
Set pp = New Point

pp.x = (pEnv.XMax + pEnv.XMin) / 2


pp.y = (pEnv.YMax + pEnv.YMin) / 2

pEnv.Width = pa.Extent.Width * pa.Extent.Width / pEnv.Width


pEnv.Height = pa.Extent.Height * pa.Extent.Height / pEnv.Height
pEnv.CenterAt pp
pa.Extent = pEnv
pa.Refresh
End Sub
Zoomtoselect
功能:缩放到选择要素
具体实现:新添一 UIcontrol:zoomtoselect

Private Sub zoomtoselect_Select()


Dim mxdoc As IMxDocument
Dim pa As IActiveView
Dim penumf As IEnumFeature
Dim pf As IFeature
Dim pEnv As IEnvelope
Dim pmap As IMap

Set mxdoc = Application.Document


Set pa = mxdoc.FocusMap
Set pmap = mxdoc.FocusMap
If pmap.SelectionCount > 0 Then
Set penumf = pmap.FeatureSelection
penumf.Reset
Set pf = penumf.Next
Set pEnv = pf.Extent
pa.Extent = pEnv
pa.Refresh
End If
End Sub

ZoomtoLayer
功能:缩放到选择要素
具体实现:新添一 UIcontrol:zoomtoLayer
Dim pGeoDataset As IGeoDataset
Set pGeoDataset = pLayer
pActiveView.Extent = pGeoDataset.Extent
pActiveView.Refresh
Full Extent
功能:全图显示
具体实现:新添一 Module,添加函数:fullextent

Public Sub fullextent()


Dim mxdoc As IMxDocument
Dim pa As IActiveView

Set mxdoc = Application.Document


Set pa = mxdoc.FocusMap

pa.Extent = pa.fullextent

pa.Refresh
End Sub

Rotate
功能:旋转
具体实现:新添一 UIcontrol:Rotate

Private Sub rotate_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long,
ByVal y As Long)
Dim mxdoc As IMxDocument
Dim pa As IActiveView
Dim pdis As IScreenDisplay
Set mxdoc = Application.Document
Set pa = mxdoc.FocusMap
Set pdis = pa.ScreenDisplay
pdis.TrackRotate

End Sub
Pan
功能:漫游
具体实现:新添一 UIcontrol:Pan

Private Sub pan_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long,
ByVal y As Long)
Dim mxdoc As IMxDocument
Dim pa As IActiveView
Dim pdis As IScreenDisplay
Set mxdoc = Application.Document
Set pa = mxdoc.FocusMap
Set pdis = pa.ScreenDisplay
pdis.TrackPan
End Sub

Panwest
功能:向西漫游
具体实现:新添一 UIcontrol:PanWest
Private Sub panwest_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long,
ByVal y As Long)
Const panfactor = 0.25
Dim mxdoc As IMxDocument
Dim pa As IActiveView
Set mxdoc = Application.Document
Set pa = mxdoc.FocusMap
Dim penv As IEnvelope
Set penv = pa.Extent
Dim xmin As Double, xmax As Double, ymin As Double, ymax As Double

xmin = penv.xmin - penv.Width * panfactor


xmax = penv.xmax - penv.Width * panfactor
ymin = penv.ymin
ymax = penv.ymax
If Not checkit(xmin, xmax) Then
xmin = pa.fullextent.xmin
xmax = xmin + penv.Width
End If
penv.PutCoords xmin, ymin, xmax, ymax
pa.Extent = penv
pa.Refresh
End Sub
Private Function checkit(minx As Double, maxx As Double) As Boole an
Dim mxdoc As IMxDocument
Dim pa As IActiveView

Set mxdoc = Application.Document


Set pa = mxdoc.FocusMap
Dim pfullenv As IEnvelope
Set pfullenv = pa.fullextent

If minx >= pfullenv.xmin Then


checkit = True
Else
checkit = False
End If
End Function
File
AddData
功能:添加 Layer
具体实现:新添一 UIcontrol:AddData
Private Sub AddData_Select() 
    Dim pMxDoc As IMxDocument 
    Dim pDlg As IAddDataDialog 
    Dim bOK As Boolean 
     
    Set pMxDoc = ThisDocument 
     
    Set pDlg = New AddDataDialog 
    pDlg.Document = pMxDoc 
    pDlg.Map = pMxDoc.FocusMap 
    pDlg.Show Application.hWnd, bOK 
     
End Sub 

You might also like