如何向TOCControl中拖動檔案並載入到Map中(VB.Net源碼)

來源:互聯網
上載者:User

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 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.