I've seen this sort of behavior before. It was the result of calling IDisplay.StartDrawing, without calling FinishDrawing. How/when are you drawing to the screen? The more common ways to draw your own geometries on the screen are:
- In response to
IActiveViewEvents.AfterDraw
- Adding IElements to the
IGraphicsContainer (implemented by
Map class)
- Adding a custom layer to the map.
Below is code showing how to add a custom layer to the map.
public class CustLayer: BaseCustomLayer
{
private IPolyline m_Polyline = null;
private ISymbol m_Symbol = null;
public CustLayer()
{
m_Symbol = new SimpleLineSymbolClass();
}
public override void Draw(esriDrawPhase drawPhase, IDisplay Display, ITrackCancel trackCancel)
{
if (drawPhase != esriDrawPhase.esriDPGeography)
return;
if (m_Polyline == null)
{
IProjectedCoordinateSystem pcs = Display.DisplayTransformation.SpatialReference as IProjectedCoordinateSystem;
if (pcs != null)
{
m_Polyline = MakePolyline(pcs.GeographicCoordinateSystem);
m_Polyline.Project(pcs);
}
else
{
m_Polyline = MakePolyline(new UnknownCoordinateSystemClass());
}
m_extent = m_Polyline.Envelope; // sets IGeoDataset.Extent
}
// startdrawing has already been called, so don't call it again here
Display.SetSymbol(m_Symbol);
Display.DrawPolyline((IGeometry)m_Polyline);
}
public static IPolyline MakePolyline(ISpatialReference sr)
{
IPointCollection pc = new PolylineClass();
// deal with legacy COM cruft ...
object missing = Type.Missing; // (not needed with VB.NET)
pc.AddPoint(MakePoint(-98.0, 29.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-97.0, 28.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-96.0, 27.0, sr), ref missing, ref missing);
pc.AddPoint(MakePoint(-95.0, 26.0, sr), ref missing, ref missing);
IPolyline polyline = (IPolyline)pc;
polyline.SpatialReference = sr;
return polyline;
}
public static IPoint MakePoint(double x, double y, ISpatialReference sr)
{
IPoint p = new PointClass();
p.PutCoords(x, y);
p.SpatialReference = sr;
return p;
}
}
Here's the code that instantiates the layer and adds it to a mapcontrol:
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
CustLayer layer = new CustLayer();
layer.Name = "my custom layer";
axMapControl1.AddLayer(layer);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message);
}
}