//
添加每一
列的值
{
pRow[i]=pFeature.get_Value(i);
}
pTable.Rows.Add(pRow);
pFeature=pFeatureCursor.NextFeature();
}
dataGridView1.DataSource=pTable;
}
第二步:
建立右键菜单项
添加一个ContextMenuStrip控件,然后增加几个菜单项。
特别注意加入一个菜单项为“显示属性”。
判断在什么情况下显示右键菜单。
ILayerm_Layer;//沟通图层
privatevoidaxTOCControl1_OnMouseDown(object
ITOCControlEvents_OnMouseDownEvente)
sender,
{
if
(e.button==
2)
{
esriTOCControlItemItem=
esriTOCControlItem.esriTOCControlItemNone;
IBasicMappBasicMap=
ILayerpLayer=null
objectother=null
objectindex=null
axTOCControl1.HitTest(e.x,e.y,
pLayer,refother,refindex);
m_Layer=pLayer;
;
;
;
null
;
ref
//实现赋值
Item,
ref
pBasicMap,
ref
if
(Item==esriTOCControlItem.esriTOCControlItemLayer)
//点击的是图层的话,就显示右键菜单
{
contextMenuStrip2.Show(axTOCControl1,newSystem.Drawing.Point(e.x,e.y));
//显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示
}
}
}
private
void
显示属性
ToolStripMenuItem_Click(
object
sender,
EventArgse)
{
Form2frm=
newForm2(m_Layer);
//定义窗体,注意窗体的参
数
frm.Text=
"Attributesof"
+m_Layer.Name;
//显示标题
frm.ShowDialog();
//以对话框形式显示窗体
}
效果如下所示:
---------------------------------------------------------------------------------------
------------------
╔════════╗
╠════╣第A2个╠══════════════════════════
════════════════════════╣
╚════════╝
●·●第二种方法:
IToolbarMenu:
第一步:
新建菜单框架:
IToolbarMenum_TOCMapMenu=newToolbarMenu();
IToolbarMenum_TOCLayerMenu
=newToolbarMenu();
第二步:
在菜单框架中加入菜单项,用
AddItem
方法实现:
m_TOCLayerMenu.Caption=
"SubMenu";
//
二级菜单设置名称
m_TOCLayerMenu.AddItem(
newControlsMapZoomInToolClass(),
0,
0,
false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.AddItem(
newControlsMapZoomInToolClass(),
0,
1,
true
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.AddItem(
newControlsMapZoomInToolClass(),
0,
2,
false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCLayerMenu.SetHook(axMapControl1);
//
作用在MapControl
上
面
m_TOCMapMenu.AddItem(
newControlsAddDataCommandClass(),
0,
0,
false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(
newControlsMapFullExtentCommand(),
0,
1,false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(
newControlsMapZoomInToolClass(),
0,2,
true
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(
newControlsMapZoomOutToolClass(),
0,
3,
false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddItem(
newControlsPagePanToolClass(),
0,
4,
false
esriCommandStyles.esriCommandStyleIconAndText);
m_TOCMapMenu.AddSubMenu(m_TOCLayerMenu,
5,false
);
m_TOCMapMenu.SetHook(axMapControl1);
第三步:
TOCControl的MouseDown事件:
privatevoidaxTOCControl1_OnMouseDown(objectITOCControlEvents_OnMouseDownEvente)
sender,
{
esriTOCControlItemitem=
esriTOCControlItem.esriTOCControlItemNone;
IBasicMapmap=null;
ILayerlayer=null;
objectother=null;
objectindex=null;
axTOCControl1.HitTest(e.x,
e.y,
ref
item,ref
map,ref
layer,
ref
other,
refindex);
if
(e.button==
2)
{
if
(item==
esriTOCControlItem.esriTOCControlItemMap)
{
axTOCControl1.SelectItem(map,
null);
m_TOCMapMenu.PopupMenu(e.x,e.y,axTOCControl1.hWnd);
}
}
}
实现效果如下:
publicintAddItem(
objectitem,
intSubType,
intindex,
boolbeginGroup,
esriCommandStylesStyle
);
第一个参数:
菜单项的内容,功能实现。
第二个参数:
对于一个工具定义多个type的时候,才会用到,每一个int代表一个新的
实现。
第三个参数:
索引值,在菜单项上面显示的位置。
默认为-1,按书写顺序排序。
第四个参数:
是否开始一个新组,就是在其上面有一个“——”的效果。
第五个参数:
显示样式。
用SubType实现:
首先:
建立工具,这个时候类不仅要继承BaseCommand类,还要继承
ICommandSubType接口
---------------------------------------------------------------------------------------
------------------
╔════════╗
╠════╣第1A个╠══════════════════════════
════════════════════════╣
╚════════╝
●·●ICommandSubType接口:
Providesaccesstomembersthatdefineasubtypedcommand.
Members
Description
GetCount
ThenumberofcommandsdefinedwiththisCLSID.
SetSubType
Thesubtypeofthecommand.
using
using
using
using
using
using
using
using
System;
System.Drawing;
System.Runtime.InteropServices;
ESRI.ArcGIS.ADF.BaseClasses;
ESRI.ArcGIS.ADF.CATIDs;
ESRI.ArcGIS.Controls;
ESRI.ArcGIS.Carto;
ESRI.ArcGIS.SystemUI;
namespaceESRI_01
{
///
///SummarydescriptionforScaleThresholdCmd.
///
[Guid(
"41b9d7ee-ba25-47d1-9644-f9eca4243c34"
)]
[ClassInterface(ClassInterfaceType.None)]
[ProgId(
"ESRI_01.ScaleThresholdCmd"
)]
public
sealed
class
ScaleThresholdCmd:
BaseCommand,
ICommandSubType
//要多继承一个接口!
{
#regionCOMRegistrationFunction(s)
[ComRegisterFunction()]
[ComVisible(false)]
staticvoidRegisterFunction(TyperegisterType)
{
//RequiredforArcGISComponent
Category
Registrar
support
ArcGISCategoryRegistration(registerType);
//
//TODO:
AddanyCOMregistrationcodehere
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
staticvoidUnregisterFunction(TyperegisterType)
{
//RequiredforArcGISComponent
Category
Registrar
support
ArcGISCategoryUnregistration(registerType);
//
//TODO:
AddanyCOMunregistrationcodehere
//
}
#regionArcGISComponentCategoryRegistrargeneratedcode
///
///RequiredmethodforArcGISComponentCategoryregistration
-
///Donotmodifythecontentsofthismethodwiththecode
editor.
///
privatestatic
void
ArcGISCategoryRegistration(Type
registerType)
{
stringregKey=
string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}"
registerType.GUID);
ControlsCommands.Register(regKey);
}
///
///RequiredmethodforArcGISComponentCategory
unregistration-
///Donotmodifythecontentsofthismethodwiththecode
editor.
///
privatestatic
void
ArcGISCategoryUnregistration(Type
registerType)
{
stringregKey=
string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}"
registerType.GUID);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private
private
private
IHookHelperm_hookHelper;IMapControl4m_mapControl;longm_subType;
//用来分类的
public
ScaleThresholdCmd()
{
}
#regionOverridenClassMethods//重写的方法
///
///Occurswhenthiscommandiscreated
///
///
Instanceoftheapplication
///
publicoverridestringCaption//重写标题
{
get
{
if
(m_subType==
1)
{
return
"设置最大显示比例尺
";
}
else
if
(m_subType==
2)
{
return
"设置最小显示比例尺
";
}
else
{
return
"取消比例尺显示限制
";
}
}
}
publicoverrideboolEnabled//重写Enabled属性
{
get
{
boolenabled=true;//默认情况下都是true
ILayerlayer=(ILayer)m_mapControl.CustomProperty;
//获取所指的layer,在MouseDown事件中有获取到这个layer。
的
没有设置,则为
if
false
(m_subType==
!
3)
//如果选择的是
3,且最大和最小比例尺都
{
if
((layer.MaximumScale==
0)&&(layer.MinimumScale==
0))
{
enabled
=false
;
}
}
returnenabled;
}
}
public
override
void
OnCreate(
object
hook)
//重写创建方法
{
if(hook
m_mapControl
is
IMapControl4)
=(IMapControl4)hook;
//给
m_mapControl
赋值
}
///
///Occurswhenthiscommandisclicked
///
publicoverridevoidOnClick()
//重写单击方法
{
//TODO:
AddScaleThresholdCmd.OnClickimplementation
IMapmap=m_mapControl.Map;
ILayerlayer=m_mapControl.CustomPropertyasILayer;
if(m_subType==1)
{
layer.MaximumScale
=map.MapScale;
//为
1
的时候,实
现最大显示比例尺
}
else
if
(m_subType==
2)
{
layer.MinimumScale
=map.MapScale;
//为
2
的时候,实
现最小显示比例尺