MapWinGIS:SampleCode-C Sharp Net:PointsOnMouseDown

This is a back-up of the WIKI.
Not all links might work
We're working on a new wiki.

Main Page | Recent changes | View source | Page history | Log in / create account |

Printable version | Disclaimers | Privacy policy

     public partial class Form1 : Form
         private MapWinGIS.Shapefile sfNew;
         private int iNewLayer;
         public Form1()
         private void openToolStripMenuItem_Click(object sender, EventArgs e)
             // Create a shapefile to compare geographic coordinates against
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter = "Shapefiles |*.shp";
             if (ofd.ShowDialog(this) != DialogResult.OK) return;
             MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
             sf.Open(ofd.FileName, null);
             int iLayer = axMap1.AddLayer(sf, true);
             axMap1.CursorMode = MapWinGIS.tkCursorMode.cmNone;
             axMap1.SendMouseDown = true;
             axMap1.MouseDownEvent += new AxMapWinGIS._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent);
             // Create a new shapefile, deleting the shapefile of the same name if it already exists.
             sfNew = new MapWinGIS.Shapefile();
             if (System.IO.File.Exists("C:\\TestPoints.shp"))System.IO.File.Delete("C:\\TestPoints.shp");
             if (System.IO.File.Exists("C:\\TestPoints.shx")) System.IO.File.Delete("C:\\TestPoints.shx");
             if (System.IO.File.Exists("C:\\TestPoints.dbf")) System.IO.File.Delete("C:\\TestPoints.dbf");
             sfNew.CreateNew("C:\\TestPoints.shp", MapWinGIS.ShpfileType.SHP_POINT);
             // You have to add at least one field I think
             MapWinGIS.Field fld = new MapWinGIS.Field();
             fld.Name = "Name";
             fld.Type = MapWinGIS.FieldType.STRING_FIELD;
             fld.Width = 50;
             sfNew.StartEditingShapes(true, null);
             int fldIndex = 0;
             sfNew.EditInsertField(fld, ref fldIndex, null);
             sfNew.StopEditingShapes(true, true, null);
             iNewLayer = axMap1.AddLayer(sfNew, true);
             axMap1.set_ShapeLayerPointColor(iNewLayer, 255); // show points as red on the map
         void axMap1_MouseDownEvent(object sender, AxMapWinGIS._DMapEvents_MouseDownEvent e)
             double x = 0;
             double y = 0;
             // Project the screen coordinates to geographic coordinates
             axMap1.PixelToProj(e.x, e.y, ref x, ref y);
             // Start editing the shapefile
             sfNew.StartEditingShapes(true, null);
             // Create a new point with the geographic coordinates
             MapWinGIS.Point pt = new MapWinGIS.Point();
             pt.x = x;
             pt.y = y;
             // Insert the point into a new shape
             MapWinGIS.Shape shp = new MapWinGIS.Shape();
             shp.ShapeType = MapWinGIS.ShpfileType.SHP_POINT;
             int ptIndex = 0;
             shp.InsertPoint(pt, ref ptIndex);
             // Add the shape into the shapefile
             int shpIndex = sfNew.NumShapes;
             if (sfNew.EditInsertShape(shp, ref shpIndex) == false)
             sfNew.EditCellValue(0, shpIndex, shpIndex);
             // Stop editing the shapefile
             sfNew.StopEditingShapes(true, true, null);
             // Update the map with the latest changes.

Retrieved from ""

This page has been accessed 3,934 times. This page was last modified on 20 February 2009, at 01:15.