DisplayFeedbackFrm.cs
上传用户:songlsx
上传日期:2022-06-19
资源大小:2227k
文件大小:21k
源码类别:

GIS编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ESRI.ArcGIS.Carto;
  9. using ESRI.ArcGIS.Display;
  10. using ESRI.ArcGIS.Controls;
  11. using ESRI.ArcGIS.esriSystem;
  12. using ESRI.ArcGIS.Geometry;
  13. using ESRI.ArcGIS.SystemUI;
  14. namespace LinGIS
  15. {
  16.     public partial class DisplayFeedbackFrm : Form
  17.     {
  18.         public DisplayFeedbackFrm()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.         
  23.         private IGraphicsContainer pGraphicContainer;
  24.         private IActiveView pActiveView;
  25.         private IDisplayFeedback pDisplayFeedback;
  26.         private IScreenDisplay pScreenDisplay;
  27.         private IElement hitElement;//鼠标点击的element
  28.         private IEnumElement pEnumElement;//element集合,移动几何对象中需要用到
  29.         private IPoint moveGeometryStartPoint;//移动几何对象时开始的位置
  30.         private void Form1_Load(object sender, EventArgs e)
  31.         {
  32.             this.pGraphicContainer = this.axMapControl1.ActiveView.FocusMap as IGraphicsContainer;
  33.             this.pActiveView = this.axMapControl1.ActiveView;
  34.             this.pScreenDisplay = this.axMapControl1.ActiveView.ScreenDisplay;
  35.         }
  36.         /// <summary>
  37.         /// 鼠标点击
  38.         /// </summary>
  39.         /// <param name="sender"></param>
  40.         /// <param name="e"></param>
  41.         private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
  42.         {
  43.             try
  44.             {
  45.                 if (e.button == 1)//按左键的话
  46.                 {
  47.                     IPoint pPoint = new PointClass();//在鼠标点击的位置生成一个点
  48.                     pPoint.PutCoords(e.mapX, e.mapY);
  49.                     if (this.btnNewPolyline.Checked)//画多义线
  50.                     {
  51.                         if (this.pDisplayFeedback == null)//如果是第一次点击,就建立第一个节点
  52.                         {
  53.                             this.pDisplayFeedback = new NewLineFeedbackClass();
  54.                             this.pDisplayFeedback.Display = this.pScreenDisplay;
  55.                             ((INewLineFeedback)this.pDisplayFeedback).Start(pPoint);
  56.                         }
  57.                         else//如果不是第一次点击,就添加节点
  58.                         {
  59.                             ((INewLineFeedback)this.pDisplayFeedback).AddPoint(pPoint);
  60.                         }
  61.                     }
  62.                     else if (this.btnLineMovePoint.Checked)//移动多义线节点
  63.                     {
  64.                         IElement pElement = this.getElement(pPoint, esriGeometryType.esriGeometryPolyline);
  65.                         if (pElement != null)
  66.                         {
  67.                             IPolyline pPolyline = pElement.Geometry as IPolyline;
  68.                             IHitTest pHitTest = pPolyline as IHitTest;
  69.                             IPoint hitPoint = new PointClass();
  70.                             double distance = 0;
  71.                             bool isOnRightSide = false;
  72.                             int hitPartIndex = 0;
  73.                             int hitSegmentIndex = 0;
  74.                             bool isHit = pHitTest.HitTest(pPoint, this.axMapControl1.ActiveView.Extent.Width / 100, esriGeometryHitPartType.esriGeometryPartVertex, hitPoint, ref distance, ref hitPartIndex, ref hitSegmentIndex, ref isOnRightSide);
  75.                             if (isHit)
  76.                             {
  77.                                 this.pDisplayFeedback = new LineMovePointFeedbackClass();
  78.                                 this.pDisplayFeedback.Display = this.pScreenDisplay;
  79.                                 ((ILineMovePointFeedback)this.pDisplayFeedback).Start(pPolyline, hitSegmentIndex, pPoint);
  80.                             }
  81.                         }
  82.                     }
  83.                     else if (this.btnNewCircle.Checked)//画圆
  84.                     {
  85.                         this.pDisplayFeedback = new NewCircleFeedbackClass();
  86.                         this.pDisplayFeedback.Display = this.pScreenDisplay;
  87.                         ((INewCircleFeedback)this.pDisplayFeedback).Start(pPoint);
  88.                     }
  89.                     else if (this.btnNewEnvelope.Checked)//画矩形
  90.                     {
  91.                         this.pDisplayFeedback = new NewEnvelopeFeedbackClass();
  92.                         this.pDisplayFeedback.Display = this.pScreenDisplay;
  93.                         ((INewEnvelopeFeedback)this.pDisplayFeedback).Constraint = esriEnvelopeConstraints.esriEnvelopeConstraintsNone;
  94.                         ((INewEnvelopeFeedback)this.pDisplayFeedback).Start(pPoint);
  95.                     }
  96.                     else if (this.btnNewPolygon.Checked)//画多边形
  97.                     {
  98.                         if (this.pDisplayFeedback == null)//如果是第一次点击,就建立第一个节点
  99.                         {
  100.                             this.pDisplayFeedback = new NewPolygonFeedbackClass();
  101.                             this.pDisplayFeedback.Display = this.pScreenDisplay;
  102.                             ((INewPolygonFeedback)this.pDisplayFeedback).Start(pPoint);
  103.                         }
  104.                         else//如果不是第一次点击,就添加节点
  105.                         {
  106.                             ((INewPolygonFeedback)this.pDisplayFeedback).AddPoint(pPoint);
  107.                         }
  108.                     }
  109.                     else if (this.btnPolygonMovePoint.Checked)//移动多边形节点
  110.                     {
  111.                         IElement pElement = this.getElement(pPoint, esriGeometryType.esriGeometryPolygon);
  112.                         if (pElement != null)
  113.                         {
  114.                             IPolygon pPolygon = pElement.Geometry as IPolygon;
  115.                             IHitTest pHitTest = pPolygon as IHitTest;
  116.                             IPoint hitPoint = new PointClass();
  117.                             double distance = 0;
  118.                             bool isOnRightSide = true;
  119.                             int hitPartIndex = 0;
  120.                             int hitSegmentIndex = 0;
  121.                             bool isHit = pHitTest.HitTest(pPoint, this.axMapControl1.ActiveView.Extent.Width / 100, esriGeometryHitPartType.esriGeometryPartVertex, hitPoint, ref distance, ref hitPartIndex, ref hitSegmentIndex, ref isOnRightSide);
  122.                             if (isHit)
  123.                             {
  124.                                 this.pDisplayFeedback = new PolygonMovePointFeedbackClass();
  125.                                 this.pDisplayFeedback.Display = this.pScreenDisplay;
  126.                                 ((IPolygonMovePointFeedback)this.pDisplayFeedback).Start(pPolygon, hitSegmentIndex, pPoint);
  127.                             }
  128.                         }
  129.                     }
  130.                     else if (this.btnNewBezierCurve.Checked)//新建Bezier曲线
  131.                     {
  132.                         if (this.pDisplayFeedback == null)//如果是第一次点击
  133.                         {
  134.                             this.pDisplayFeedback = new NewBezierCurveFeedbackClass();
  135.                             this.pDisplayFeedback.Display = this.pScreenDisplay;
  136.                             ((INewBezierCurveFeedback)this.pDisplayFeedback).Start(pPoint);
  137.                         }
  138.                         else//如果不是第一次点击
  139.                         {
  140.                             ((INewBezierCurveFeedback)this.pDisplayFeedback).AddPoint(pPoint);
  141.                         }
  142.                     }
  143.                     else if (this.btnMoveGeometry.Checked)//移动几何对象
  144.                     {
  145.                         this.pEnumElement = this.pGraphicContainer.LocateElements(pPoint, this.pActiveView.Extent.Width / 100);
  146.                         if (this.pEnumElement != null)
  147.                         {
  148.                             this.pDisplayFeedback = new MoveGeometryFeedbackClass();
  149.                             this.pDisplayFeedback.Display = this.pScreenDisplay;
  150.                             IElement pElement;
  151.                             this.pEnumElement.Reset();
  152.                             //需要逐个逐个添加Geometry
  153.                             for (pElement = this.pEnumElement.Next(); pElement != null; pElement = this.pEnumElement.Next())
  154.                             {
  155.                                 ((IMoveGeometryFeedback)this.pDisplayFeedback).AddGeometry(pElement.Geometry);
  156.                             }
  157.                             ((IMoveGeometryFeedback)this.pDisplayFeedback).Start(pPoint);
  158.                             this.moveGeometryStartPoint = pPoint;
  159.                         }
  160.                     }
  161.                     else if (this.btnStretchLine.Checked)//拉伸多义线
  162.                     {
  163.                         IElement pElement = this.getElement(pPoint, esriGeometryType.esriGeometryPolyline);
  164.                         if (pElement != null)
  165.                         {
  166.                             IPolyline pPolyline = pElement.Geometry as IPolyline;
  167.                             this.pDisplayFeedback = new StretchLineFeedbackClass();
  168.                             this.pDisplayFeedback.Display = this.pScreenDisplay;
  169.                             ((IStretchLineFeedback)this.pDisplayFeedback).Anchor = pPolyline.FromPoint;
  170.                             ((IStretchLineFeedback)this.pDisplayFeedback).Start(pPolyline, pPoint);
  171.                         }
  172.                     }
  173.                 }
  174.                 else if (e.button == 2)//按右键的就把画面清空
  175.                 {
  176.                     if (this.pDisplayFeedback == null)
  177.                     {
  178.                         this.pGraphicContainer.DeleteAllElements();
  179.                         this.axMapControl1.Refresh();
  180.                     }
  181.                 }
  182.             }
  183.             catch (Exception ex)
  184.             {
  185.                 MessageBox.Show(ex.Message);
  186.             }
  187.         }
  188.         
  189.         
  190.         /// <summary>
  191.         /// 根据点击位置和需要的element类型,取得在该位置的给类型的element
  192.         /// </summary>
  193.         /// <param name="pPoint"></param>
  194.         /// <param name="geometryType"></param>
  195.         /// <returns></returns>
  196.         private IElement getElement(IPoint pPoint,esriGeometryType geometryType)
  197.         {
  198.             IEnumElement pEnumElement = this.pGraphicContainer.LocateElements(pPoint, this.axMapControl1.ActiveView.Extent.Width / 100);
  199.             if (pEnumElement != null)
  200.             {
  201.                 pEnumElement.Reset();
  202.                 IElement pElement;
  203.                 for (pElement=pEnumElement.Next(); pElement!= null;pElement=pEnumElement.Next())
  204.                 {
  205.                     if (pElement.Geometry.GeometryType == geometryType)
  206.                     {
  207.                         this.hitElement = pElement;
  208.                         return pElement;
  209.                     }
  210.                 }
  211.                 return null;
  212.             }
  213.             return null;
  214.         }
  215.         
  216.         /// <summary>
  217.         /// 鼠标移动,各种工作中鼠标移动的处理是一致的
  218.         /// </summary>
  219.         /// <param name="sender"></param>
  220.         /// <param name="e"></param>
  221.         private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
  222.         {
  223.             IPoint pPoint = new PointClass();
  224.             pPoint.PutCoords(e.mapX, e.mapY);
  225.             if (this.pDisplayFeedback != null)
  226.             {
  227.                 this.pDisplayFeedback.MoveTo(pPoint);
  228.             }
  229.         }
  230.         /// <summary>
  231.         /// 双击鼠标
  232.         /// </summary>
  233.         /// <param name="sender"></param>
  234.         /// <param name="e"></param>
  235.         private void axMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e)
  236.         {
  237.             if (this.pDisplayFeedback != null)
  238.             {
  239.                 IGeometry pGeometry = null;
  240.                 if (this.btnNewPolyline.Checked)//新建多义线
  241.                 {
  242.                     pGeometry = ((INewLineFeedback)this.pDisplayFeedback).Stop();
  243.                 }
  244.                 else if (this.btnNewPolygon.Checked)//新建多边形
  245.                 {
  246.                     pGeometry = ((INewPolygonFeedback)this.pDisplayFeedback).Stop();
  247.                 }
  248.                 else if (this.btnNewBezierCurve.Checked)//新建Bezier曲线
  249.                 {
  250.                     pGeometry = ((INewBezierCurveFeedback)this.pDisplayFeedback).Stop();
  251.                 }
  252.                 this.pDisplayFeedback = null;
  253.                 this.AddElement(pGeometry);
  254.             }
  255.         }
  256.         
  257.         /// <summary>
  258.         /// 把Geometry弄成一个element,添加到地图上
  259.         /// </summary>
  260.         /// <param name="pGeometry"></param>
  261.         private void AddElement(IGeometry pGeometry)
  262.         {
  263.             try
  264.             {
  265.                 IElement pElement = null;
  266.                 ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
  267.                 pSimpleLineSymbol.Width = 2;
  268.                 pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
  269.                 ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
  270.                 pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
  271.                 pSimpleFillSymbol.Outline = pSimpleLineSymbol;
  272.                 if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolyline)//多义线
  273.                 {
  274.                     ILineElement pLineElement = new LineElementClass();
  275.                     pLineElement.Symbol = pSimpleLineSymbol;
  276.                     pElement = pLineElement as IElement;
  277.                     pElement.Geometry = pGeometry;
  278.                 }
  279.                 else if (pGeometry.GeometryType == esriGeometryType.esriGeometryCircularArc)//圆
  280.                 {
  281.                     ISegmentCollection pSegmentCollection;
  282.                     pSegmentCollection = new PolygonClass();
  283.                     object Missing = Type.Missing;//注意
  284.                     pSegmentCollection.AddSegment(pGeometry as ISegment, ref Missing, ref Missing);//后两个参数必须是这样,帮助说的,为什么??
  285.                     pElement = new CircleElementClass();
  286.                     pElement.Geometry = pSegmentCollection as IGeometry;
  287.                     IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
  288.                     pFillShapeElement.Symbol = pSimpleFillSymbol;
  289.                 }
  290.                 else if (pGeometry.GeometryType == esriGeometryType.esriGeometryEnvelope)//矩形
  291.                 {
  292.                     pElement = new RectangleElementClass();
  293.                     pElement.Geometry = pGeometry;
  294.                     IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
  295.                     pFillShapeElement.Symbol = pSimpleFillSymbol;
  296.                 }
  297.                 else if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolygon)//多边形
  298.                 {
  299.                     pElement = new PolygonElementClass();
  300.                     pElement.Geometry = pGeometry;
  301.                     IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
  302.                     pFillShapeElement.Symbol = pSimpleFillSymbol;
  303.                 }
  304.                 else if (pGeometry.GeometryType == esriGeometryType.esriGeometryBezier3Curve)//Bezier曲线
  305.                 {
  306.                     pElement = new LineElementClass();
  307.                     pElement.Geometry = pGeometry;
  308.                     ILineElement pLineElement = pElement as ILineElement;
  309.                     pLineElement.Symbol = pSimpleLineSymbol;
  310.                 }
  311.                 this.pGraphicContainer.AddElement(pElement, 0);
  312.                 this.axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
  313.             }
  314.             catch (Exception ex)
  315.             {
  316.                 MessageBox.Show(ex.Message);
  317.             }
  318.         }
  319.         private void btnNewPolyline_Click(object sender, EventArgs e)
  320.         {
  321.             foreach (ToolStripItem i in this.toolStrip1.Items)
  322.             {
  323.                 ((ToolStripButton)i).Checked = false;
  324.             }
  325.             ((ToolStripButton)sender).Checked = true;
  326.             this.pDisplayFeedback = null;
  327.         }
  328.         /// <summary>
  329.         /// 松开鼠标
  330.         /// </summary>
  331.         /// <param name="sender"></param>
  332.         /// <param name="e"></param>
  333.         private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
  334.         {
  335.             if (this.pDisplayFeedback != null)
  336.             {
  337.                 if (this.btnLineMovePoint.Checked)//移动多义线节点
  338.                 {
  339.                     IGeometry pGeometry = ((ILineMovePointFeedback)this.pDisplayFeedback).Stop();
  340.                     if (pGeometry != null)
  341.                     {
  342.                         this.hitElement.Geometry = pGeometry;
  343.                         this.pGraphicContainer.UpdateElement(this.hitElement);
  344.                     }
  345.                     this.hitElement = null;
  346.                     this.pDisplayFeedback = null;
  347.                     this.axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
  348.                 }
  349.                 else if (this.btnNewCircle.Checked)//画圆
  350.                 {
  351.                     IGeometry pGeometry = ((INewCircleFeedback)this.pDisplayFeedback).Stop();
  352.                     this.AddElement(pGeometry);
  353.                     this.pDisplayFeedback = null;
  354.                 }
  355.                 else if (this.btnNewEnvelope.Checked)//画矩形
  356.                 {
  357.                     IGeometry pGeometry = ((INewEnvelopeFeedback)this.pDisplayFeedback).Stop();
  358.                     this.AddElement(pGeometry);
  359.                     this.pDisplayFeedback = null;
  360.                 }
  361.                 else if (this.btnPolygonMovePoint.Checked)//移动多边形节点
  362.                 {
  363.                     IGeometry pGeometry = ((IPolygonMovePointFeedback)this.pDisplayFeedback).Stop();
  364.                     if (pGeometry != null)
  365.                     {
  366.                         this.hitElement.Geometry = pGeometry;
  367.                         this.pGraphicContainer.UpdateElement(this.hitElement);
  368.                     }
  369.                     this.hitElement = null;
  370.                     this.pDisplayFeedback = null;
  371.                     this.axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
  372.                 }
  373.                 else if (this.btnMoveGeometry.Checked)//移动几何对象
  374.                 {
  375.                     if (this.pEnumElement != null)
  376.                     {
  377.                         this.pEnumElement.Reset();
  378.                         IElement pElement;
  379.                         ITransform2D pTransform2D;
  380.                         //需要用ITransform2D接口逐个逐个的移动element
  381.                         for (pElement = this.pEnumElement.Next(); pElement != null; pElement = this.pEnumElement.Next())
  382.                         {
  383.                             pTransform2D = pElement as ITransform2D;
  384.                             pTransform2D.Move(e.mapX - this.moveGeometryStartPoint.X, e.mapY - this.moveGeometryStartPoint.Y);
  385.                             this.pGraphicContainer.UpdateElement(pElement);
  386.                         }
  387.                         this.moveGeometryStartPoint = null;
  388.                         this.pEnumElement = null;
  389.                         this.pDisplayFeedback = null;
  390.                         this.axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
  391.                     }
  392.                 }
  393.                 else if (this.btnStretchLine.Checked)//拉伸多义线
  394.                 {
  395.                     if (this.hitElement != null)
  396.                     {
  397.                         IGeometry pGeometry = ((IStretchLineFeedback)this.pDisplayFeedback).Stop();
  398.                         this.hitElement.Geometry = pGeometry;
  399.                         this.pGraphicContainer.UpdateElement(this.hitElement);
  400.                         this.hitElement = null;
  401.                         this.pDisplayFeedback = null;
  402.                         this.axMapControl1.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);
  403.                     }
  404.                 }
  405.             }
  406.         }
  407.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  408.         {
  409.             ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
  410.         }
  411.         private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
  412.         {
  413.             //if (this.pDisplayFeedback != null)
  414.             //{
  415.             //    this.pDisplayFeedback.Refresh(this.pScreenDisplay.hWnd);
  416.             //}
  417.         }
  418.         private void toolStripButton1_Click(object sender, EventArgs e)
  419.         {
  420.             foreach (ToolStripItem i in this.toolStrip1.Items)
  421.             {
  422.                 ((ToolStripButton)i).Checked = false;
  423.             }
  424.             ((ToolStripButton)sender).Checked = true;
  425.             this.pDisplayFeedback = null;
  426.             NewPolyline pNewPolyline = new NewPolyline();
  427.             pNewPolyline.OnCreate(this.axMapControl1.Object);
  428.             this.axMapControl1.CurrentTool = ((ITool)pNewPolyline);
  429.         }
  430.        
  431.     }
  432. }