1

I zoom into a map, then do a screenDisp.DrawPolyline(polyline); Now the line is in the correct place, but the map that is the background of the polyline object is actually the initial map and not the zoomed in map.

Here's a screenshot: http://i11.photobucket.com/albums/a173/iteration_x/LineBehavior.jpg

What you are seeing is a really zoomed in map of minnesota with most of the layers turned off, and that interior picture is part of the West Coast from the initial map load.

2 Answers 2

2

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);
    }
}
6
  • @Kirk Kuykendall Thanks again for the code. I can't seem to hit a breakpoint in the the CustLayer Draw function. Do I need to do some additional wiring?
    – patrick
    Commented Sep 1, 2010 at 14:21
  • 1
    Do you see the layer in your TOC? Is it checked? Commented Sep 1, 2010 at 14:47
  • @Kirk Kuykendall layer is in TOC and its is checked, but breakpoint in Draw is still not hit...
    – patrick
    Commented Sep 1, 2010 at 14:49
  • 1
    Have you tried panning/zooming? This should cause ILayer.Draw to be called. In visual studio have you set Debug>Exceptions to break when an exception is thrown? Maybe there's an exception firing before your layer has a chance to draw. Commented Sep 1, 2010 at 15:29
  • @Kirk Kuykendall I tried panning and zooming but it didn't hit the breakpont, no exceptions are thrown my code is set to break.
    – patrick
    Commented Sep 1, 2010 at 16:16
0

You could also try calling IActiveView:PartialRefresh to refesh the graphics container or the layer. To test whether refresh has anything to do with your problem call IActiveView:Refresh which is a full refresh. If it does use partial refresh instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.