常规openfiledialog导入
OpenFileDialog open = new OpenFileDialog();
open.Multiselect=true;//可以选择多个选项
open.Title = "选择需要加载的地里数据文件";
open.Filter = "Shape格式文件(*.shp)|*.shp";
if (open.ShowDialog ()== DialogResult.OK)
{
foreach (string Wenjian in open.FileNames)
{
//选择了shape文件后
string pathName = System.IO.Path.GetDirectoryName(Wenjian);
string fileName = System.IO.Path.GetFileNameWithoutExtension(Wenjian);
//axmapcontrol1加载地图
axMapControl1.AddShapeFile(pathName, fileName);
//鹰眼加载地图
//axMapControl2.ClearLayers();//鹰眼视图是否更换
axMapControl2.AddShapeFile(pathName, fileName);
axMapControl2.Extent = axMapControl2.FullExtent;//显示全图
}
}
拖动导入
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string pName in filePath)
{
char d = '.';
string pathName = System.IO.Path.GetDirectoryName(pName);//指定路径字符串
string fileName = System.IO.Path.GetFileNameWithoutExtension(pName);//不具扩展名的路径
//axmapcontrol1加载地图
axMapControl1.AddShapeFile(pathName, fileName);
//鹰眼加载地图
//axMapControl2.ClearLayers();//鹰眼视图是否更换
axMapControl2.AddShapeFile(pathName, fileName);
axMapControl2.Extent = axMapControl2.FullExtent;//显示全图
}
DropDrop事件:将AcceptDrop设置为true,这样程序就可以有DropEnter和DropDrop事件了
DropEnter触发时,需要将AllowedEffect属性设置为非None值比如e.Effect = DragDropEffects.Move.
DragDrop触发时,可以得到一个文件全路径的字符串数组
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
}