Exemple #1
0
        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            //屏幕坐标点转化为地图坐标点
            //pPointPt = (axMapControl1.Map as IActiveView).ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
            IGeometry geometry = null;

            // FormSelection formSelection = new FormSelection();
            if (e.button == 1)//左键
            {
                IActiveView pActiveView = axMapControl1.ActiveView;
                IEnvelope   pEnvelope   = new EnvelopeClass();

                switch (pMouseOperate)
                {
                    #region 拉框放大

                case "ZoomIn":
                    pEnvelope = axMapControl1.TrackRectangle();
                    //如果拉框范围为空则返回
                    if (pEnvelope == null || pEnvelope.IsEmpty || pEnvelope.Height == 0 || pEnvelope.Width == 0)
                    {
                        return;
                    }
                    //如果有拉框范围,则放大到拉框范围
                    pActiveView.Extent = pEnvelope;
                    pActiveView.Refresh();
                    break;

                    #endregion

                    #region 拉框缩小

                case "ZoomOut":
                    pEnvelope = axMapControl1.TrackRectangle();

                    //如果拉框范围为空则退出
                    if (pEnvelope == null || pEnvelope.IsEmpty || pEnvelope.Height == 0 || pEnvelope.Width == 0)
                    {
                        return;
                    }
                    //如果有拉框范围,则以拉框范围为中心,缩小倍数为:当前视图范围/拉框范围
                    else
                    {
                        double dWidth  = pActiveView.Extent.Width * pActiveView.Extent.Width / pEnvelope.Width;
                        double dHeight = pActiveView.Extent.Height * pActiveView.Extent.Height / pEnvelope.Height;
                        double dXmin   = pActiveView.Extent.XMin -
                                         ((pEnvelope.XMin - pActiveView.Extent.XMin) * pActiveView.Extent.Width /
                                          pEnvelope.Width);
                        double dYmin = pActiveView.Extent.YMin -
                                       ((pEnvelope.YMin - pActiveView.Extent.YMin) * pActiveView.Extent.Height /
                                        pEnvelope.Height);
                        double dXmax = dXmin + dWidth;
                        double dYmax = dYmin + dHeight;
                        pEnvelope.PutCoords(dXmin, dYmin, dXmax, dYmax);
                    }
                    pActiveView.Extent = pEnvelope;
                    pActiveView.Refresh();
                    break;

                    #endregion

                    #region 漫游

                case "Pan":
                    axMapControl1.Pan();
                    break;

                    #endregion

                    //#region 选择要素

                    //case "SelFeature":
                    //    IEnvelope pEnv = mainMapControl.TrackRectangle();
                    //    IGeometry pGeo = pEnv as IGeometry;
                    //    //矩形框若为空,即为点选时,对点范围进行扩展
                    //    if (pEnv.IsEmpty == true)
                    //    {
                    //        tagRECT r;
                    //        r.left = e.x - 5;
                    //        r.top = e.y - 5;
                    //        r.right = e.x + 5;
                    //        r.bottom = e.y + 5;
                    //        pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4);
                    //        pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference;
                    //    }
                    //    pGeo = pEnv as IGeometry;
                    //    mainMapControl.Map.SelectByShape(pGeo, null, false);
                    //    mainMapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                    //    break;

                    //#endregion

                    //#region 区域导出
                    //case "ExportRegion":
                    //    //删除视图中数据
                    //    mainMapControl.ActiveView.GraphicsContainer.DeleteAllElements();
                    //    mainMapControl.ActiveView.Refresh();
                    //    IPolygon pPolygon = DrawPolygon(mainMapControl);
                    //    if (pPolygon == null) return;
                    //    ExportMap.AddElement(pPolygon, mainMapControl.ActiveView);
                    //    if (frmExpMap == null || frmExpMap.IsDisposed)
                    //    {
                    //        frmExpMap = new FormExportMap(mainMapControl);
                    //    }
                    //    frmExpMap.IsRegion = true;
                    //    frmExpMap.GetGeometry = pPolygon as IGeometry;
                    //    frmExpMap.Show();
                    //    frmExpMap.Activate();
                    //    break;
                    //#endregion

                    //#region 距离量算
                    //case "MeasureLength":
                    //    //判断追踪线对象是否为空,若是则实例化并设置当前鼠标点为起始点
                    //    if (pNewLineFeedback == null)
                    //    {
                    //        //实例化追踪线对象
                    //        pNewLineFeedback = new NewLineFeedbackClass();
                    //        pNewLineFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
                    //        //设置起点,开始动态线绘制
                    //        pNewLineFeedback.Start(pPointPt);
                    //        dToltalLength = 0;
                    //    }
                    //    else //如果追踪线对象不为空,则添加当前鼠标点
                    //    {
                    //        pNewLineFeedback.AddPoint(pPointPt);
                    //    }
                    //    //pGeometry = m_PointPt;
                    //    if (dSegmentLength != 0)
                    //    {
                    //        dToltalLength = dToltalLength + dSegmentLength;
                    //    }
                    //    break;
                    //#endregion

                    //#region 面积量算
                    //case "MeasureArea":
                    //    if (pNewPolygonFeedback == null)
                    //    {
                    //        //实例化追踪面对象
                    //        pNewPolygonFeedback = new NewPolygonFeedback();
                    //        pNewPolygonFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
                    //        ;
                    //        pAreaPointCol.RemovePoints(0, pAreaPointCol.PointCount);
                    //        //开始绘制多边形
                    //        pNewPolygonFeedback.Start(pPointPt);
                    //        pAreaPointCol.AddPoint(pPointPt, ref missing, ref missing);
                    //    }
                    //    else
                    //    {
                    //        pNewPolygonFeedback.AddPoint(pPointPt);
                    //        pAreaPointCol.AddPoint(pPointPt, ref missing, ref missing);
                    //    }
                    //    break;
                    //#endregion

                    //#region 要素选择
                    //case "SelectFeature":
                    //    IPoint point = new PointClass();
                    //    IGeometry pGeometry = point as IGeometry;
                    //    mainMapControl.Map.SelectByShape(pGeometry, null, false);
                    //    mainMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                    //    break;
                    //#endregion

                    /*  #region 点选
                     * case "PointSel":
                     *
                     *     ESRI.ArcGIS.Geometry.Point pt = new ESRI.ArcGIS.Geometry.Point();
                     *     pt.X = e.mapX;
                     *     pt.Y = e.mapY;
                     *     geometry = pt as IGeometry;
                     *     axMapControl1.Map.SelectByShape(geometry, null, false);
                     *     axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                     *     MessageBox.Show("点选");
                     *     //将当前主窗体中MapControl控件中的Map对象赋值给FormQueryByAttribute窗体的CurrentMap属性
                     *     //formSelection.CurrentMap = axMapControl1.Map;
                     *     //显示属性查询窗体
                     *    // formSelection.Show();
                     *     break;
                     #endregion*/
                    /*      #region 框选
                     *    case "RecSel":
                     *        geometry = axMapControl1.TrackRectangle();
                     *        axMapControl1.Map.SelectByShape(geometry, null, false);
                     *        axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                     *
                     *        //将当前主窗体中MapControl控件中的Map对象赋值给FormQueryByAttribute窗体的CurrentMap属性
                     *        formSelection.CurrentMap = axMapControl1.Map;
                     *        //显示属性查询窗体
                     *        formSelection.Show();
                     *        break;
                     #endregion */
                    /*       #region 圆选
                     *     case "CircleSel":
                     *         geometry = axMapControl1.TrackCircle();
                     *         axMapControl1.Map.SelectByShape(geometry, null, false);
                     *         axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                     *
                     *         //将当前主窗体中MapControl控件中的Map对象赋值给FormQueryByAttribute窗体的CurrentMap属性
                     *         formSelection.CurrentMap = axMapControl1.Map;
                     *         //显示属性查询窗体
                     *         formSelection.Show();
                     *         break;
                     #endregion*/
                    #region 点选
                case "NTinfo":
                    /*  ESRI.ArcGIS.Geometry.Point pt1 = new ESRI.ArcGIS.Geometry.Point();
                     * pt1.X = e.mapX;
                     * pt1.Y = e.mapY;
                     * axMapControl1.Map.SelectByShape(pt1.Envelope, null, false);*///不能用点,地类图斑图层都是面元素

                    IGeometry   g = null;
                    IEnvelope   pEnv;
                    IActiveView pActiveView1 = axMapControl1.ActiveView;
                    IMap        pMap         = axMapControl1.Map;
                    pEnv = axMapControl1.TrackRectangle();
                    if (pEnv.IsEmpty == true)
                    {
                        ESRI.ArcGIS.esriSystem.tagRECT r;
                        r.bottom = e.y + 5;
                        r.top    = e.y - 5;
                        r.left   = e.x - 5;
                        r.right  = e.x + 5;
                        pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnv, ref r, 4);
                        pEnv.SpatialReference = pActiveView.FocusMap.SpatialReference;
                    }
                    g = pEnv as IGeometry;
                    axMapControl1.Map.SelectByShape(g, null, false);
                    axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

                    //获取选取的要素集
                    IEnumFeature pEnumFeature = axMapControl1.Map.FeatureSelection as IEnumFeature;
                    IFeature     pFeature     = pEnumFeature.Next();
                    while (pFeature != null)
                    {
                        //string str1 = pFeature.OID.ToString();
                        string str2 = pFeature.get_Value(2).ToString();
                        MessageBox.Show("图斑号:" + Convert.ToInt32(str2));
                        pFeature = pEnumFeature.Next();
                        if (fi == null || fi.IsDisposed)
                        {
                            fi            = new FarmInfo();
                            fi.currentMap = axMapControl1.Map;
                            fi.Show();
                        }
                        fi.setTBH(Convert.ToInt32(str2));
                    }

                    break;

                    #endregion
                default:
                    break;
                }
            }
            else if (e.button == 2)
            {
                pMouseOperate = "";
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
        }