本节将向你介绍,利用ToolStrip制作自定义GIS工具条。
步骤如下:
①向ToolStrip中添加一个Button
②向该Button的lmg属性添加图片素材,并将Button的图片比例(ImageScaling)属性设置为自动适应(SizetoFit),自动调整大小(AutoSzize)为真(True)。最后通过调整ToolStrip的图片尺寸缩放比例(lmageSealingSize)为合适尺寸,例如width:48,height:48,最后再手动调节控件的大小。
③双击Buton,在其单击事件下写代码。
本例实现的代码如下:
/// <summary> /// 放大工具 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ZoomIntoolStripButton1_Click(object sender, EventArgs e) { //当选中为数据制图时 if (tabControl1.SelectedIndex == 0) { //ICommand提供对定义COM命令的成员的访问。 ICommand pCommand = new ControlsMapZoomInToolClass(); //将ICommand强转成ITool ITool pTool = pCommand as ITool; //调用ICommand中的OnCreate方法生成放大工具 pCommand.OnCreate(this.axMapControl1.Object); //设置放大工具命令的作用对象为axMapControl1 this.axMapControl1.CurrentTool = pTool; } //当选中为布局视图时 if (tabControl1.SelectedIndex == 1) { //ICommand提供对定义COM命令的成员的访问。 ICommand pCommand = new ControlsMapZoomInToolClass(); //将ICommand强转成ITool ITool pTool = pCommand as ITool; //调用ICommand中的OnCreate方法生成放大工具 pCommand.OnCreate(this.axPageLayoutControl1.Object); //设置放大工具命令的作用对象为axPageLayoutControl1 this.axPageLayoutControl1.CurrentTool = pTool; } } /// <summary> /// 缩小工具 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ZoomOuttoolStripButton2_Click(object sender, EventArgs e) { //当选中为数据视图时 if (tabControl1.SelectedIndex == 0) { ICommand pCommand = new ControlsMapZoomOutToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axMapControl1.Object); this.axMapControl1.CurrentTool = pTool; } //当选中为布局视图时 if (tabControl1.SelectedIndex == 1) { ICommand pCommand = new ControlsMapZoomOutToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axPageLayoutControl1.Object); this.axPageLayoutControl1.CurrentTool = pTool; } } /// <summary> /// 漫游工具 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PantooltoolStripButton4_Click(object sender, EventArgs e) { //当选中为数据视图时 if (tabControl1.SelectedIndex == 0) { ICommand pCommand = new ControlsMapPanToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axMapControl1.Object); this.axPageLayoutControl1.CurrentTool = pTool; } //当选中为布局视图时 if (tabControl1.SelectedIndex == 1) { ICommand pCommand = new ControlsMapPanToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axPageLayoutControl1.Object); this.axPageLayoutControl1.CurrentTool = pTool; } } /// <summary> /// 全图工具 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ZoomFulltoolStripButton3_Click(object sender, EventArgs e) { //当选中为数据视图时 if (tabControl1.SelectedIndex == 0) { ICommand pCommand = new ControlsMapFullExtentCommandClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axMapControl1.Object); this.axMapControl1.CurrentTool = pTool; pCommand.OnClick(); } //当选中为布局视图时 if (tabControl1.SelectedIndex == 1) { ICommand pCommand = new ControlsMapFullExtentCommandClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axPageLayoutControl1.Object); this.axPageLayoutControl1.CurrentTool = pTool; pCommand.OnClick(); } } /// <summary> /// 指针选择工具 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SelecttoolStripButton1_Click(object sender, EventArgs e) { if (tabControl1.SelectedIndex == 0) { ICommand pCommand = new ControlsSelectFeaturesToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axMapControl1.Object); this.axMapControl1.CurrentTool = pTool; } if (tabControl1.SelectedIndex == 1) { ICommand pCommand = new ControlsSelectFeaturesToolClass(); ITool pTool = pCommand as ITool; pCommand.OnCreate(this.axPageLayoutControl1.Object); this.axPageLayoutControl1.CurrentTool = pTool; } }
注:
1、数据视图和布局视图的控件是不同的。
2、全图工具需要添加以下代码,才能生效。
pCommand.OnClick();
3、其他工具的制作方法大同小异!
核心DIY工具函数总结:
谢谢观看!本人初学GIS二次开发,如果有不对的地方,请多多包涵!