public bool DeleteElement(IActiveView pActiveView, IElement pElement) { bool flag; try { if (pActiveView != null) { if (pElement == null) { return(false); } if (pActiveView is IGraphicsContainer) { if (pElement is IElementShutdown) { (pElement as IElementShutdown).Shutdown(); } IGraphicsContainer container = pActiveView as IGraphicsContainer; try { container.DeleteElement(pElement); } catch (Exception) { } goto Label_004A; } } return(false); Label_004A: flag = true; } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.ElementFun", "DeleteElement", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); flag = false; } return(flag); }
/// <summary> /// 删除指定名称的 Element 张琪 20110628 /// </summary> /// <param name="pScene"></param> /// <param name="sName"></param> public static void DeleteAllElementsWithName(IMap pMap, string sName) { try { IElement pElement; IGraphicsContainer pGCon = pMap.BasicGraphicsLayer as IGraphicsContainer; pGCon.Reset(); pElement = pGCon.Next() as IElement; while (pElement != null) { IElementProperties pElemProps = pElement as IElementProperties; if (pElemProps.Name == sName) { pGCon.DeleteElement(pElement); pGCon.Reset(); } pElement = pGCon.Next() as IElement; } } catch { } }
private void btnQuery_Click(object sender, EventArgs e) { QueryGeometry = AxMapCtrlHis.Extent; string sSQL = createSql(); //RefreshQueryGrid(sSQL); pGra = AxMapCtrlHis.Map as IGraphicsContainer; if (pEleEnv != null) { try { pGra.DeleteElement(pEleEnv); } catch { } } pEleEnv = null; //DataGridViewRow row = dataGridViewX1.Rows[e.RowIndex]; //if (row == null) return; //if (row.Tag == null) return; //string sPrjID = row.Tag.ToString(); //m_sCurPrjName = row.Cells["ColPrjName"].Value.ToString(); string sDataUpDate = this.dTimePost.Value.ToString("yyyy-MM-dd HH:mm:ss"); setMapLyrsDefinitionOfHPeriod(sDataUpDate); //ModHistory.ZoomToCaseArea(this.PrjEnvelope, AxMapCtrlHis.Map, sPrjID); //pEleEnv = new LineElementClass(); //IGeometry pGeo = getEnvGeometry(sPrjID); //ITopologicalOperator pTO = pGeo as ITopologicalOperator; //IGeometry pOutLine = pTO.Boundary; //if (pGeo == null) // return; //pEleEnv.Geometry = pOutLine; //(pEleEnv as ILineElement).Symbol = getSymbol(); //pGra.AddElement(pEleEnv, 0); //AxMapCtrlHis.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
private void btnOK_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtFileRoute.Text)) { lblInfo.Visible = true; } this.Refresh(); Application.DoEvents(); Deal(); this.Refresh(); txtFileRoute.Text = ""; if (m_pElement != null) { IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)m_pMap; IActiveView pActiveView = (IActiveView)m_pMap; pGraphicsContainer.DeleteElement(m_pElement); pActiveView.Refresh(); m_pElement = null; } }
/// <summary> /// Method used to remove graphics from the graphics container /// Elements are tagged with a GUID on the IElementProperties.Name property /// </summary> /// <param name="gc">map graphics container</param> /// <param name="list">list of GUIDs to remove</param> internal void RemoveGraphics(IGraphicsContainer gc, List <AMGraphic> list) { if (gc == null || !list.Any()) { return; } var elementList = new List <IElement>(); gc.Reset(); var element = gc.Next(); while (element != null) { var eleProps = element as IElementProperties; if (list.Any(g => g.UniqueId == eleProps.Name)) { elementList.Add(element); } element = gc.Next(); } foreach (var ele in elementList) { gc.DeleteElement(ele); } // remove from master graphics list foreach (var graphic in list) { if (GraphicsList.Contains(graphic)) { GraphicsList.Remove(graphic); } } elementList.Clear(); RaisePropertyChanged(() => HasMapGraphics); }
private void clearGraphics() { IGraphicsContainer graphicsContainer = (IGraphicsContainer)map; foreach (IElement grp in graphics) { if (grp == null) { break; } //grp.Locked = false; try { graphicsContainer.DeleteElement(grp); } catch (Exception) { Console.Write("Element was already deleted"); } } graphics.Clear(); }
private void syn(AxPageLayoutControl mainlayoutControl) { IObjectCopy objectcopy = new ObjectCopyClass(); object tocopymap = mainlayoutControl.ActiveView.GraphicsContainer; //获取mapcontrol中的map 这个是原始的 object copiedmap = objectcopy.Copy(tocopymap); //复制一份map,是一个副本 object tooverwritemap = PrintPagelayoutControl.ActiveView.GraphicsContainer; //IActiveView.FocusMap : The map that tools and controls act on. 控件和工具作用的地图,大概是当前地图吧!!! objectcopy.Overwrite(copiedmap, ref tooverwritemap); System.Runtime.InteropServices.Marshal.ReleaseComObject(objectcopy); IGraphicsContainer mainGraphCon = tooverwritemap as IGraphicsContainer; mainGraphCon.Reset(); IElement pElement = mainGraphCon.Next(); IArray pArray = new ArrayClass(); while (pElement != null) { pArray.Add(pElement); pElement = mainGraphCon.Next(); } int pElementCount = pArray.Count; IPageLayout PrintPageLayout = PrintPagelayoutControl.PageLayout; IGraphicsContainer PrintGraphCon = PrintPageLayout as IGraphicsContainer; PrintGraphCon.Reset(); IElement pPrintElement = PrintGraphCon.Next(); while (pPrintElement != null) { PrintGraphCon.DeleteElement(pPrintElement); pPrintElement = PrintGraphCon.Next(); } for (int i = 0; i < pArray.Count; i++) { PrintGraphCon.AddElement(pArray.get_Element(pElementCount - 1 - i) as IElement, 0); } PrintPagelayoutControl.Refresh(); }
/// <summary> /// 移除图元 /// </summary> /// <param name="element">要移除的图元对象</param> /// <param name="layer">图元所在的图层</param> /// <returns></returns> public bool RemoveElement(IMFElement element, ILayer layer) { if (element == null) { return(true); } IGraphicsContainer graphicsLayer = layer as IGraphicsContainer; if (graphicsLayer == null) { return(true); } Polygon_ArcGlobe polygonElement = element as Polygon_ArcGlobe; if (polygonElement.Rasterize) { polygonElement.Rasterize = false; } graphicsLayer.DeleteElement(polygonElement); return(true); }
private void CheckClosing(object sender, FormClosingEventArgs e) //窗体关闭事件 { //检查属性值是否有错误 typeValue = (sender as CheckForm).comboBox1.Text; bool OverlapsOrContains = IsOverlaps(pGeometry, typeValue.Trim()); if (OverlapsOrContains) { DialogResult dr = MessageBox.Show("是否保留绘制的图形?\n选择 “确定”,则保留绘制图形并为 Type 赋值为 “空”,\n选择 “取消”,则删除绘制图形!", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.Cancel) { pFeature.Delete(); IGraphicsContainer pGC = axMapControl1.Map as IGraphicsContainer; pGC.DeleteElement(pElement); axMapControl1.ActiveView.Refresh(); } else if (dr == DialogResult.OK) { pFeature.set_Value(pFeature.Fields.FindField("Type"), "空"); } } }
//删除布局视图上的地图元素 public void deleteElement(string eleName) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add("ScaleBar", "比例尺"); dict.Add("Legend", "图例"); dict.Add("NorthArrow", "指北针"); IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView; IMap pMap = pActiveView.FocusMap as IMap; IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; IElement pDeletElement = axPageLayoutControl1.FindElementByName(eleName);//获取PageLayout中的图例元素 if (pDeletElement != null) { pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在指北针,删除已经存在的指北针 } else { MessageBox.Show("当前不存在" + dict[eleName]); return; } }
private void 添加指北针ToolStripMenuItem_Click(object sender, EventArgs e) { IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView; IMap pMap = pActiveView.FocusMap as IMap; IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; IMapSurround pMapSurround; INorthArrow pNorthArrow; pNorthArrow = new MarkerNorthArrowClass(); pMapSurround = pNorthArrow; pMapSurround.Name = "NorthArrow"; //定义UID UID uid = new UIDClass(); uid.Value = "esriCarto.MarkerNorthArrow"; //定义MapSurroundFrame对象 IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uid, null); pMapSurroundFrame.MapSurround = pMapSurround; IElement pDeletElement = axPageLayoutControl1.FindElementByName("NorthArrow");//获取PageLayout中的图例元素 if (pDeletElement != null) { pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在指北针,删除已经存在的指北针 } //定义Envelope设置Element摆放的位置 IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.PutCoords(16, 24, 21, 32); IElement pElement = pMapSurroundFrame as IElement; pElement.Geometry = pEnvelope; pGraphicsContainer.AddElement(pElement, 0); //刷新axPageLayoutControl1的内容 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
public void Undo() { this.ienumElement_0.Reset(); IElement element = this.ienumElement_0.Next(); IGraphicsContainer graphicsContainer = this.iactiveView_0.GraphicsContainer; IGraphicsContainerSelect graphicsContainerSelect = graphicsContainer as IGraphicsContainerSelect; graphicsContainerSelect.UnselectAllElements(); while (element != null) { if (element is IGroupElement) { IEnumElement elements = (element as IGroupElement).Elements; elements.Reset(); for (IElement i = elements.Next(); i != null; i = elements.Next()) { graphicsContainer.DeleteElement(i); } graphicsContainer.AddElement(element, -1); graphicsContainerSelect.SelectElement(element); } element = this.ienumElement_0.Next(); } }
private bool UseTemplateMxtToPageLayout(AxPageLayoutControl mainPageLayoutControl, string sTemplatePath) { bool success = false; try { IMap map = null; IActiveView activeView = null; IPageLayout curPageLayout = mainPageLayoutControl.PageLayout; activeView = curPageLayout as IActiveView; map = activeView.FocusMap; // 读取模板 IMapDocument tempMapDocument = new MapDocumentClass(); tempMapDocument.Open(sTemplatePath, ""); IPageLayout tempPageLayout = tempMapDocument.PageLayout; IPage tempPage = tempPageLayout.Page; IPage curPage = curPageLayout.Page; // 替换单位及地图方向 curPage.Orientation = tempPage.Orientation; curPage.Units = tempPage.Units; // 替换页面尺寸 Double width, height; tempPage.QuerySize(out width, out height); curPage.PutCustomSize(width, height); // 删除当前layout中除了mapframe外所有element IGraphicsContainer graphicsContainer = curPageLayout as IGraphicsContainer; graphicsContainer.Reset(); IElement element = graphicsContainer.Next(); IMapFrame mapFrame = null; mapFrame = graphicsContainer.FindFrame(map) as IMapFrame; while (element != null) { if (element is IMapFrame) { mapFrame = element as IMapFrame; } else { graphicsContainer.DeleteElement(element); graphicsContainer.Reset(); } element = graphicsContainer.Next(); } // 遍历模板中的PageLayout所有元素,替换当前PageLayout的所有元素 IGraphicsContainer tempGraphicsContainer = tempPageLayout as IGraphicsContainer; tempGraphicsContainer.Reset(); IArray array = new ArrayClass(); while (element != null) { if (element is IMapFrame) { IElement mapFrameElement = mapFrame as IElement; mapFrameElement.Geometry = element.Geometry; } else { if (element is IMapSurroundFrame) { IMapSurroundFrame mapSurroundFrame = element as IMapSurroundFrame; mapSurroundFrame.MapFrame = mapFrame; IMapSurround tempMapSurround = mapSurroundFrame.MapSurround; } array.Add(element); } element = tempGraphicsContainer.Next(); } int elementCount = array.Count; for (int i = 0; i < array.Count; i++) { graphicsContainer.AddElement(array.get_Element(elementCount - 1 - i) as IElement, 0); } activeView.Refresh(); success = true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(success); }
/// <summary> /// 获得自定义多边形 /// </summary> /// <param name="Button"></param> /// <param name="Shift"></param> /// <param name="X"></param> /// <param name="Y"></param> public override void OnMouseDown(int Button, int Shift, int X, int Y) { IMapControl4 mapControl = null; if (_hookHelper != null) { mapControl = (IMapControl4)_hookHelper.Hook; _polygon = (IPolygon)mapControl.TrackPolygon(); } if (_polygon != null) { if (_polygon.IsEmpty == false) { IMap map = _hookHelper.FocusMap; IGraphicsContainer graphicsContainer = (IGraphicsContainer)map; IActiveView activeView = (IActiveView)map; if (_element != null) { graphicsContainer.DeleteElement(_element); activeView.Refresh(); } IRgbColor color = new RgbColor(); color.Red = 255; color.Green = 255; color.Blue = 0; IMarkerLineSymbol markerLineSymbol = new MarkerLineSymbol(); markerLineSymbol.Color = color; markerLineSymbol.Width = 2; ISimpleFillSymbol simpleFillSym = new SimpleFillSymbol(); simpleFillSym.Style = esriSimpleFillStyle.esriSFSHollow; simpleFillSym.Outline = markerLineSymbol; IFillShapeElement polygonElement = new PolygonElementClass(); polygonElement.Symbol = simpleFillSym; _element = (IElement)polygonElement; _element.Geometry = _polygon; graphicsContainer.AddElement(_element, 0); activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); //传值 if (_frmPolygonExtract.IsDisposed) { _frmPolygonExtract = new FrmPolygonExtract(); } _frmPolygonExtract.Map = _hookHelper.FocusMap; _frmPolygonExtract.MapControl = (IMapControl4)_hookHelper.Hook; _frmPolygonExtract.Geometry = _polygon; _frmPolygonExtract.Element = _element; _frmPolygonExtract.ShowDialog(); } else { MessageBoxEx.Show("画出的几何图象是空的!", "提示"); } } else { return; } mapControl.CurrentTool = null; _element = null; }
/// <summary> /// Method used to remove graphics from the graphics container /// Elements are tagged with a GUID on the IElementProperties.Name property /// </summary> /// <param name="gc">map graphics container</param> /// <param name="list">list of GUIDs to remove</param> internal void RemoveGraphics(IGraphicsContainer gc, List<string> list) { if (gc == null || !list.Any()) return; var elementList = new List<IElement>(); gc.Reset(); var element = gc.Next(); while (element != null) { var eleProps = element as IElementProperties; if (list.Contains(eleProps.Name)) { elementList.Add(element); } element = gc.Next(); } foreach (var ele in elementList) { gc.DeleteElement(ele); } list.Clear(); elementList.Clear(); RaisePropertyChanged(() => HasMapGraphics); }
// 添加图例 private void AddLegend() { IActiveView pActiveView = axPageLayoutControl_main.ActiveView; IGraphicsContainer pGC = axPageLayoutControl_main.PageLayout as IGraphicsContainer; UID pUID = new UIDClass() { Value = "esriCarto.Legend" }; IMapFrame pMapFrame = pGC.FindFrame(pActiveView.FocusMap) as IMapFrame; // 根据唯一标识符,创建与之对应的MapSurroundFrame IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null); // 检查PageLayout中是否已有图例(有则删除) IElement pDelElement = axPageLayoutControl_main.FindElementByName("Legend"); if (pDelElement != null) { pGC.DeleteElement(pDelElement); } // 设置MapSurroundFrame背景 ISymbolBackground pSymbolBackground = new SymbolBackgroundClass() { FillSymbol = new SimpleFillSymbolClass() { Color = GetRgbColor(240, 240, 240), Outline = new SimpleLineSymbolClass() { Color = GetRgbColor(0, 0, 0), Width = 1 } } }; pMapSurroundFrame.Background = pSymbolBackground; // 添加图例 IElement pElement = pMapSurroundFrame as IElement; IEnvelope pEnv = axPageLayoutControl_main.Extent; pEnv.XMin += 2; pEnv.YMin += 3; pElement.Geometry = pEnv; IMapSurround pMapSurround = pMapSurroundFrame.MapSurround; ILegend pLegend = pMapSurround as ILegend; pLegend.ClearItems(); pLegend.Title = "图例"; for (int i = 0; i < pActiveView.FocusMap.LayerCount; i++) { ILegendItem pLegendItem = new HorizontalLegendItemClass() { Layer = pActiveView.FocusMap.get_Layer(i), ShowDescriptions = false, Columns = 1, ShowHeading = true, ShowLabels = true }; pLegend.AddItem(pLegendItem); } pGC.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
private bool zoomToQuery(string query, int count, Double zoom) { List <datacontract.locationResult> loc = adresLocation.getAdresLocation(query, count); if (loc.Count > 0) { IPoint leftXY = new ESRI.ArcGIS.Geometry.Point() { X = loc[0].BoundingBox.LowerLeft.Lon_WGS84, Y = loc[0].BoundingBox.LowerLeft.Lat_WGS84, SpatialReference = wgs }; IPoint toleftXY = geopuntHelper.Transform(leftXY as IGeometry, map.SpatialReference) as IPoint; IPoint rightXY = new ESRI.ArcGIS.Geometry.Point() { X = loc[0].BoundingBox.UpperRight.Lon_WGS84, Y = loc[0].BoundingBox.UpperRight.Lat_WGS84, SpatialReference = wgs }; IPoint torightXY = geopuntHelper.Transform(rightXY as IGeometry, map.SpatialReference) as IPoint; IEnvelope bbox = geopuntHelper.makeExtend(toleftXY.X, toleftXY.Y, torightXY.X, torightXY.Y, map.SpatialReference); IPoint XY = new ESRI.ArcGIS.Geometry.Point() { X = loc[0].Location.Lon_WGS84, Y = loc[0].Location.Lat_WGS84, SpatialReference = wgs }; IPoint toXY = geopuntHelper.Transform(XY as IGeometry, map.SpatialReference) as IPoint; //create a graphic IRgbColor rgb = new RgbColorClass() { Red = 255, Blue = 255, Green = 0 }; if (graphic != null) { IGraphicsContainer grpCont = (IGraphicsContainer)map; grpCont.DeleteElement(graphic); graphic = null; } graphic = geopuntHelper.AddGraphicToMap(map, toXY, rgb, new RgbColorClass() { Red = 0, Blue = 0, Green = 0 }, 8, true); infoLabel.Text = geopuntHelper.adresTypeStringTranslate(loc[0].LocationType); map.MapScale = 1000; view.Extent = bbox; geopuntHelper.ZoomByRatioAndRecenter(view, 1, toXY.X, toXY.Y); view.Refresh(); return(true); } else { return(false); } }
private void button_dress_Click(object sender, EventArgs e) { //获取axPageLayoutControl1的图形容器 IGraphicsContainer graphicsContainer = axPageLayoutControl1.GraphicsContainer; //获取axPageLayoutControl1空间里面显示的地图图层 IMapFrame mapFrame = (IMapFrame)graphicsContainer.FindFrame(axPageLayoutControl1.ActiveView.FocusMap); if (mapFrame == null) { return; } //--------------创建图例------------ UID uID = new UIDClass(); //创建UID作为该图例的唯一标识符,方便创建之后进行删除、移动等操作 uID.Value = "esriCarto.Legend"; IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null); if (mapSurroundFrame == null) { return; } if (mapSurroundFrame.MapSurround == null) { return; } mapSurroundFrame.MapSurround.Name = "图例"; IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(16, 2, 18, 7);//设置图例摆放位置(原点在axPageLayoutControl左下角) IElement element = (IElement)mapSurroundFrame; element.Geometry = envelope; //将图例转化为几何要素添加到axPageLayoutControl1,并刷新页面显示 axPageLayoutControl1.AddElement(element, Type.Missing, Type.Missing, "Legend", 0); axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); //-----------设置指北针-------- IMapSurround pMapSurround; INorthArrow pNorthArrow; pNorthArrow = new MarkerNorthArrowClass();//创建指北针的实例 pMapSurround = pNorthArrow; pMapSurround.Name = "NorthArrow"; //定义UID UID uid = new UIDClass(); uid.Value = "esriCarto.MarkerNorthArrow"; //定义MapSurroundFrame对象 IMapSurroundFrame pMapSurroundFrame = mapFrame.CreateSurroundFrame(uid, null); pMapSurroundFrame.MapSurround = pMapSurround; IElement pDeletElement = axPageLayoutControl1.FindElementByName("NorthArrow");//获取PageLayout中的图例元素 if (pDeletElement != null) { graphicsContainer.DeleteElement(pDeletElement); //如果已经存在指北针,删除已经存在的指北针 } //定义Envelope设置Element摆放的位置 IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.PutCoords(16, 24, 21, 32); IElement pElement = pMapSurroundFrame as IElement; pElement.Geometry = pEnvelope; graphicsContainer.AddElement(pElement, 0); //刷新axPageLayoutControl1的内容 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); //-----------设置比例尺------------ IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView; IMap pMap = pActiveView.FocusMap as IMap; IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; //IMapSurround pMapSurround; //设置比例尺样式 IScaleBar pScaleBar = new ScaleLineClass(); pScaleBar.Units = esriUnits.esriKilometers; pScaleBar.Divisions = 4; pScaleBar.Subdivisions = 3; pScaleBar.DivisionsBeforeZero = 0; pScaleBar.UnitLabel = "km"; pScaleBar.LabelPosition = esriVertPosEnum.esriBelow; pScaleBar.LabelGap = 3.6; pScaleBar.LabelFrequency = esriScaleBarFrequency.esriScaleBarDivisionsAndFirstMidpoint; pScaleBar.LabelPosition = esriVertPosEnum.esriBelow; ITextSymbol pTextsymbol = new TextSymbolClass(); pTextsymbol.Size = 1; stdole.StdFont pFont = new stdole.StdFont(); pFont.Size = 3; pFont.Name = "Arial"; pTextsymbol.Font = pFont as stdole.IFontDisp; pTextsymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; pScaleBar.UnitLabelSymbol = pTextsymbol; pScaleBar.LabelSymbol = pTextsymbol; INumericFormat pNumericFormat = new NumericFormatClass(); pNumericFormat.AlignmentWidth = 0; pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits; pNumericFormat.RoundingValue = 0; pNumericFormat.UseSeparator = true; pNumericFormat.ShowPlusSign = false; //定义UID pMapSurround = pScaleBar; pMapSurround.Name = "ScaleBar"; // UID uid = new UIDClass(); uid.Value = "esriCarto.ScaleLine"; //定义MapSurroundFrame对象IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uid, null); pMapSurroundFrame.MapSurround = pMapSurround; //定义Envelope设置Element摆放的位置 //IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.PutCoords(8, 2, 14, 4); //IElement pElement = pMapSurroundFrame as IElement; pElement.Geometry = pEnvelope; //IElement pDeletElement = axPageLayoutControl1.FindElementByName("ScaleBar"); //获取PageLayout中的比例尺元素 if (pDeletElement != null) { pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在比例尺,删除已经存在的比例尺 } pGraphicsContainer.AddElement(pElement, 0); //刷新axPageLayoutControl1的内容 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); //---------------添加标题------------- //IGraphicsContainer graphicsContainer = axPageStation.PageLayout as IGraphicsContainer; //IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(-14, 26, 35, 26); IRgbColor pColor = new RgbColorClass() { Red = 0, Blue = 0, Green = 0 }; pFont.Name = "宋体"; pFont.Bold = true; ITextSymbol pTextSymbol = new TextSymbolClass() { Color = pColor, //Font = pFont, Size = 30 }; ITextElement pTextElement = new TextElementClass() { Symbol = pTextSymbol, ScaleText = true, Text = "盗墓难度专题图" }; element = pTextElement as ESRI.ArcGIS.Carto.IElement; element.Geometry = envelope; graphicsContainer.AddElement(element, 0); axPageLayoutControl1.Refresh(); }
//点击地图框事件 private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnMouseDownEvent e) { if (e.button == 1) { IPageLayout pPageLayout = axPageLayoutControl1.PageLayout;; IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer; IActiveView pActiveView = pPageLayout as IActiveView; IMapFrame pMapFrame; IMapSurroundFrame pMapSurroundFrame; IElement pElement; switch (tag) { #region 添加标题 case 1: ITextElement pTextElement = new TextElementClass(); pTextElement.Text = title; pTextElement.Symbol = pTextSymbol; pElement = pTextElement as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pGraphicsContainer.AddElement(pElement, 0); axPageLayoutControl1.Refresh(); tag = 0; break; #endregion #region 添加图例 case 2: pLegend2 = new LegendClass_2(); pElement = axPageLayoutControl1.FindElementByName("legend"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } pElementTypeName = "lenend"; pUid = new UIDClass(); pUid.Value = "esriCato.Legend"; if (pGraphicsContainer == null) { return; } pMapSurround = pLegend2 as IMapSurround; pMapSurround.Map = pActiveView.FocusMap; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; if (pMapFrame == null) { return; } pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pMapSurround; pMapSurroundFrame.MapSurround.Name = pElementTypeName; pMapSurroundFrame.MapFrame = pMapFrame; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pElement.Activate(pActiveView.ScreenDisplay); pElement.Draw(pActiveView.ScreenDisplay, null); pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); //图例标题字体样式 pLegend2.Title = "图例"; pTitltFont.Name = "黑体"; pTitltFont.Bold = true; pTitltFont.Size = 16; pTitltTextSymbol.Color = ColorToIColor(Color.Black); pTitltTextSymbol.Font = pTitltFont; pLenendFormat.TitleSymbol = pTitltTextSymbol; //图例图层名字体样式 pLayerFont.Name = "仿宋_GB2312"; pLayerFont.Size = 13; pLayerTextSymbol.Font = pLayerFont; //图例标签字体样式 pLabelFont.Name = "仿宋_GB2312"; pLabelFont.Size = 13; pLabelTextSymbol.Font = pLayerFont; for (int i = 0; i < pLenendItemsString.Count; i++) { for (int j = 0; j < pLegend2.ItemCount; j++) { if (pLenendItemsString[i].ToString() == pLegend2.get_Item(j).Layer.Name) { pLegend2.RemoveItem(j); } } } pLegend2.AdjustColumns(pColumnCount); pLegend2.Refresh(); tag = 0; break; #endregion #region 添加指北针 case 3: if (pGraphicsContainer == null) { return; } pElement = axPageLayoutControl1.FindElementByName("northarrow"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } pElementTypeName = "northarrow"; pUid = new UIDClass(); pUid.Value = "esriCato.MarkerNorthArrow"; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pNorthArrowStyleGalleryItem.Item as IMapSurround; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); return; } pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); tag = 0; break; #endregion #region 添加比例尺 case 4: if (pGraphicsContainer == null) { return; } pElement = axPageLayoutControl1.FindElementByName("scalebar"); if (pElement != null) { pGraphicsContainer.DeleteElement(pElement); } IScaleBar pScaleBar; pScaleBar = pScaleStyleGalleryItem.Item as IScaleBar; pScaleBar.Units = esriUnits.esriKilometers; pScaleBar.UnitLabel = " KM"; pElementTypeName = "scalebar"; pUid = new UIDClass(); pUid.Value = "esriCato.AlternatingScaleBar"; pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame; pMapSurroundFrame = new MapSurroundFrameClass(); pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUid, null); pMapSurroundFrame.MapSurround = pScaleStyleGalleryItem.Item as IMapSurround; pElement = pMapSurroundFrame as IElement; try { pElement.Geometry = axPageLayoutControl1.TrackRectangle(); } catch { MessageBox.Show("请拉框选择范围!", "提示"); } pGraphicsContainer.AddElement(pElement, 0); pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); tag = 0; break; #endregion } } }
private void axMapControl1_OnDoubleClick(object sender, IMapControlEvents2_OnDoubleClickEvent e) { //当“添加polygon”被勾选时,结束polygon的绘制 if (miAddPolygon.Checked) { //结束反馈 IPolygon p_polygon = ((INewPolygonFeedback)m_DisplayFeedback).Stop(); //当多边形不为空 if (p_polygon != null) { //IGraphicsContainer转换接口,用于在当前地图存放多边形 m_graphicsContainer = (IGraphicsContainer)(axMapControl1.Map); IActiveView p_activeView = axMapControl1.Map as IActiveView; IScreenDisplay screenDisplay = p_activeView.ScreenDisplay; //设置多边形的符号 ISimpleFillSymbol p_simpleFillSymbol = new SimpleFillSymbolClass(); IRgbColor p_rgbColor = new RgbColorClass(); p_rgbColor.Red = 255; p_rgbColor.Green = 255; IColor p_color = p_rgbColor; p_color.Transparency = 0; p_simpleFillSymbol.Color = p_color; //新建element对象存放多边形 IPolygonElement p_polygonElement = new PolygonElementClass(); IElement p_element = (IElement)p_polygonElement; //将多边形以element的形式储存在graphisContainer中 if (p_element != null) { p_element.Geometry = p_polygon; IFillShapeElement pFillShapeElement = p_element as IFillShapeElement; pFillShapeElement.Symbol = p_simpleFillSymbol; m_graphicsContainer.AddElement(p_element, 0); } //更改标记符为零 m_marker = 0; //取消勾选“添加polygon” miAddPolygon.Checked = false; if (m_element_red != null) { m_graphicsContainer.DeleteElement(m_element_red); } if (m_element_green != null) { m_graphicsContainer.DeleteElement(m_element_green); } m_polyline = new PolylineClass(); m_point = new PointClass(); m_element_red = null; m_element_green = null; p_activeView.Refresh(); } //如果多边形为空,初始化所有变量 else { if (m_element_red != null) { m_graphicsContainer.DeleteElement(m_element_red); } if (m_element_green != null) { m_graphicsContainer.DeleteElement(m_element_green); } m_hitElement = null; m_DisplayFeedback = null; m_marker = 0; m_polyline = new PolylineClass(); m_point = new PointClass(); m_element_red = null; m_element_green = null; axMapControl1.ActiveView.Refresh(); } } }
/// <summary> /// Method used to remove graphics from the graphics container /// Elements are tagged with a GUID on the IElementProperties.Name property /// Removes graphics from all tabs, not just the tab that is currently active /// </summary> private void RemoveGraphics(IGraphicsContainer gc, bool removeOnlyTemporary) { if (gc == null || !GraphicsList.Any()) return; // keep track of the graphics that we need to remove from the GraphicsList List<Graphic> removedGraphics = new List<Graphic>(); var elementList = new List<IElement>(); gc.Reset(); var element = gc.Next(); while (element != null) { var eleProps = element as IElementProperties; foreach (Graphic graphic in GraphicsList) { if (graphic.UniqueId.Equals(eleProps.Name)) { if (graphic.IsTemp == removeOnlyTemporary) { elementList.Add(element); removedGraphics.Add(graphic); } break; } } element = gc.Next(); } foreach (var ele in elementList) { gc.DeleteElement(ele); } // clean up the GraphicsList and remove the necessary graphics from it foreach (Graphic graphic in removedGraphics) { GraphicsList.Remove(graphic); } elementList.Clear(); RaisePropertyChanged(() => HasMapGraphics); }
private void 添加比例尺ToolStripMenuItem_Click(object sender, EventArgs e) { IActiveView pActiveView = axPageLayoutControl1.PageLayout as IActiveView; IMap pMap = pActiveView.FocusMap as IMap; IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer; IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame; IMapSurround pMapSurround; //设置比例尺样式 IScaleBar pScaleBar = new ScaleLineClass(); pScaleBar.Units = esriUnits.esriKilometers; pScaleBar.Divisions = 4; pScaleBar.Subdivisions = 3; pScaleBar.DivisionsBeforeZero = 0; pScaleBar.UnitLabel = "km"; pScaleBar.LabelPosition = esriVertPosEnum.esriBelow; pScaleBar.LabelGap = 3.6; pScaleBar.LabelFrequency = esriScaleBarFrequency.esriScaleBarDivisionsAndFirstMidpoint; pScaleBar.LabelPosition = esriVertPosEnum.esriBelow; ITextSymbol pTextsymbol = new TextSymbolClass(); pTextsymbol.Size = 1; stdole.StdFont pFont = new stdole.StdFont(); pFont.Size = 3; pFont.Name = "Arial"; pTextsymbol.Font = pFont as stdole.IFontDisp; pTextsymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; pScaleBar.UnitLabelSymbol = pTextsymbol; pScaleBar.LabelSymbol = pTextsymbol; INumericFormat pNumericFormat = new NumericFormatClass(); pNumericFormat.AlignmentWidth = 0; pNumericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfSignificantDigits; pNumericFormat.RoundingValue = 0; pNumericFormat.UseSeparator = true; pNumericFormat.ShowPlusSign = false; //定义UID pMapSurround = pScaleBar; pMapSurround.Name = "ScaleBar"; UID uid = new UIDClass(); uid.Value = "esriCarto.ScaleLine"; //定义MapSurroundFrame对象 IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uid, null); pMapSurroundFrame.MapSurround = pMapSurround; //定义Envelope设置Element摆放的位置 IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.PutCoords(8, 5, 14, 7); IElement pElement = pMapSurroundFrame as IElement; pElement.Geometry = pEnvelope; IElement pDeletElement = axPageLayoutControl1.FindElementByName("ScaleBar");//获取PageLayout中的比例尺元素 if (pDeletElement != null) { pGraphicsContainer.DeleteElement(pDeletElement); //如果已经存在比例尺,删除已经存在的比例尺 } pGraphicsContainer.AddElement(pElement, 0); //刷新axPageLayoutControl1的内容 axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); }
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) { if (e.button == 1) { //给m_graphicsContainer赋值 m_graphicsContainer = axMapControl1.Map as IGraphicsContainer; //在添加polygon被选中时 if (miAddPolygon.Checked) { //在鼠标点击的位置生成一个点 IPoint p_Point = new PointClass(); p_Point.PutCoords(e.mapX, e.mapY); IActiveView p_activeview = axMapControl1.Map as IActiveView; IScreenDisplay p_ScreenDisplay = p_activeview.ScreenDisplay; //标记符等于0时 if (m_marker == 0) { //对m_DisplayFeedback重新初始化 m_DisplayFeedback = new NewPolygonFeedbackClass(); //设置m_DisplayFeedback的display m_DisplayFeedback.Display = p_ScreenDisplay; //开始绘制polygon的第一个点 ((INewPolygonFeedback)m_DisplayFeedback).Start(p_Point); //将m_polyline转为pointcollection IPointCollection p_PointCollection = m_polyline as IPointCollection; //将第一个点加入pointcollection中 p_PointCollection.AddPoint(p_Point); //将第一个点记录到m_point中 m_point = p_Point; //对m_polyline重新赋值 m_polyline = p_PointCollection as IPolyline; if (m_marker == 0) { m_marker = 1; } //刷新 axMapControl1.ActiveView.Refresh(); } //如果不是第一次点击,就添加节点 else { //初始化红线和蓝线 if (m_element_red != null) { m_graphicsContainer.DeleteElement(m_element_red); } if (m_element_green != null) { m_graphicsContainer.DeleteElement(m_element_green); } //向m_DisplayFeedback中添加顶点 ((INewPolygonFeedback)m_DisplayFeedback).AddPoint(p_Point); IPointCollection p_PointCollection = m_polyline as IPointCollection; p_PointCollection.AddPoint(p_Point); //生成红线 m_polyline = p_PointCollection as IPolyline; ILineElement p_LineElement_red = new LineElementClass(); m_element_red = p_LineElement_red as IElement; m_element_red.Geometry = m_polyline; ISimpleLineSymbol p_SimpleLineSymbol = new SimpleLineSymbolClass(); IRgbColor p_rgbcolor = new RgbColorClass(); p_rgbcolor.Red = 255; p_rgbcolor.Green = 0; p_rgbcolor.Blue = 0; p_SimpleLineSymbol.Color = p_rgbcolor as IColor; p_LineElement_red.Symbol = p_SimpleLineSymbol as ILineSymbol; //设置蓝线symbol ISimpleLineSymbol p_SimpleLine_green = new SimpleLineSymbolClass(); IRgbColor p_rgbcolor_green = new RgbColorClass(); p_rgbcolor_green.Red = 0; p_rgbcolor_green.Green = 255; p_rgbcolor_green.Blue = 255; p_SimpleLine_green.Color = p_rgbcolor_green as IColor; //生成蓝线 IPolyline p_polyline = new PolylineClass(); IPointCollection p_pointColl_green = p_polyline as IPointCollection; //向蓝线中添加两个顶点 p_pointColl_green.AddPoint(m_point); p_pointColl_green.AddPoint(p_Point); p_polyline = p_pointColl_green as IPolyline; ILineElement p_LineElement_green = new LineElementClass(); m_element_green = p_LineElement_green as IElement; p_LineElement_green.Symbol = p_SimpleLine_green as ILineSymbol; m_element_green.Geometry = p_polyline; //将红线和蓝线加入containner显示 m_graphicsContainer.AddElement(m_element_red, 0); m_graphicsContainer.AddElement(m_element_green, 0); //更新视图 axMapControl1.ActiveView.Refresh(); } } //当选择polygon被选中时 if (miSelectPolygon.Checked) { //如果m_list不为空,则从容器中把其所对应element删除并将m_list清空 if (m_list.Count != 0) { for (int i = 0; i < m_list.Count; i++) { m_graphicsContainer.DeleteElement(m_list[i]); } m_list.Clear(); } //刷新 axMapControl1.ActiveView.Refresh(); IPoint p_point = new PointClass(); //记录点下的点 p_point.PutCoords(e.mapX, e.mapY); //得到选中的element集合 IEnumElement p_EnumElement = m_graphicsContainer.LocateElements(p_point, 5); IGeometry p_geometry; //选中的element集合不为空时 if (p_EnumElement != null) { m_hitElement = p_EnumElement.Next(); p_geometry = m_hitElement.Geometry; //循环找到类型为polygon的element while (p_geometry.GeometryType != esriGeometryType.esriGeometryPolygon && m_hitElement != null) { m_hitElement = p_EnumElement.Next(); if (m_hitElement != null) { p_geometry = m_hitElement.Geometry; } } if (p_geometry.GeometryType == esriGeometryType.esriGeometryPolygon) { m_polygon = (IPolygon)p_geometry; m_PointColl = m_polygon as IPointCollection; //循环将选中的polygon的每个点以element加入容器中 for (int k = 1; k < m_PointColl.PointCount; k++) { IMarkerElement p_MarkerElement = new MarkerElementClass(); ISimpleMarkerSymbol p_simpleMarker = new SimpleMarkerSymbolClass(); p_simpleMarker.Size = 5; IElement p_element = (IElement)p_MarkerElement; p_MarkerElement.Symbol = p_simpleMarker as IMarkerSymbol; if (!(p_element == null)) { p_element.Geometry = m_PointColl.get_Point(k); m_graphicsContainer.AddElement(p_element, 0); } m_list.Add(p_element); } axMapControl1.ActiveView.Refresh(); } } } //当编辑polygon被选中时 if (miEditPolygon.Checked) { if (m_polygon != null) { //在鼠标点击的位置生成一个点 IPoint p_Point = new PointClass(); p_Point.PutCoords(e.mapX, e.mapY); IActiveView p_ActiveView = axMapControl1.Map as IActiveView; IScreenDisplay p_ScreenDisplay = p_ActiveView.ScreenDisplay; //建立新的hittest,设置相关变量,获取点击处polygon的顶点 IHitTest p_HitTest = m_polygon as IHitTest; IPoint p_hitPoint = new PointClass(); double p_distance = 0; bool p_isOnRightSide = true; int p_hitPartIndex = 0; int p_hitSegmentIndex = 0; bool p_isHit = p_HitTest.HitTest(p_Point, this.axMapControl1.ActiveView.Extent.Width / 100, esriGeometryHitPartType.esriGeometryPartVertex, p_hitPoint, ref p_distance, ref p_hitPartIndex, ref p_hitSegmentIndex, ref p_isOnRightSide); //如果有点被选中,开始拖动该点 if (p_isHit) { m_DisplayFeedback = new PolygonMovePointFeedbackClass(); m_marker = 2; m_DisplayFeedback.Display = p_ScreenDisplay; ((IPolygonMovePointFeedback)m_DisplayFeedback).Start(m_polygon, p_hitSegmentIndex, p_Point); } } } } }
public void LocateCheckError(IGeometry location) { IGeometry area = location; if (location.GeometryType == esriGeometryType.esriGeometryPoint) { ITopologicalOperator topo = location as ITopologicalOperator; IGeometry buffer = topo.Buffer(10); area = buffer.Envelope; this.axMapControl2.Extent = buffer.Envelope; } else { this.axMapControl2.Extent = location.Envelope; } IGraphicsContainer gcon = this.axMapControl2.ActiveView.GraphicsContainer; if (m_ErrorLocation_ptn != null) { gcon.DeleteElement(m_ErrorLocation_ptn); } if (m_ErrorLocation_ln != null) { gcon.DeleteElement(m_ErrorLocation_ln); } if (m_ErrorLocation_poly != null) { gcon.DeleteElement(m_ErrorLocation_poly); } if (location.GeometryType == esriGeometryType.esriGeometryPoint) { IRgbColor color = new RgbColorClass(); color.Blue = 0; color.Green = 0; color.Red = 255; ISimpleMarkerSymbol smsym = new SimpleMarkerSymbolClass(); smsym.Color = color as IColor; smsym.Size = 10; m_ErrorLocation_ptn = new MarkerElementClass(); IMarkerElement pelem = m_ErrorLocation_ptn as IMarkerElement; pelem.Symbol = smsym as ISimpleMarkerSymbol; m_ErrorLocation_ptn.Geometry = location as IGeometry; gcon.AddElement(m_ErrorLocation_ptn, 0); } else if (location.GeometryType == esriGeometryType.esriGeometryPolyline) { IRgbColor color = new RgbColorClass(); color.Blue = 0; color.Green = 0; color.Red = 255; ISimpleLineSymbol slsym = new SimpleLineSymbolClass(); slsym.Color = color as IColor; slsym.Width = 4; m_ErrorLocation_ln = new LineElementClass(); ILineElement pelem = m_ErrorLocation_ln as ILineElement; pelem.Symbol = slsym as ISimpleLineSymbol; m_ErrorLocation_ln.Geometry = location as IGeometry; gcon.AddElement(m_ErrorLocation_ln, 0); } else if (location.GeometryType == esriGeometryType.esriGeometryPolygon) { IRgbColor color = new RgbColorClass(); color.Blue = 0; color.Green = 0; color.Red = 255; ISimpleLineSymbol slsym = new SimpleLineSymbolClass(); slsym.Color = color as IColor; ISimpleFillSymbol sfsym = new SimpleFillSymbolClass(); sfsym.Outline = slsym as ILineSymbol; sfsym.Style = esriSimpleFillStyle.esriSFSNull; m_ErrorLocation_poly = new PolygonElementClass(); IFillShapeElement pelem = m_ErrorLocation_poly as IFillShapeElement; pelem.Symbol = sfsym as IFillSymbol; m_ErrorLocation_poly.Geometry = location as IGeometry; gcon.AddElement(m_ErrorLocation_poly, 0); } }