所以必须对ArcGIS Control传送的类型作检查。HookHelper用来控件回调并返回ActiveView忽略的回调类型(MapControl、PageLayoutControl和ToolbarControl都是这样)。
17. 在类视图中定位到BaseTool基类的OnMouseDown方法,并在其上点击右键显示上下文菜单。选择添加,然后重
载并增加该属性至代码窗口。
18. 增加下列代码,重载BaseTool类实现的默认OnMouseDown函数。
public override void OnMouseDown(int Button, int Shift, int X, int Y) {
// TODO: 添加 AddDateTool.OnMouseDown 实现 base.OnMouseDown (Button, Shift, X, Y); // 获取活动视图
IActiveView activeView = m_HookHelper.ActiveView; // 创建新的文本元素
ITextElement textElement = new TextElementClass(); // 创建文本符号
ITextSymbol textSymbol = new TextSymbolClass(); textSymbol.Size = 25; // 设置文本元素属性
textElement.Symbol = textSymbol;
textElement.Text = DateTime.Now.ToShortDateString(); // 对IElementQI
IElement element = (IElement) textElement; // 创建页点
IPoint point = new PointClass();
point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
// 设置元素图形
element.Geometry = point; // 增加元素到图形绘制容器
activeView.GraphicsContainer.AddElement(element, 0); // 刷新图形
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
19. ArcGIS Engine期望自定义命令是一个COM类;因此,你必须指定你所创建的.NET类也成为一个COM类,它是
通过创建一个COM可调用包装(callable wrapper)实现的。在解决方案资源管理器窗口中,在Commands项目上右击鼠标键并从上下文菜单中选择属性。
20. 在项目属性页对话框中选择配置属性;并点击生成。在右面的面板中,改变为“为Com Interop注册”为True,点
确定。
注:设置“为Com Interop注册”属性为True会调用程序集注册工具(Regasm.exe)。这将增加客户端期望找到的类信息。
如果“为Com Interop注册”属性设为False,则使项目不要是一个C#类库类型。
21. 在AddDateTool类的代码编写窗口的AddDateTool类声明的开始位置增加下列代码,指定COM需要的属性。
[ClassInterface(ClassInterfaceType.None)] [Guid(\
注:新的GUID可能通过Visual Studio .NET中的GuidGen.exe实用工具生成,或者从工具菜单中选择创建GUID。GUID应该像上面的格式并不包含大括号(curly braces)。
22. 向AddDateTool类成员变量的后面增加下列代码。此代码定义了一些函数,这些函数使用目录实用工具向ESRI
控件命令(ESRI Control Commands)组件目录注册和取消注册AddDateTool类。
// 在“ESRI Controls Commands”组件目录注册 #region Component Category Registration [ComRegisterFunction()] [ComVisible(false)]
static void RegisterFunction(String sKey) {
string fullKey = sKey.Remove(0, 18) + @\ Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true);
if (regKey != null) {
regKey.CreateSubKey(\
}
}
[ComUnregisterFunction()] [ComVisible(false)]
static void UnregisterFunction(String sKey) {
string fullKey = sKey.Remove(0, 18) + @\ Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true);
if (regKey != null) {
regKey.DeleteSubKey(\
}
}
#endregion
23. 生成工程。
24. 在方案开始创建的Visual Studio .NET “Windows应用程序”项目中,增加地图导航命令代码的后面增加以下代码。
private void Form1_Load(object sender, System.EventArgs e) {
// 前面是命令导航代码??
// 添加自定义日期工具
progID = \ axToolbarControl1.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconAndText); // 后面是ToolbarMenu相关代码?? }
25. 生成并运行应用程序,使用添加日期工具向PageLayoutControl上增加一个包含当天日期的文本元素。
(十一) 自定义ToolbarControl
同在Form_Load事件中向ToolbarControl控件增加ArcGIS Engine命令和工具一样,你也可以使用定制对话框和自定义ToolbarControl的方式添加命令和工具。要实现它,就要将ToolbarControl置为定制模式并显示定制对话框。
1. 向类中增加下列成员变量:
??
private ITransformEvents_VisibleBoundsUpdatedEventHandler
// PageLayoutControl的焦点图事件
visBoundsUpdatedE;
private ICustomizeDialog m_CustomizeDialog = new
CustomizeDialogClass(); // CurtomizeDialog被ToolbarControl使用
private ICustomizeDialogEvents_OnStartDialogEventHandler
startDialogE; // CustomizeDialog启动事件