第一步:设置MapControl的AllowDrop属性为True
第二步:设置MapControl的OldDropEnabled属性为True
第三步:选择MapControl的OnOleDrop事件
第四步:加入如下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void m_MapControl_OnOleDrop(object sender, IMapControlEvents2_OnOleDropEvent e)
{
IDataObjectHelper pDataObjectHelper = e.dataObjectHelper as IDataObjectHelper;
if (e.dropAction == esriControlsDropAction.esriDropped)
{
System.Array array = pDataObjectHelper.GetFiles() as System.Array;
for (int i = 0; i < array.Length; i++)
{
AddData(array.GetValue(i).ToString());
}
m_MapControl.Extent = m_MapControl.FullExtent;
}
}
private void AddData(string filePath)
{
string fileExtension = System.IO.Path.GetExtension(filePath);
switch (fileExtension)
{
case ".mxd":
{
m_MapControl.LoadMxFile(filePath);
}
break;
case ".shp":
{
m_MapControl.AddShapeFile(System.IO.Path.GetDirectoryName(filePath), System.IO.Path.GetFileName(filePath));
}
break;
default:
{
MessageBox.Show("错误");
}
break;
}
}
}
}
运行结果如下: