在实现中,主要的是OnMouseDown事件的实现。OnMouseMove与OnMouseUp事件在identify工具中不用实现。
OnMouseDown事件实现了identify元素(用户自己添加的图标等)和要素(sde数据库中的地图要素)。
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="button">button按钮</param>
/// <param name="shift">shift键</param>
/// <param name="x">屏幕X坐标</param>
/// <param name="y">屏幕Y坐标</param>
public void OnMouseDown(int button, int shift, int x, int y)
{
m_isMouseDown = true;
//未选中图标
pointId = -1;
// 获取焦点地图的IActiveView对象
IActiveView pActiveView = m_pHookHelper.ActiveView.FocusMap as IActiveView;
//IDisplay 对象
IDisplay pDisplay = pActiveView.ScreenDisplay;
//IGraphicsContainer对象,图形容器
IGraphicsContainer pGraphicsContainer;
pGraphicsContainer = pActiveView.GraphicsContainer;
//Create a point
IPoint pPoint = new PointClass();
pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
//元素
IElement pElement;
//元素枚举
IEnumElement pEnumElement;
pEnumElement = pGraphicsContainer.LocateElements(pPoint,5);
if (pEnumElement != null)//i查询element
{
pElement = pEnumElement.Next();
while (pElement != null)
{
if (pElement.Geometry.GeometryType.Equals(esriGeometryType.esriGeometryPoint))
{
m_element = pElement;
//点
IPoint mPoint = new PointClass();
mPoint = pElement.Geometry as IPoint;
//类的属性PointId 设置
pointId = mPoint.ID;
}
pElement = pEnumElement.Next();
}
}
else//i查询要素
{
//实现要素的查询
IdentifyFeature(pPoint);
}
}
/// <summary>
/// 查找要素
/// </summary>
/// <param name="pPoint">点</param>
private void IdentifyFeature(IPoint pPoint)
{
//清空以前查询的数据
m_identifyObj = null;
m_feature = null;
//查询对象,用于查询要素
IIdentify pIdentify;
//查询,用于控制一个图层的要素查询控制
IIdentifyObj pIdentifyObj;
//要素查询对象
IFeatureIdentifyObj pFeatureIdentifyObj;
//要素行查询对象
IRowIdentifyObject pRowIdentifyObj;
//查询的要素
IFeature pFeature;
//数组,存放要素查询对象
IArray pIdArray;
//地图
IMap pMap;
pMap = m_pHookHelper.FocusMap;
// 获取焦点地图的IActiveView对象
IActiveView pActiveView = m_pHookHelper.ActiveView.FocusMap as IActiveView;
//显示对象
IScreenDisplay pScreenDisplay = pActiveView.ScreenDisplay;
//显示转换对象
IDisplayTransformation pDisplayTrnsformation;
pDisplayTrnsformation = pScreenDisplay.DisplayTransformation;
//范围
IEnvelope pEnvlope = new EnvelopeClass();
pEnvlope = pPoint.Envelope;
pEnvlope.Expand(pDisplayTrnsformation.VisibleBounds.Width / 100,
pDisplayTrnsformation.VisibleBounds.Height / 100,
false);
//图层组
IGroupLayer pGroupLayer = new GroupLayerClass();
for (int i = 0; i < pMap.LayerCount; i++)
{
pGroupLayer.Add(pMap.get_Layer(i));
}
pIdentify = pGroupLayer as IIdentify;
pIdArray = pIdentify.Identify(pEnvlope as IGeometry);
//get the FeatureIdentifyObject
if ((pIdArray != null) && (pIdArray.Count > 0))
{
pFeatureIdentifyObj = pIdArray.get_Element(0) as IFeatureIdentifyObj;
pIdentifyObj = pFeatureIdentifyObj as IIdentifyObj;
pRowIdentifyObj = pFeatureIdentifyObj as IRowIdentifyObject;
pFeature = pRowIdentifyObj.Row as IFeature;
m_feature = pFeature;
m_identifyObj = pIdentifyObj;
pIdentifyObj.Flash(pScreenDisplay);
}
}//end of IdentifyFeature