系统演示请看《我的插件GIS系统演示》
今天来讲一下工具栏的设计,目前是完全按照Sharpdevelop(我把它简称为SD)的规则建立工具栏,讲的内容也完全是SD中如何定义工具栏,大家有兴趣的话也可以实现自己的工具栏加载机制,我现在在另一个项目中已经脱离SD,通过Sqlite数据库配置界面,实现了一个简单的插件框架。
使用sharpdevelop的插件机制之后,工具栏的开发已经非常便捷,主要优点有:
1、插件方式,装配、修改便捷
由于SD的插件配置文件采用XML文件,便于阅读,我们可以根据自己的需求新建、修改配置信息。实现满足特定需求的工具栏。
2、利用Condition实现条件可视化
很多时候,工具栏按钮是否显示(Visible)、是否可用 (Enable)需要根据特定条件变化。SD框架设计了ICondition条件接口,通过实现该接口自定义类来完成条件可视化。
<!--条件(单一条件),当前活动窗体是MapViewContent时显示-->
<Condition name = "WindowActive" activewindow="GISP.Addin.ArcMap.Views.MapViewContent">
<ToolbarItem id = "New" icon = "Icons.16x16.NewDocumentIcon" tooltip = "${res:XML.MainMenu.FileMenu.New.File.Description}" class = "GISP.Addin.ArcMap.Commands.NewMxdCommand"/>
<ToolbarItem id="OpenMxd" tooltip="${res:GIS.MapControl.Commands.OpenMXD}" icon="Icons.16x16.OpenFileIcon" class="GISP.Addin.ArcMap.Commands.OpenMxdCommand"/>
<ToolbarItem id="SaveMxd" tooltip="${res:GIS.MapControl.Commands.SaveMXD}" icon="Icons.16x16.SaveIcon" class="GISP.Addin.ArcMap.Commands.SaveMxdCommand"/>
<ToolbarItem id="SaveAsMxd" tooltip="${res:GIS.MapControl.Commands.SaveAsMXD}" icon="Icons.16x16.SaveIcon" class="GISP.Addin.ArcMap.Commands.SaveMxdAsCommand"/>
<ToolbarItem id = "Separator1" type = "Separator"/>
<ToolbarItem id="ExportToPic" tooltip="输出图片" icon="ArcGIS.MapOpen16" class="GISP.Addin.ArcMap.Commands.ExportActiveViewToPictureCommand"/>
</Condition>
<!--复合条件-->
<ComplexCondition>
<Or>
<Condition name="CADLayerTOCSelectItemConditionEvaluator" itemtype="null"/>
<Condition name="CADLayerTOCSelectItemConditionEvaluator" itemtype="group"/>
</Or>
</ComplexCondition>
条件分为单一条件和复合条件。
复合条件的复合关系包括有:And和Or。
条件的action状态分为不显示(不指定)和不可用(action="disable")。
3、工具栏的父对象灵活指定
工具栏可的父对象一般是主窗体,也可以是子控件、子窗体,通过简单的代码就可以在不同级别的对象上建立工具栏。
如果要子子窗体或者空间上显示工具栏,只需要在代码中添加类似代码即可:
public partial class CatalogControl : UserControl
{
……
ToolStrip toolStrip = null;
#region 构造函数
public CatalogControl()
{
InitializeComponent();
InitControl();
……
}
#endregion
private void InitControl()
{
toolStrip = ToolbarService.CreateToolStrip(this, "/SDF/Pads/ArcGIS/CatalogPad/Toolbar");
toolStrip.GripStyle = ToolStripGripStyle.Hidden;
this.Controls.Add(toolStrip);
}
接下来讲一下如何定义工具栏,以地图导航工具栏为例:
1、首先定义工具栏按钮实现类
1 /// <summary>
2 /// 地图放大
3 /// </summary>
4 class MapZoomInCommand : BaseMapControlCommand
5 {
6 public override void Run()
7 {
8
9 if (MapControl != null)
10 {
11 ESRI.ArcGIS.SystemUI.ICommand command = new ESRI.ArcGIS.Controls.ControlsMapZoomInToolClass();
12 command.OnCreate(MapControl.MapControlDefault);
13 MapControl.MapControlDefault.CurrentTool = command as ESRI.ArcGIS.SystemUI.ITool;
14 StatusBarService.SetMessage("放大");
15 }
16 }
17 }
MapZoomInCommand的父类是BaseMapControlCommand,在内部封装了DM控件、TOC控件、PageLayout控件的属性,以便在子类中简化实现。
1 public class BaseMapControlCommand : AbstractCommand
2 {
3
4 protected DefaultMapControl MapControl
5 {
6 get
7 {
8 foreach (IViewContent view in WorkbenchSingleton.Workbench.ViewContentCollection)
9 {
10 if (view.Control is DefaultMapControl)
11 return view.Control as DefaultMapControl;
12 }
13 return null;
14 }
15 }
16
17 protected DefaultPageLayoutControl PageLayoutControl
18 {
19 get
20 {
21 foreach (IViewContent view in WorkbenchSingleton.Workbench.ViewContentCollection)
22 {
23 if (view.Control is DefaultPageLayoutControl)
24 return view.Control as DefaultPageLayoutControl;
25 }
26 return null;
27 }
28 }
29
30 protected TocControl TocControl
31 {
32 get
33 {
34 foreach (PadDescriptor pad in WorkbenchSingleton.Workbench.PadContentCollection)
35 {
36 if (pad.PadContent.Control is TocControl)
37 return pad.PadContent.Control as TocControl;
38 }
39 return null;
40 }
41 }
42
43 public override void Run()
44 {
45
46 }
47 }
2、定义Addin文件
命令类定义好之后,就需要在Addin文件中进行配置。
<Path name="/SDF/Workbench/ToolBar/ArcMap_Navagation">
<Condition name = "WindowActive" activewindow="GISP.Addin.ArcMap.Views.MapViewContent">
<ToolbarItem id="ZoomIn" tooltip="${res:GIS.MapControl.Commands.ZoomIn}" icon="GIS.MapControl.ZoomIn" class="GISP.Addin.ArcMap.Commands.MapZoomInCommand"/>
<ToolbarItem id="ZoomOut" tooltip="${res:GIS.MapControl.Commands.ZoomOut}" icon="GIS.MapControl.ZoomOut" class="GISP.Addin.ArcMap.Commands.MapZoomOutCommand"/>
......
配置时需要指定ID、提示信息、图标和命令类。需要注意的是图标需要保存在资源文件中。
3、 启动程序测试
如果Addin文件没有书写错误,程序启动后,会生成如下图所示的工具栏。
补充
工具栏按钮的类型包括普通按钮和下拉按钮,普通按钮的定义上面已经进行了介绍,这里对下拉按钮做一个简单说明。
下拉按钮的界面显示类似下图
配置文件的格式如下代码,注意指定type = "DropDownButton",其下拉项是menuitem。
<ToolbarItem id = "Editor" type = "DropDownButton" icon = "ArcGIS.EditingLayerTarget16"
tooltip = "编辑" class ="GISP.Addin.ArcMap.Commands.MapEditorDropDownList">
<Condition name="MapContainsLayer" itemtype="ESRI.ArcGIS.Carto:ESRI.ArcGIS.Carto.IFeatureLayer" action="Disable">
<Condition name = "EditState" state="notediting" action="Disable">
<MenuItem id="StartEdit" label="启动编辑" icon="" class="GISP.Addin.ArcMap.Commands.MapStartEditingCommand"/>
</Condition>
<Condition name = "EditState" state="editing" action="Disable">
<MenuItem id="StopEdit" label="停止编辑" icon="" insertafter="StartEdit" class="GISP.Addin.ArcMap.Commands.MapStopEditingCommand"/>
<MenuItem id = "editStatusSeprator" type = "Separator" insertafter="StopEdit"/>
<MenuItem id="saveEdit" label="保存编辑" icon="" insertafter="editStatusSeprator" class="GISP.Addin.ArcMap.Commands.MapSaveEditingCommand"/>
<MenuItem id = "SaveEditSeprator" type = "Separator" insertafter="saveEdit"/>
<MenuItem id="Edit_Move" label="移动.." icon="" insertafter="SaveEditSeprator" class="GISP.Addin.ArcMap.Commands.MapEdit_MoveFeatureCommand"/>
<MenuItem id="Edit_Split" label="分解" icon="" insertafter="Edit_Move" class="GISP.Addin.ArcMap.Commands.MapEdit_SplitMultipleFeatureCommand"/>
<MenuItem id="Edit_Buffer" label="缓冲..." icon="" insertafter="Edit_Split" class="GISP.Addin.ArcMap.Commands.MapEdit_BufferFeatureCommand"/>
<MenuItem id="Edit_Union" label="合并..." icon="" insertafter="Edit_Buffer" class="GISP.Addin.ArcMap.Commands.MapEdit_MergeFeatureCommand"/>
<MenuItem id="Edit_Intersect" label="交集" icon="" insertafter="Edit_Union" class="GISP.Addin.ArcMap.Commands.MapEdit_IntersectFeatureCommand"/>
<!--<MenuItem id="SelectFeatures" label="求异" icon="" class="GISP.Addin.ArcMap.Commands.MapEdit_DifferenceFeatureCommand"/>-->
<MenuItem id="Edit_Cut" label="裁剪..." icon="" insertafter="Edit_Intersect" class="GISP.Addin.ArcMap.Commands.MapEdit_ClipFeatureCommand"/>
<MenuItem id="Edit_AddIntersectPointOfLine" label="添加交点..." insertafter="Edit_Cut" icon="" class="GISP.Addin.ArcMap.Commands.MapEdit_AddIntersectPointOfPolyLineFeatureCommand"/>
<MenuItem id="Edit_GeneralizeGeometry" label="一般化..." icon="AddIntersectPointOfLine" insertafter="Edit_AddIntersectPointOfLine" class="GISP.Addin.ArcMap.Commands.MapEdit_GeneralizeGeometryCommand"/>
<MenuItem id="Edit_DeleteOverlap" label="删除压盖要素..." icon="" insertafter="Edit_AddIntersectPointOfLine" class="GISP.Addin.ArcMap.Commands.MapEdit_DeleteOverlapFeaturesCommand"/>
</Condition>
</Condition>
</ToolbarItem>