public IElement AddGraphicToMap(IMap map, IGeometry geometry, IRgbColor rgbColor, IRgbColor outlineRgbColor)
{
IGraphicsContainer graphicsContainer = (IGraphicsContainer)map; // Explicit Cast
IElement element = null;
if ((geometry.GeometryType) == esriGeometryType.esriGeometryPoint)
{
// Marker symbols
ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
simpleMarkerSymbol.Color = rgbColor;
simpleMarkerSymbol.Outline = true;
simpleMarkerSymbol.OutlineColor = outlineRgbColor;
simpleMarkerSymbol.OutlineSize = 1.5;
simpleMarkerSymbol.Size = 10;
simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
IMarkerElement markerElement = new MarkerElementClass();
markerElement.Symbol = simpleMarkerSymbol;
element = (IElement)markerElement; // Explicit Cast
}
else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolyline)
{
// Line elements
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
simpleLineSymbol.Color = rgbColor;
simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDot; //.esriSLSSolid;
simpleLineSymbol.Width = 1;
ILineElement lineElement = new LineElementClass();
lineElement.Symbol = simpleLineSymbol;
element = (IElement)lineElement; // Explicit Cast
}
else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolygon)
{
// Polygon elements
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
simpleFillSymbol.Color = rgbColor;
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSForwardDiagonal;
IFillShapeElement fillShapeElement = new PolygonElementClass();
fillShapeElement.Symbol = simpleFillSymbol;
element = (IElement)fillShapeElement; // Explicit Cast
}
if (!(element == null))
{
element.Geometry = geometry;
graphicsContainer.AddElement(element, 0);
}
return element;
}
private void button_Click(object sender, EventArgs e)
{
//get running arcmap-instance
IApplication application = null;
IAppROT appRot = new AppROTClass();
for (int i = 0; i < appRot.Count; i++)
{
if (appRot.get_Item(i) is IMxApplication)
{
application = appRot.get_Item(i);
}
}
//get document handle
IMxDocument mxDocument = (IMxDocument)application.Document;
//create points
IPoint fromPoint = new PointClass();
IPoint toPoint = new PointClass();
fromPoint.X = 0;
fromPoint.Y = 0;
toPoint.X = 9999999;
toPoint.Y = 9999999;
//create line
IPolyline line = new PolylineClass();
line.FromPoint = fromPoint;
line.ToPoint = toPoint;
//define color
IRgbColor colorGreen = new RgbColorClass();
colorGreen.Green = 200;
//draw points and lines on the map
AddGraphicToMap(mxDocument.ActiveView.FocusMap, fromPoint, colorGreen, colorGreen);
AddGraphicToMap(mxDocument.ActiveView.FocusMap, line, colorGreen, colorGreen);
AddGraphicToMap(mxDocument.ActiveView.FocusMap, toPoint, colorGreen, colorGreen);
//refresh map to make changes visible
mxDocument.ActiveView.Refresh();
}