private void toolStripButton1_Click(object sender, EventArgs e)
{
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
PointColl[SelectCount] = new List<SharpMap.Geometries.Point>();
SelectCount++;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
List<SharpMap.Geometries.Geometry> geometries = new List<SharpMap.Geometries.Geometry>();
SharpMap.Geometries.Point p = myMap.ImageToWorld(e.Location);
PointColl[SelectCount-1].Add(p);
geometries.Add(new SharpMap.Geometries.Polygon(new SharpMap.Geometries.LinearRing(PointColl[SelectCount-1])));//选区
SharpMap.Layers.VectorLayer layerSelect = new SharpMap.Layers.VectorLayer("Select"+(SelectCount-1).ToString());
layerSelect.DataSource = new SharpMap.Data.Providers.GeometryProvider(geometries);
Color Mycolor = Color.FromArgb(128, selectionColor);//说明:1-(128/255)=1-0.5=0.5 透明度为0.5,即50%
layerSelect.Style.Fill = new SolidBrush(Mycolor);
layerSelect.SRID = 4326;
if (PointColl[SelectCount - 1].Count != 1)
{
VectorLayer deleteLayer = myMap.GetLayerByName("Select" + (SelectCount - 1).ToString()) as VectorLayer;
myMap.Layers.Remove(deleteLayer);
}
myMap.Layers.Add(layerSelect);
pictureBox1.Image = myMap.GetMap();
}
目的是在新加层的时候把原来的删掉,防止太多造成不透明,但是如果是完整的选层已经做好的话,就保留下来(通过名字来删,这样整的选层的选层就可保留)。
问题在于在选好一个选第二个时发现pictureBox1_MouseClick函数执行了两次,想了下 ,多点了几次toolStripButton1,多触发了几次toolStripButton1_Click函数,结果发现触发的pictureBox1_MouseClick次数更多了,原来是每次点toolStripButton1都会注册一个this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
事件,而点鼠标后注册了几个该事件就会触发几次,所以,改称:
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{
this.pictureBox1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
//this.GotFocus += new EventHandler(WeatherForecast_GotFocus);
PointColl[SelectCount] = new List<SharpMap.Geometries.Point>();
SelectCount++;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
问题解决。