GIS二次开发之弹出菜单
前几遍都在讲自定义工具,在开始将弹出菜单之前先看看功能效果图吧。
可以看到之前几个随笔中讲到的自定义工具在这些效果图中都有展示,同时也展示了这次要将的弹出菜单实现后的效果。在主地图的视图中右击鼠标会弹出菜单,这个菜单里面的功能和工具栏中的功能一样,在toc导航工具中右击地图的时候和右击图层的时候弹出的菜单内容是不同的。选择map的时候可以添加图层和全图,选择layer的时候可以删除当前选择的图层和刷新。
这讲主要说说弹出菜单的实现代码,其他的代码在以后会慢慢补充。
先看看主视图中弹出菜单的实现代码,和在前几讲中添加的方法一样,只是这次的容器是ToolbarMenu.看看代码的实现:
代码
//axMapMain的右击菜单
private IToolbarMenu mapMenu = new ToolbarMenuClass();
/// <summary>
/// 为axMapMain定义右击菜单
/// </summary>
private void AddMapMainPopuMenu()
{
//progID添加
string progID = "CSharpDotNETCommands.MapPan";
mapMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapZoomIn";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapZoomOut";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapFullExtent";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapGoBack";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapGoForward";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
mapMenu.SetHook(axMapMain); //axMapMain右键菜单
}
private IToolbarMenu mapMenu = new ToolbarMenuClass();
/// <summary>
/// 为axMapMain定义右击菜单
/// </summary>
private void AddMapMainPopuMenu()
{
//progID添加
string progID = "CSharpDotNETCommands.MapPan";
mapMenu.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapZoomIn";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapZoomOut";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapFullExtent";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapGoBack";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapGoForward";
mapMenu.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
mapMenu.SetHook(axMapMain); //axMapMain右键菜单
}
/// <summary>
/// axMapMain右击菜单
/// </summary>
private void axMapMain_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 2)
{
mapMenu.PopupMenu(e.x, e.y, axMapMain.hWnd);
}
}
/// axMapMain右击菜单
/// </summary>
private void axMapMain_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
{
if (e.button == 2)
{
mapMenu.PopupMenu(e.x, e.y, axMapMain.hWnd);
}
}
这样在主视图中右键的时候就会弹出mapmenu菜单。
下面看看在toc中弹出菜单的实现代码:
代码
//axTocControl的右击菜单
private IToolbarMenu axTocMenu1 = new ToolbarMenuClass();
private IToolbarMenu axTocMenu2 = new ToolbarMenuClass();
/// <summary>
/// 为axTocControl中map定义右击菜单
/// </summary>
private void AddTocPopuMenu1()
{
string progID = "CSharpDotNETCommands.MapFullExtent";
axTocMenu1.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapAddLayer";
axTocMenu1.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
}
///<summary>
///为axTocControl中layer定义右击菜单
///</summary>
private void AddTocPopuMenu2()
{
string progId = "CSharpDotNETCommands.MapFlash";
axTocMenu2.AddItem(progId, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progId = "CSharpDotNETCommands.MapDelLayer";
axTocMenu2.AddItem(progId, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
}
///<summary>
///显示弹出菜单
///</summary>
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemMap;
IBasicMap pBasicMap = null;
ILayer pLayer = null;
object unk = null;
object data = null;
axTOCControl1.HitTest(e.x, e.y, ref item, ref pBasicMap, ref pLayer, ref unk, ref data);
if (item == esriTOCControlItem.esriTOCControlItemMap) //选中Map的时候
{
axTOCControl1.SelectItem(pBasicMap, null);
if (e.button == 2) //AddData
{
axTocMenu1.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
}
}
else if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
axTOCControl1.SelectItem(pLayer, null);
if (e.button == 2) //delete
{
axTocMenu2.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
}
}
}
private IToolbarMenu axTocMenu1 = new ToolbarMenuClass();
private IToolbarMenu axTocMenu2 = new ToolbarMenuClass();
/// <summary>
/// 为axTocControl中map定义右击菜单
/// </summary>
private void AddTocPopuMenu1()
{
string progID = "CSharpDotNETCommands.MapFullExtent";
axTocMenu1.AddItem(progID, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progID = "CSharpDotNETCommands.MapAddLayer";
axTocMenu1.AddItem(progID, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
}
///<summary>
///为axTocControl中layer定义右击菜单
///</summary>
private void AddTocPopuMenu2()
{
string progId = "CSharpDotNETCommands.MapFlash";
axTocMenu2.AddItem(progId, -1, -1, false, esriCommandStyles.esriCommandStyleIconAndText);
progId = "CSharpDotNETCommands.MapDelLayer";
axTocMenu2.AddItem(progId, -1, -1, true, esriCommandStyles.esriCommandStyleIconAndText);
}
///<summary>
///显示弹出菜单
///</summary>
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemMap;
IBasicMap pBasicMap = null;
ILayer pLayer = null;
object unk = null;
object data = null;
axTOCControl1.HitTest(e.x, e.y, ref item, ref pBasicMap, ref pLayer, ref unk, ref data);
if (item == esriTOCControlItem.esriTOCControlItemMap) //选中Map的时候
{
axTOCControl1.SelectItem(pBasicMap, null);
if (e.button == 2) //AddData
{
axTocMenu1.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
}
}
else if (item == esriTOCControlItem.esriTOCControlItemLayer)
{
axTOCControl1.SelectItem(pLayer, null);
if (e.button == 2) //delete
{
axTocMenu2.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
}
}
}
添加如上代码就可以实现一开始截图中的效果。至于添加图层,删除图层的代码在下一随笔中贴出