要求:在ToolbarControl中添加一个自定义命令,点击可清除当前活动工具。
步骤:
1、创建GIS类,选择Base Command模版,Extending ArcObjects,ArcMap MapControl or PageLayoutControl command,命名为ClearCurrentActiveToolCmd.cs
2、在类中定义IToolbarControl的接口变量m_ToolbarControl,并在类的构造函数中改变所继承的基类属性,其中,base.m_bitmap,即该命令的图标可直接双击本工程中的ClearCurrentActiveToolCmd.bmp修改。
3、在OnCreate重载函数中,将Hook转换为IToolbarControl,并赋给m_ToolbarControl变量。
4、在OnClick重载函数中,将m_ToolbarControl接口变量的CurrentTool属性设置为空。
代码如下:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
namespace WindowsFormsApplication2_1_2
{
/// <summary>
/// Summary description for ClearCurrentActiveToolCmd.
/// </summary>
[Guid("df41e943-e173-4d88-90c1-52d7230ca74a")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication2_1_2.ClearCurruntActiveToolCmd")]
public sealed class ClearCurrentActiveToolCmd : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
private IToolbarControl m_ToolbarControl;
public ClearCurrentActiveToolCmd()
{
//
// TODO: Define values for the public properties
//
base.m_category = "Custom Command"; //localizable text
base.m_caption = "清除当前活动工具"; //localizable text
base.m_message = "清除当前活动工具"; //localizable text
base.m_toolTip = "清除当前活动工具"; //localizable text
base.m_name = "清除当前活动工具"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
if (m_hookHelper.Hook is IToolbarControl)
{
m_ToolbarControl = m_hookHelper.Hook as IToolbarControl;
m_ToolbarControl.CurrentTool = null;
}
// TODO: Add ClearCurruntActiveToolCmd.OnClick implementation
}
#endregion
}
}
5、在Form1.cs的窗体load事件中添加代码,将该命令添加到toolstrip上。
axToolbarControl1.AddItem(new ClearCurrentActiveToolCmd(), -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
转载本文请联系原作者获取授权,同时请注明本文来自刘廷祥科学网博客。
链接地址:http://blog.sciencenet.cn/blog-3373120-1109422.html
本文介绍如何在ArcGIS的ToolbarControl中添加自定义命令,实现清除当前活动工具的功能。通过创建BaseCommand模版类,定义IToolbarControl接口,及在OnClick事件中设置CurrentTool属性为空来实现目标。
5600

被折叠的 条评论
为什么被折叠?



