AE中toccontrol並不支援檔案拖動載入,我寫了一個類,可以支援toccontrol的檔案載入,使用方法很簡單
首先在form的load事件中加入:
Dim pC As New DragDropFilesTOCControl
pC.DragDropAxTOCControl = AxTOCControl1
pC.DragDropLoad()
此時就可以向toccontrol中拖動檔案,如shp、dem、pic等等
它就會載入到toccontrol綁定的map中
在form的close事件中加入:
pC.DragDropUnLoad()
該類繼承了http://www.cnblogs.com/wall/archive/2008/12/31/1366251.html這篇文章中的DragDropFiles類,代碼如下
當然也可以寫一些其他控制項的DragDrop類,繼承DragDropFiles類。
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.esriSystem
''' <summary>
''' toccontrol拖動檔案類
''' </summary>
''' <remarks></remarks>
Public Class DragDropFilesTOCControl
Inherits DragDropFiles
Private m_AxTOCControl As AxTOCControl
''' <summary>
''' 傳入的AxTOCControl控制項
''' </summary>
''' <value></value>
''' <remarks></remarks>
WriteOnly Property DragDropAxTOCControl() As AxTOCControl
Set(ByVal value As AxTOCControl)
m_AxTOCControl = value
End Set
End Property
''' <summary>
''' 載入拖動
''' </summary>
''' <remarks></remarks>
Public Overrides Sub DragDropLoad()
If m_AxTOCControl Is Nothing Then
MessageBox.Show("圖層清單控制項為空白!", "提示", MessageBoxButtons.OK)
Exit Sub
End If
If m_AxTOCControl.Buddy Is Nothing Then
MessageBox.Show("圖層清單控制項沒有綁定地圖控制項!", "提示", MessageBoxButtons.OK)
Exit Sub
End If
Me.DragDropHwnd = m_AxTOCControl.Handle
MyBase.DragDropLoad()
End Sub
''' <summary>
''' 繼承積累的函數
''' </summary>
''' <param name="hDrop"></param>
''' <remarks></remarks>
Protected Overrides Sub DropFiles(ByVal hDrop As Long)
MyBase.DropFiles(hDrop)
AddDragLayersToMap(DragDropFiles, m_AxTOCControl.Buddy)
End Sub
''' <summary>
''' 將檔案添加到map中
''' </summary>
''' <param name="pFilePaths"></param>
''' <param name="pMapControl"></param>
''' <remarks></remarks>
Private Sub AddDragLayersToMap(ByVal pFilePaths As List(Of String), ByVal pMapControl As IMapControl2)
pMapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass
For i As Integer = 0 To pFilePaths.Count - 1
If pMapControl.CheckMxFile(pFilePaths(i)) Then
pMapControl.LoadMxFile(pFilePaths(i))
Else
pMapControl.Map.AddLayer(CreateLayerFromPath(pFilePaths(i)))
End If
Next
pMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault
End Sub
''' <summary>
''' 根據檔案路徑建立layer
''' </summary>
''' <param name="pFilePath">檔案全路徑</param>
''' <returns>返回建立的layer</returns>
''' <remarks></remarks>
Private Function CreateLayerFromPath(ByVal pFilePath As String) As ILayer
Try
Dim pLayerFactoryHelper As ILayerFactoryHelper
pLayerFactoryHelper = New LayerFactoryHelper
Dim pFileName As IFileName = New FileName
pFileName.Path = pFilePath
Dim pEnumLayer As IEnumLayer
pEnumLayer = pLayerFactoryHelper.CreateLayersFromName(pFileName)
pEnumLayer.Reset()
Dim pLayer As ILayer
pLayer = pEnumLayer.Next
Dim pReLayer As ILayer = Nothing
Do While Not pLayer Is Nothing
pReLayer = pLayer
pLayer = pEnumLayer.Next
Loop
Return pReLayer
Catch ex As Exception
Return Nothing
End Try
End Function
End Class