ArcEngine二次开发,TOCControl控件上使用contextMenuStrip

右键菜单,在二次开发中很实用,以前没用过,最近通过一本书了解到,一直想找这么一个控件来用.  

一般的控件,将contextMenuStrip控件拖到所依托的控件上,然后输入自己想要的几个功能.  在所依托的控件的属性里有个"ContextMenuStrip",选择ContextMenuStripX,这样就算是绑定了.

但是TOCControl控件绑定ContextMenuStrip就不同了,如果还像上面那样绑定就不管用了,绑定的过程没这么简单,要用代码来实现,首先要声明一个全局变量 private ILayer m_Layer; 然后在TOCControl1的OnMouseDown事件中添加如下代码 :

private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button==2)
{
esriTOCControlItem Item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasicMap = null;
ILayer pLayer = null;
object other = null;
object index = null;
axTOCControl1.HitTest(e.x,e.y,ref Item,ref pBasicMap,ref pLayer,ref other,ref index); //以指定的坐标返回TOCControl中的项目
m_Layer = pLayer;
if (Item==esriTOCControlItem.esriTOCControlItemLayer)
{
contextMenuStrip1.Show(axTOCControl1,new System.Drawing.Point(e.x,e.y));
}
}

}

这样ContextMenuStrip就可以用了.

另外还有一种方法是纯代码形式的,没试验过:http://developer.51cto.com/art/201104/256774.htm.

参考资料:http://developer.51cto.com/art/201104/256774.htm

以及官网的,里面有sample:

http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//0016000000r7000000

 

悲观者更正确,乐观者更成长。时代大潮下,充满着机遇和风险。 目标不同,选择也就不同,人生没有标准答案,对大多数人而言,还是要不停地提高自己,这个世界什么都能快,但学习从来都没有捷径,或者说学习已是捷径。
原文地址:https://www.cnblogs.com/youzi-xuchongyou/p/7372505.html