MapControl对应ArcMap中的数据视图,它封装了Map对象,并提供了额外的属性、方法、事件用于:

1 管理控件的外观、显示属性和地图属性

2 添加并管理控件中的数据层

3 装载Map文档控件中

4 从其它应用程序拖放数据到控件中

5 tracking shapes and drawing to the display

MapControl实现的主要接口有:IMapControlDefault   IMapControl2 IMapControl3和事件接口IMapControlEvents2

IMapControlDefault接口是地图控件缺省接口,多数开发环境自动使用这个接口定义的属性和方法。由于MapControl是一个自动化控件,当它被放到一个容器---如窗体上后,它会自动产生一个被称为axMapControl1的对象,这个对象可以直接使用缺省接口定义的属性和方法。MapControl当前最新版本接口为IMapControl3.

当需要使用这个接口时,可使用下面的代码:

IMapControlDefault pMapControl;
pMapControl = axMapControl1.Object as IMapControlDefault;

对于文档文件,MapControl控件可以直接使用LoadMxFile方法来载入,这是最简单的方法。除此之外,也可以使用IMapDocument接口定义的属性和方法来加载一个MSD文件。下面是一个载入文档的例子:

private void LoadMapDocument()
{
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

上例中,先使用.NET框架类库提供的OpenFileDialog对话框找到要打开的MSD文档sFilePath,然后利用AxMapControl提供的方法LoadMxFile()打开该MXD文档,否则,显示有关信息。

如果要装载某个地图文档中的特定地图,则可以使用以下方法:

private void LoadMapDocument2()
{
System.Windows.Forms.OpenFileDialog openFileDialog1;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open Map Document";

openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
string sFilePath = openFileDialog1.FileName;
if (axMapControl1.CheckMxFile(sFilePath))
{
IArraypArray ;
pArray = axMapControl1.ReadMxMaps(sFilePath,Type.Missing);
int i ;
IMap pMap;
//遍历这些Map对象
for(i = 0;i<=pArray.Count - 1;i++)
{
//QI
pMap = pArray.get_Element(i) as IMap;
if (pMap.Name == "Layers")
{
//载入文档中特定的Map对象
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(sFilePath, 0,Type.Missing);
axMapControl1.MousePointer =
esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(sFilePath + " is not a valid ArcMap document");
return;
}
}

下面的例子是打开、保存、另存为一个文档文件,详细代码如下:

IMapDocument m_MapDocument;
private void LoadMapDoc()
{
m_MapDocument = new MapDocumentClass();
try
{
//打开文件对话框选取MXD文件
System.Windows.Forms.OpenFileDialog openFileDialog2;
openFileDialog2 = new OpenFileDialog();
openFileDialog2.Title = "Open Map Document";
openFileDialog2.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string sFilePath = openFileDialog2.FileName;
//将数据载入pMapDocument并与map控件联系起来
m_MapDocument.Open(sFilePath, "");
int i;
for (i = 0 ; i<= pMapDocument.MapCount - 1;i++)
{
//一个IMapDocument对象中可能有多个Map对象,遍历每个map对象
axMapControl1.Map = m_MapDocument.get_Map(i);
}
//刷新地图
axMapControl1.Refresh();
}
catch( Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void SaveDocument()
{

//Check that the document is not read only
if (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename)
== true)
{
MessageBox.Show("This map document is read only!");
return;
}
//Save with the current relative path setting
m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);
MessageBox.Show("Changes saved successfully!");
}
private void SaveAsDocument ()
{
//Open a file dialog for saving map documents
saveFileDialog1.Title = "Save Map Document As";
saveFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
//Exit if no map document is selected
string sFilePath = saveFileDialog1.FileName;
if (sFilePath == "")
{
return;
}
if (sFilePath == m_MapDocument.DocumentFilename)
{
//Save changes to the current document
SaveDocument();
}
else
{
//SaveAs a new document with relative paths
m_MapDocument.SaveAs(sFilePath, true, true);
//Open document
OpenDocument((sFilePath));
MessageBox.Show("Document saved successfully!");

}
}
新建一个空的地图文档,并将其加载到 MapControl 的代码如下:
private void mnuNew_Click(object sender, System.EventArgs e)
{
// IMap pMap = new MapClass();
// axMapControl1.Map = pMap ;
MapDocument pMapDocument = new MapDocumentClass();
string sFilePath = @"d:\temp\untitled.mxd";
pMapDocument.New(sFilePath);
pMapDocument.Open(sFilePath,"");
axMapControl1.Map = pMapDocument.get_Map(0);
}

此外,MapControl控件还可以使用AddLayerFromFile方法添加一个图层文件,使用AddshapeFile添加一个Shape文件,使用AddLayer快速添加一个图层,DeleteLayer快速删除一个图层,MoveLayerTo改变一个图层的索引等。

AO总结10:MapControl控件的更多相关文章

  1. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  2. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

  3. 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout

    [源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...

  4. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  5. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  6. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  7. 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

    [源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...

  8. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

  9. 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox

    [源码下载] 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox 作者:webabcd ...

随机推荐

  1. 不再用.NET框架

    .NET 平台很棒.真的很棒.直到它不再那么棒.我为什么不再用 .NET?简单来说,它限制了我们选择的能力(对我来说很重要),转移了我们的注意力,使得我们向内认知它的安全性,替代了帮助我们认知外面广阔 ...

  2. 使用wget备份禅道

    禅道7.1,管理了公司所有项目.需要每月备份. 主机安装在一台windows上.为了方便,写个脚本自动调用禅道的备份功能,并把服务器上的备份文件下载到本地. @echo off setlocal re ...

  3. ORA-00845

    系统版本: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 数据库版本: Oracle Database ...

  4. 【转载】mysqldump的single-transaction和master-data

    原文地址:mysqldump的single-transaction和master-data 作者:myownstars 先看一下--lock-tables和--lock-all-tables --lo ...

  5. gcc编译出现的问题

    /usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error 解决办法:g++ -std=c++11

  6. Codeforces Round #333 DIV2

    D: B. Lipshitz Sequence time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  8. List<T>中Exists 和Contains的区别

    .net编码中,使用泛型List<>时,经常遇到这样的需求:新来一个Model对象,如果已有的List中没有这条数据,则把新对象Add到List中,否则不处理 判断已有的List中是否包含 ...

  9. nodejs小问题:express不是内部或外部命令(转载)

    安装express之后发现居然提示express不是内部或外部命令. 工具/原料 Node.js安装包 方法/步骤 1 首先下载Node.js安装包,此处我用的是官方最新的v0.10.27 32位版: ...

  10. linux第四周作业

    一.用户态内核态与中断 1.库函数把内核调用封装起来. 2.区分内核态和用户态是为了让系统更稳定.Linux里吧用户态定位3级,把内核态定位0级. 3.中断处理就是从用户态进入内核态的主要方法,系统调 ...