图形元素:不在数据库中保存的地理实体,一个临时的元素
相关知识:http://blog.csdn.net/summer_dew/article/details/79187995
思路:
以添加点元素到地图控件中为例:
-
创建点符号
ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass(); //创建类 pSimpleMarkerSymbol.Color = GetRGB(0, 255, 0); //自定义得到颜色函数 pSimpleMarkerSymbol.Size = 5; //设置大小 pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond; //设置样式
-
创建点的位置
//点位置 IPoint pPoint = new PointClass(); pPoint.PutCoords(e.mapX, e.mapY);
-
创建点元素
//点 IMarkerElement pMarkerElement = new MarkerElementClass(); pMarkerElement.Symbol = pSimpleMarkerSymbol; //赋值点元素的符号 IElement pElement = pMarkerElement as IElement; pElement.Geometry = pPoint as IGeometry; //赋值点位置
-
加入容器
IMap pMap = axMapControl1.Map; IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer; pGraphicsContainer.AddElement(pElement, 0);
-
刷新
IActiveView pActiveView = pMap as IActiveView; pActiveView.Refresh();
代码:
//添加点、线、面元素的点击事件 标记Flag变量
private int Flag = -1;
private void drawMarkerElementToolStripMenuItem_Click(object sender, EventArgs e)
{
Flag = 11; //点
}
private void drawLineElementToolStripMenuItem_Click(object sender, EventArgs e)
{
Flag = 12; //线
}
private void drawPolygonElementToolStripMenuItem_Click(object sender, EventArgs e)
{
Flag = 13; //面
}
//MapControl点击事件
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1) {
switch (Flag) {
case 11: //点
{
//点符号样式
ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass();
pSimpleMarkerSymbol.Color = GetRGB(0, 255, 0);
pSimpleMarkerSymbol.Size = 5;
pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
//点位置
IPoint pPoint = new PointClass();
pPoint.PutCoords(e.mapX, e.mapY);
//点
IMarkerElement pMarkerElement = new MarkerElementClass();
pMarkerElement.Symbol = pSimpleMarkerSymbol;
IElement pElement = pMarkerElement as IElement;
pElement.Geometry = pPoint as IGeometry;
//加入地图
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView;
IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.Refresh();
break;
}
case 12: //线
{
//线符号
ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass();
pSimpleLineSymbol.Color = GetRGB(0, 255, 0);
pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDash;
//线
ILineElement pLineElement = new LineElementClass();
pLineElement.Symbol = pSimpleLineSymbol;
//位置
IElement pElement = pLineElement as IElement;
pElement.Geometry = axMapControl1.TrackLine();
//加入地图并刷新
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView;
IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.Refresh();
break;
}
case 13: //面
{
IMap pMap = axMapControl1.Map; //获取Map对象
IActiveView pActiveView = pMap as IActiveView; //获取与Map相关联的活动视图
IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer; //获取与Map相关联的图形容器
IFillShapeElement pPolygonElement = new PolygonElementClass(); //创建新的图形元素,并设置符号
//设置符号
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
pSimpleFillSymbol.Color = GetRGB(0, 255, 0);
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
pPolygonElement.Symbol = pSimpleFillSymbol;
IElement pElement = pPolygonElement as IElement;
pElement.Geometry = axMapControl1.TrackPolygon();
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.Refresh();
break;
}
}
}
else if (e.button == 2)
{
contextMenuStrip3.Show(axTOCControl1, e.x, e.y);
}
}