I'm trying to draw a line on layout view on ArcMap with C# and ArcObjects. I'm working on this sample. But I have some problems.
1.) This code for draw line on data view but I need to draw line on layout view(In box frame).I want to see properties of line element when I selected my line Item.
2.) When I click on this line in ArcMap, It become invisible..why is this happening?
This is Code Sample :
protected override void OnClick()
{
//drawing a polyline
Point p1 = new PointClass();
p1.X = 10; p1.Y = 10;
IPoint p2 = new PointClass();
p2.X = 20; p2.Y = 20;
IPoint p3 = new PointClass();
p3.X = 35; p3.Y = 15;
IPoint p4 = new PointClass();
p4.X = 40; p4.Y = 17;
IPoint p5 = new PointClass();
p5.X = 50; p5.Y = 19;
IPoint p6 = new PointClass();
p6.X = 60; p6.Y = 18;
IPolyline polyline = new PolylineClass();
IPointCollection pointColl = polyline as IPointCollection;
pointColl.AddPoint(p1);
pointColl.AddPoint(p2);
pointColl.AddPoint(p3);
pointColl.AddPoint(p4);
pointColl.AddPoint(p5);
pointColl.AddPoint(p6);
IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
IActiveView activeView = mxdoc.ActiveView;
IScreenDisplay screenDisp = activeView.ScreenDisplay;
short screenCache = Convert.ToInt16(esriScreenCache.esriNoScreenCache);
screenDisp.StartDrawing(screenDisp.hDC, screenCache);
IRgbColor color = new RgbColorClass();
color.Red = 0; color.Blue = 0; color.Green = 0;
ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
simpleLineSymbol.Color = color;
simpleLineSymbol.Width = 1;
screenDisp.SetSymbol(simpleLineSymbol as ISymbol);
screenDisp.DrawPolyline(polyline);
screenDisp.FinishDrawing();
}
Best Answer
The reason why this code is failing is that you are writing to the screen cache, which you are obtaining from the
activeview
and this could be themap
orpagelayout
; you need to be more explicit.As soon as there is any sort of screen refresh you loose the graphic you have drawn. If you want graphics to persist you need to be storing them in the PageLayout graphics container. The following VBA code shows you how to do this using your example points.
Note: the coordinates are interpreted as page coordinates, not geographic so the line that gets created is much larger than your page.