I have some code that I'm using to selecting features in a certain extent. I already rigged the tool properly, so it selected the objects as needed, but I want to draw interactively the selection box the user is making.

I'm using IScreenDisplay.DrawRectangle method, but it pollutes the screen with many small rectangles. is There a way to clear up the old drawings in the screen?

This Draw method is being called from a mouse move event. Any ideas?

Here are two methods I'm using to do the dirty work:

    public override void OnMouseMove(int button, int shift, int x, int y)
        if (button != 1)

        if (!_IsSelectOperation)

        endPoint = focusScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
        envelope.PutCoords(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
        DrawEnvelope(envelope, BuildSelectionSymbol(), focusScreenDisplay);

    private void DrawEnvelope(IEnvelope envelope, ISymbol symbol, IScreenDisplay screenDisplay)
        screenDisplay.StartDrawing(screenDisplay.WindowDC, 0);

Here is the code for BuildSelectionSymbol

    private ISymbol BuildSelectionSymbol()
        IColor fillColor = new RgbColorClass();
        fillColor.Transparency = 0;

        IRgbColor outlineColor = new RgbColorClass();
        outlineColor.Red = 0;
        outlineColor.Green = 0;
        outlineColor.Blue = 0;

        ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
        lineSymbol.Color = outlineColor as IColor;
        lineSymbol.Width = 1;
        lineSymbol.Style = esriSimpleLineStyle.esriSLSDash;

        ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
        fillSymbol.Color = fillColor;
        fillSymbol.Outline = lineSymbol;
        fillSymbol.Style = esriSimpleFillStyle.esriSFSHollow;

        ISymbol symbol = fillSymbol as ISymbol;
        symbol.ROP2 = esriRasterOpCode.esriROPNOP;

        return symbol;

It stills draws all the rectangles, but I still need a way to clean them.

For the linesymbol, fillsymbol and symbol, set ROP2 = esriRasterOpCode.esriROPNotXOrPen.


I created a MeaureAreaTool using this sample Sample: Custom subtyped command and tool I don't get any problems with the redraw of the Polygon. Mayby you can get any ideas from my code.

Private m_polygonFeedback As INewPolygonFeedback
Private m_screenDisplay As IScreenDisplay

Private m_hookHelper As IHookHelper

Public Overrides Sub OnCreate(ByVal hook As Object)
If (m_hookHelper Is Nothing) Then m_hookHelper = New HookHelperClass

If Not hook Is Nothing Then
m_hookHelper.Hook = hook
End If
End Sub

Public Overrides Sub OnClick()
m_screenDisplay = m_hookHelper.ActiveView.ScreenDisplay
End Sub

Public Overrides Sub OnDblClick()
Dim polygon As IPolygon = m_polygonFeedback.Stop()
MsgBox("Area: " & CType(polygon, IArea).Area)
m_polygonFeedback = Nothing
End Sub

Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
If Button = 1 Then
Dim cursorPoint As ESRI.ArcGIS.Geometry.IPoint = m_screenDisplay.DisplayTransformation.ToMapPoint(X, Y)

If m_polygonFeedback Is Nothing Then
m_polygonFeedback = New NewPolygonFeedbackClass()
m_polygonFeedback.Display = m_screenDisplay

End If

End If
End Sub

Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
If Not m_polygonFeedback Is Nothing Then
Dim cursorPoint As ESRI.ArcGIS.Geometry.IPoint = m_screenDisplay.DisplayTransformation.ToMapPoint(X, Y)
End If
End Sub

I already have the answer for this. I was using a normal envelope. The deal is that ESRI already has something to handle that for you and it's contained in INewEnvelopeFeedback class.


Actually, this interface got me where the code snippet in the question was. It stills draws all the rectangles, but I still need a way to clean them.

Any ideas?

 public override void OnMouseDown(int Button, int Shift, int X, int Y)
          if (Button == 2)
                **m_activeview.ScreenDisplay.Invalidate(null, true, m_activeview.get_ScreenCacheID(esriViewDrawPhase.esriViewGraphics, null));**

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Please provide more info to explain your answer rather than just some code. – Midavalo May 11 '16 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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