目前所接触到的工具有两种,一种是以命令形式存在,如ControlsMapZoomToLastExtentForwardCommand等。一种是以交互工具形式存在的,如ControlsMapZoomInTool等。
命令形式的工具只需要创建传入交互对象,交互工具形式工具需要设置交互对象的CurrentTool
例如命令形式的
ICommand pmapPreView = new ControlsMapZoomToLastExtentBackCommand();
pmapPreView.OnCreate(axMapControl1.Object);//传入交互对象
pmapPreView.OnClick();//启动
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;//鼠标指针,不相关
axMapControl1.CurrentTool = null;//清楚当前视图中的工具
例如交互工具形式的
ICommand pPan = new ControlsMapPanTool();
pPan.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = pPan as ITool;//制定窗口
ToolbarControl自定义控件
要自定义控件必须要继承BaseCommand(命令形式的工具),BaseTool(交互形式的工具)
例如工具
class RemoveAllLayer : BaseCommand
{
protected IMapControl2 m_MapControl;
///构造函数,设置基类
public RemoveAllLayer()
{
base.m_caption = "清空数据";
base.m_toolTip = "清空视图数据";
base.m_message = "删除数据图层";
base.m_category = "清空数据";
string bmppath =Application.StartupPath + "[图标地址]\\empty.png";
base.m_bitmap = new System.Drawing.Bitmap(bmppath);
}
///初始化窗口
public override void OnCreate(object hook)
{
if (hook is IToolbarControl)
{
IToolbarControl pToolbarControl = (IToolbarControl)hook;
this.m_MapControl = pToolbarControl.Buddy as IMapControl2;
}
}
///命令所执行的东西
public override void OnClick()
{
for(int i=0;i< m_MapControl.Map.LayerCount;i++)
{
ILayer layer = m_MapControl.Map.Layer[0];
m_MapControl.Map.DeleteLayer(layer);
}
}
}
这样写的话在初始化的时候就只能用一次初始化一次,也就是如下:
axToolbarControl1.AddItem(new ImportPicSource.RemoveAllLayer(), -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
axToolbarControl2.AddItem(new ImportPicSource.RemoveAllLayer(), -1,-1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
而不能
ImportPicSource.RemoveAllLayer improtpic = new ImportPicSource.RemoveAllLayer();
axToolbarControl1.AddItem(improtpic, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
axToolbarControl2.AddItem(improtpic, -1, -1, false, -1, esriCommandStyles.esriCommandStyleIconOnly);
因为控件axToolbarControl1与axToolbarControl2所绑定的空间不一样时,类RemoveAllLayer中的变量m_MapControl将会被替换掉。也可是使用一下方法来不被替换,那就需要在OnClick事件中编些代码,去判断我当前所操作的窗口是哪个窗口。
如下
IntPtr pHandle = new IntPtr(m_hookHelper.ActiveView.ScreenDisplay.hWnd);
AxMapControl axMapControl = System.Windows.Forms.Form.FromHandle(pHandle) as AxMapControl;//对这个地图控件对象操作,会直接反应到主窗体的地图控件上
使用这样来获取当前所造作的MapControl
支持赞助:505180987@qq.com(支付宝)
培训联系QQ :505180987
欢迎关注微信订阅号: