[GIS] .net arcobjects draw polygon(rectangle) can not start at the preferred location

arcobjectsmap-drawingnetpolygon

I am using the sample code (draw a polygon on the screen) provided by Esri but it seems I cannot start the drawing from a location I want, every time it starts at wherever the function(or tool) located on map.

Anybody know how to draw a polygon as desired? Any example there?

Best Answer

Here's something I put together from the link you posted and another link that you will see commented out in the code. When you click on the map it will draw a square 10x10 map units from where you clicked. I work in metres so this draws a reasonably sized graphic.

Create an add-in with a tool and add this to the class.

protected override void OnMouseDown(MouseEventArgs arg)
    {

        IMap _map = ArcMap.Document.FocusMap;
        IScreenDisplay3 _display = (IScreenDisplay3)ArcMap.Document.ActiveView.ScreenDisplay;
        ESRI.ArcGIS.Geometry.IPoint _clickedPoint = _display.DisplayTransformation.ToMapPoint(arg.Location.X, arg.Location.Y);

        ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
        rgbColor.Red = 255;

        ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit cast.
        ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new
            ESRI.ArcGIS.Display.SimpleFillSymbolClass();
        simpleFillSymbol.Color = color;

        ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as
            ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.


        ////http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000002wm000000

        ESRI.ArcGIS.Geometry.IPointCollection4 _poly = (ESRI.ArcGIS.Geometry.IPointCollection4)(new ESRI.ArcGIS.Geometry.Polygon());
        _poly.AddPoint(_clickedPoint, Type.Missing, Type.Missing);

        ESRI.ArcGIS.Geometry.IPoint _pnt = new ESRI.ArcGIS.Geometry.Point();
        _pnt.PutCoords(_clickedPoint.X + 10, _clickedPoint.Y);
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);
        _pnt.PutCoords(_clickedPoint.X + 10, _clickedPoint.Y + 10);
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);
        _pnt.PutCoords(_clickedPoint.X, _clickedPoint.Y + 10 );
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);

        _poly.AddPoint(_clickedPoint, Type.Missing, Type.Missing);            _display.StartDrawing(_display.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);
        _display.SetSymbol(symbol);
        _display.DrawPolygon(((ESRI.ArcGIS.Geometry.IPolygon)_poly));
        _display.FinishDrawing();



            base.OnMouseDown(arg);
    }
Related Question