line drawing on mouse move event

Mar 15, 2012 at 10:24 AM
Edited Mar 16, 2012 at 10:54 AM

Hi guys!

In my application I am trying to write code to enable user to draw line. I have written following code to let user visualize the line while drawing(just like the application MS-PAINT in Microsoft Windows). But its not working likewise.I have written following code =>

 private void toolStripdrwlin_Click(object sender, EventArgs e)       
{                  
      count++;           
       map1.FunctionMode = FunctionMode.None;     
      
       drawclick = true;       
      drawpolyclick = false;       
      drawpointclick = false;

       polydrawn = false;   
        linedrawn = false;       
      fandrawn = false;
        map1.Cursor = Cursors.Cross;

        lineF = new FeatureSet(FeatureType.Line);   

        lineF.Projection = map1.Projection;
        DataColumn lineid = new DataColumn("ID");     
        DataColumn linename = new DataColumn("NAME");             

         lineF.DataTable.Columns.Add(lineid);         
  lineF.DataTable.Columns.Add(linename);         
          lineLayer = (MapLineLayer)map1.Layers.Add(lineF);   
        LineSymbolizer symbol = new LineSymbolizer(Color.Chocolate, 8);
            lineLayer.Symbolizer = symbol;
            Airname = "Airway" + (count).ToString(); 

          lineLayer.LegendText = Airname;
            firstClick = true;                   

}

//----------------------------------------------------------------------------------------------------------

private void map1_MouseDown_1(object sender, MouseEventArgs e)       
{ 
 if (linedrawn == true)           
    return;

      if (drawclick == true)           
      {  
                 drawpolyclick = false;         
       map1.FunctionMode = FunctionMode.None; 
              map1.Cursor = Cursors.Cross;
                if (e.Button == MouseButtons.Left)         

      {                   

Coordinate coord = map1.PixelToProj(e.Location);   
                lastpoint = e.Location;                 
                        if (firstClick)         
        {                     

List lineArray = new List();         
              LineString lineGeometry = new LineString(lineArray);           
            IFeature lineFeature = lineF.AddFeature(lineGeometry);                 
      lineFeature.Coordinates.Add(coord);
                        lineID = lineID + 1;       

                lineFeature.DataRow["ID"] = lineID;
                        linename = "line" + (lineID).ToString();   
                    lineFeature.DataRow["NAME"] = linename;               

                        firstClick = false;
                       }                 

  }             

  else         
      if (e.Button == MouseButtons.Right)   
          {                   

firstClick = true;                                  
    map1.ResetBuffer();               
    lineF.SaveAs("D:\\Airway" + (lincnt++) + ".shp", false);       
            lineF.InvalidateVertices();             
      lineF = null;               
    MessageBox.Show("The line shapefile has been saved.");       
            map1.Cursor = Cursors.Arrow;             
      linedrawn = true;             
      lineID = 0;                   
            }   
      }
}

//----------------------------------------------------------------------------------------------------------

private void map1_MouseMove(object sender, MouseEventArgs e)       
{           
if (linedrawn == true)               
return;

 if (drawclick == true)           
{               
if (e.Button == MouseButtons.Left)               
{                   
Coordinate cd = map1.PixelToProj(e.Location);
                    IFeature existingFeature = lineF.Features[lineF.Features.Count - 1];                   

existingFeature.Coordinates.Add(cd);                   
                    if (existingFeature.Coordinates.Count >= 2)                   

{                       

lineF.InitializeVertices();                       
map1.ResetBuffer();                   
}                   
firstClick = true;                   
linedone = false;               
}           
}
}

//----------------------------------------------------------------------------------------------------------

private void map1_MouseUp(object sender, MouseEventArgs e)       

{           
linedone = true;           
if (linedrawn == true)               
return;           
if (drawclick == true)           
{               

if(e.Button == MouseButtons.Left)               

{                   

Coordinate cd = map1.PixelToProj(e.Location);                   
if (linedone == true)                   
{                       
IFeature existingFeature = lineF.Features[lineF.Features.Count - 1];                       

existingFeature.Coordinates.Add(cd);                                            

   if (existingFeature.Coordinates.Count >= 2)                       

{                           
lineF.InitializeVertices();                           
map1.ResetBuffer();                       

}                       
firstClick = true;                       
linedone = false;                   
}               
}                          
}                                           
}

 

I think I am going wrong in map1_MouseMove() function.  because in output I am not getting line instead a crocked curve. Can anybody please suggest something??

Mar 16, 2012 at 9:28 AM
Edited Mar 16, 2012 at 10:55 AM

Its done guys with just a small code snippet added in the map1_MouseMove() event. By Saving the previous co-ordinate and simultaneously removing it as the coordinate changes at mouse-move.

code=>

Coordinate hold = new Coordinate();
if(hold!= cd)                   
 {                         
            existingFeature.Coordinates.Remove(hold);                         
              lineF.InitializeVertices();                                              
 }                   
hold = cd;