即当你单击“文件”菜单弹出下拉菜单时,按下字母O就可以定位到“打开”菜单。
还有一种在程序运行时都有效的全局快捷键,可以在属性面板中的“ShortCutKeys”中设置。
你还可以在属性面板中的Image属性中设置你喜欢的菜单图标。单击Image那一行右边的按钮,弹出如下菜单。选择“项目资源文件”,再单击导入就可以选择你的图标了。
最终效果如下所示。
注意,在解决方案面板中,选中刚才添加的所有图标,在其属性面板中将生成操作设置为“嵌入的资源”,这一点很重要!
2、实现相关菜单
首先定义指针(写在public partial class Form1 : Form下面即可): private ESRI.ArcGIS.Controls.IMapControl3 m_mapControl = null;
private ESRI.ArcGIS.Controls.IPageLayoutControl2 m_pageLayoutControl = null; private IMapDocument pMapDocument;
若以上指针无效,请添加以下引用:
using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.SystemUI;
在设计视图中的属性面板中,选择Form1,即主窗体,单击事件按钮(闪电形状的那个按钮),打到“Load”事件并双击,添加此事件。
在Form1_Load函数中初始化这些指针:
//取得MapControl和PageLayoutControl的引用
m_mapControl = (IMapControl3)this.axMapControl1.Object;
m_pageLayoutControl = (IPageLayoutControl2)this.axPageLayoutControl1.Object;
依次双击每个菜单项,添加菜单响应函数。实现代码如下:
///
///
private void New_Click(object sender, EventArgs e) { //本命令涉及到MapControl和PageLayoutControl同步问题,将在下一讲中实现 }
///
/// 打开地图文档Mxd命令 ///
private void Open_Click(object sender, EventArgs e) {
//本命令涉及到MapControl和PageLayoutControl同步问题,将在下一讲中实现 }
///
private void AddData_Click(object sender, EventArgs e) {
int currentLayerCount = this.axMapControl1.LayerCount;
ICommand pCommand = new ControlsAddDataCommandClass(); pCommand.OnCreate(this.axMapControl1.Object); pCommand.OnClick(); }
///
/// 保存地图文档命令 ///
private void Save_Click(object sender, EventArgs e) {
//首先确认当前地图文档是否有效 if (null != m_pageLayoutControl.DocumentFilename m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename)) {
//创建一个新的地图文档实例
IMapDocument mapDoc = new MapDocumentClass(); //打开当前地图文档
mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty);
//用PageLayout中的文档替换当前文档中的PageLayout部分
mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControl.PageLayout); //保存地图文档
mapDoc.Save(mapDoc.UsesRelativePaths, false); mapDoc.Close(); } }
///
/// 另存为地图文档命令 ///
private void SaveAs_Click(object sender, EventArgs e) {
//调用另存为命令
ICommand command = new ControlsSaveAsDocCommandClass(); command.OnCreate(m_controlsSynchronizer.ActiveControl); command.OnClick(); }
///
private void Exit_Click(object sender, EventArgs e) {
Application.Exit(); }
&&
3、编译运行
按F5编译运行程序。也许你会发现,菜单命令的实现方式都是类型的。没错,在AE9.2中,内置了许多常用的Command和Tool,如ControlsAddDataCommandClass、ControlsMapZoomInToolClass、ControlsMapPanToolClass等等,这些内置对象在ESRI.ArcGIS.Controls命名空间中,你可以对象浏览器中查看。而且这些内置对象的调用方式都类似,如下所示:
//定义
ICommand command = new ControlsSaveAsDocCommandClass();
//创建
command.OnCreate(m_controlsSynchronizer.ActiveControl); //调用
command.OnClick();
希望你可以举一反三,去实现更多的你想要的功能。
第三讲 MapControl与PageLayoutControl同步
在ArcMap中,能够很方面地进行MapView和Layout View两种视图的切换,而且二者之间的数据是同步显示的。
关于两种视图同步的实现方法有多种,可以使用ObjectCopy对象进行数据硬拷贝,而比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。
1、新建同步类ControlsSynchronizer
在解决方案面板中右击项目名,选择“添加|类”,在类别中选择“Visual C#项目项”,在模板中选择“类”,输入类名“ControlsSynchronizer.cs”,将以下代码覆盖自动生成的代码:
using System;
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms; using System.IO;
using System.Runtime.InteropServices; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.SystemUI;
namespace _sdnMap {
///
/// This class is used to synchronize a gven PageLayoutControl and a MapControl. /// When initialized, the user must pass the reference of these control to the class, bind /// the control together by calling 'BindControls' which in turn sets a joined Map referenced
/// by both control; and set all the buddy controls joined between these two controls. /// When alternating between the MapControl and PageLayoutControl, you should activate the visible control
/// and deactivate the other by calling ActivateXXX.
/// This calss is limited to a situation where the controls are not simultaneously visible.
///
public class ControlsSynchronizer {
#region class members
private IMapControl3 m_mapControl = null;
private IPageLayoutControl2 m_pageLayoutControl = null; private ITool m_mapActiveTool = null;
private ITool m_pageLayoutActiveTool = null; private bool m_IsMapCtrlactive = true;
private ArrayList m_frameworkControls = null; #endregion
#region constructor ///
public ControlsSynchronizer() {
//初始化ArrayList
m_frameworkControls = new ArrayList(); }
///
public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl) : this() {
//为类成员赋值
m_mapControl = mapControl;
m_pageLayoutControl = pageLayoutControl; }
#endregion
#region properties ///
/// 取得或设置MapControl ///
public IMapControl3 MapControl {
get { return m_mapControl; } set { m_mapControl = value; } }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库ArcGIS Engine+C# 初学者实例代码(2)在线全文阅读。
相关推荐: