AO总结10:MapControl控件
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控件的更多相关文章
- 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
[源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
[源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...
- 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout
[源码下载] 背水一战 Windows 10 (35) - 控件(弹出类): FlyoutBase, Flyout, MenuFlyout 作者:webabcd 介绍背水一战 Windows 10 之 ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
[源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton
[源码下载] 背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButt ...
- 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox
[源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...
- 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox
[源码下载] 背水一战 Windows 10 (29) - 控件(文本类): RichTextBlock, RichTextBlockOverflow, RichEditBox 作者:webabcd ...
随机推荐
- Flash Activex NPAPI PPAPI 各种网页插件完整安装包下载地址
内容全部是自己手工原创写作的参考内容,完全排除从其他网站COPY的内容信息.如有雷同实属巧合. 奉献给有需求的人士,也给各位解决FLASH安装头疼的问题,正常在线下载安装运气不好的安装半天.运气好 ...
- javascript 柯里化
先看一下代码 function add(){ var sum=0; for(var i=0;i<arguments.length;i++){ sum+=arguments[i]; } retur ...
- BI的核心价值[转]
BI的核心价值是辅助决策,从一个洁净的数据源中自动提取有价值的数据进行分析,从而成为重要商业决定的决策基础.但在国内,洁净的数据源不易得到,很多情况下都需要进行数据清洗,所以BI的应用受到很大程度的抑 ...
- 多实例MySQL批量添加用户和密码并授权
OS:Cent OS 6.3 DB:5.5.14 如果一台服务器上有100个MySQL数据库实例,此时需要给不同端口的几个数据库添加用户名.密码并授权应用IP,一个个授权没问题,反正累的不是我,哇咔咔 ...
- ArcGIS操作问题
1.利用分析工具——叠加分析——“空间连接”工具,将完全包含(COMPLETELY_CONTAINS)某点的面的属性值赋为该点的属性值. 其中定义用于匹配行的条件.匹配选项包括: 相交—如果连接要素与 ...
- iOS多线程编程Part 2/3 - NSOperation
多线程编程Part 1介绍了NSThread以及NSRunLoop,这篇Blog介绍另一种并发编程技术:NSOPeration. NSOperation & NSOperationQueue ...
- Entity Framework (二) 查询
待完善-------------------------------------- ----------- base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的方法. 指定创建 ...
- Qt版helloworld
跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Cre ...
- Disable keyboard input on Android TimePicker
try to use: myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS); to disable f ...
- 常见的装置与其在Linux当中的档名
需要特别留意的是硬盘机(不论是IDE/SCSI/U盘都一样),每个磁碟机的磁盘分区(partition)不同时, 其磁碟档名还会改变呢!下一小节我们会介绍磁盘分区的相关概念啦!需要特别注意的是磁带机的 ...