缘起
个人总结梳理,顺便练练文笔,For me and for you like me!
目标
我一直很认同古人所说的君子性非异也,善假于物也
,本文主要目的是告诉读者如何使用帮助来学习了解ArcEngine中的控件,怎样更好的阅读帮助。
- 学会如何使用帮助,能在帮助中找到和控件相关的内容。
- 了解每个控件都是干嘛用的,具体的细节本文不做介绍。
- 了解控件类实现了哪些接口和哪些方法。
- 尝试性的写写代码,熟悉相关的控件。
初识
简介
使用Winfrom开发,经常会用到地图控件,了解常用的地图控件也是入门必修功课之一。
常用控件如下:
- MapControl(地图控件,主要用来显示地图)
- PageLayoutControl(页面布局控件,主要用来打印、输出地图)
- GlobeControl(本文不做介绍)
- SceneControl(本文不做介绍)
- ToCControl(主要用来显示图层列表(地图数据目录树))
- ToolbarControl(地图工具条,提供一些小工具)
- SymbologyControl(地图符号控件)
资源
- 本地资源(找不到帮助的请看上一篇文章)
- ArcGIS Engine入门教程_第四章_创建Engine应用程序
- 参考《ArcGIS Engine 10 开发更新》中的创建第一个桌面应用程序(需要电子书的可以加Q群自行下载)
庖丁解牛
ArcMap和ArcEngine中控件的对应关系。
常用控件
在vs中点击:视图-》工具箱-》ArcGIS Windows Forms。默认添加完这些控件,生成的控件实例对象命名都带前缀ax
,如axMapcContorl1
,axPageLayoutControl1
强烈建议参考李崇贵的《ArcGIS Engine 组件式开发及应用》学习,个人觉得他们写的还是不错的,需要资源的加Q群自取。
LicenseControl
左侧为产品许可级别,右侧为许可扩展。
注意:左侧主许可是单选,如果选中了多个,则默认为第一个
。由于下面Basic、Standard和Advanced许可是Desktop产品的许可,而LicenseControl控件只能在Engine产品下使用,所以使用LicenseControl控件无法初始化Basic、Standard和Advanced许可
,但可以使用代码进行初始化。右侧扩展许可是可以多选的。一个程序只能初始化一次许可,或者使用LicenseControl控件,或者使用代码,代码初始化许可如下:
绑定产品:
/* 这里是绑定一个产品,即Engine程序调用哪个安装产品下的资源,绑定Engine,即调用Engine安装目录的资源,绑定Desktop,
即调用Desktop安装目录下的资源,绑定EngineOrDesktop,即优先寻找机器上有没有安装Engine,有的话绑定Engine,
没有再绑定Desktop。而LicenseControl控件是初始化许可,这两者一定要加以区别。*/
if (!RuntimeManager.Bind(ProductCode.Engine))
{
if (!RuntimeManager.Bind(ProductCode.Desktop))
{
MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.");
return;
}
}
初始化许可:
IAoInitialize m_AoInitialize = new AoInitializeClass();
esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
{
licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);
if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
{
licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
{
XtraMessageBox.Show("获取ARCGIS授权失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
// 检出扩展许可
licenseStatus = m_AoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);
licenseStatus = m_AoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
个人经验:不建议使用LicenseControl控件,建议使用代码初始化许可。
MapControl
MapControl控件主要用来加载显示地图数据,对应ArcMap中的数据视图。
MapControl类封装了Map对象,并提供相应的属性、方法、事件,它可以实现以下功能:
- 地图显示;
- 地图的放大、缩小和漫游;
- 控制地图上鼠标显示的样式;
- 生成点线面等图形元素;
- 识别地图上选中的元素,并进行属性查询;
- 标注地图元素等;
- 监听地图事件;
PageLayoutControl
PageLayoutControl控件主要用于制图,对应ArcMap的布局视图,利用该控件可以方便地操作各种元素对象,以便产生精美的地图。
PageLayoutControl封装了PageLayout类,PageLayout提供了在布局视图中控制元素的属性和方法。除此之外,还有Printer属性用于处理地图打印时的系列设置、Page属性用于处理控件的页面效果、Element属性用于管理控件的地图元素。
关于MapControl和PageLayoutControl实现鹰眼功能
的代码网上有很多, 官方示例代码
TOCControl
TOCControl以树形结构显示其“伙伴控件“”的地图、图层和符号体系,该控件通过ITOCBuddy接口来访问其伙伴控件。
SymbologyControl
SymbologyControl主要是用来显示*.ServerStyle
(ArcEngine)和*.Style
(ArcMap)文件的符号目录。该控件允许用户选择一个符号应用到一部分程序上,比如图层的符号,Element的符号等等。
注意:
- *.Style文件只适用于Desktop产品。
- *.ServerStyle文件适用于所有的产品。(位于:<install_location>\ArcGIS<EngineVersion>\Styles)
加载ServerStyle文件
axSymbologyControl1.LoadStyleFile(@"myInstallLocation\ArcGIS\Engine10.2\Styles\ESRI.ServerStyle");
示例:
IStyleGalleryItem serverStyleGalleryItem = axSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass).GetSelectedItem();
System.Windows.Forms.MessageBox.Show(serverStyleGalleryItem.Name);
ToolbarControl
ToolbarControl需要绑定伙伴控件(MapControl/PageLayoutControl/GlobeControl/SceneControl),可以使用SetBuddyControl
方法动态绑定,ToolbarControl相当于是许多Commands、Tool Control、Menus、Palettes(调色板)的容器。
这块主要涉及到ToolbarControl、ToolbarItem、ToolbarMenu、CommandPool、Customizedialog、MissingCommand。
示例代码:
// 绑定伙伴控件
axToolbarControl1.SetBuddyControl(axMapControl1);
// 添加打开文档命令
axToolbarControl1.AddItem("esriControls.ControlsOpenDocCommand", - 1, - 1, false, 0, esriCommandStyles.esriCommandStyleIconAndText);
//添加地图放大命令
axToolbarControl1.AddItem("esriControls.ControlsMapZoomInTool", - 1, - 1, true, 0, esriCommandStyles.esriCommandStyleIconOnly);
//添加地图缩小命令
axToolbarControl1.AddItem("esriControls.ControlsMapZoomOutTool", - 1, - 1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
//添加地图全图命令
axToolbarControl1.AddItem("esriControls.ControlsMapFullExtentCommand", - 1, - 1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);