日期:2014-05-18  浏览次数:21135 次

c#在函数内捕捉鼠标点击事件一般怎么做?
情况大概是这样的:①我需要画一条曲线②我需要从鼠标点击开始,才开始画线。
问题是:我不知道怎么在这个函数内,捕捉鼠标点击事件。
菜鸟,不懂,请多理解,谢谢。
代码:
  private void button2_Click(object sender, EventArgs e)
  {
  ILayer layer = null;
  for (int i = 0; i < axMapControl1.LayerCount; i++)
  {
  layer = axMapControl1.get_Layer(i);
  IFeatureLayer fl = layer as IFeatureLayer;
  IFeatureClass fc = fl.FeatureClass;
  if (fc.ShapeType == esriGeometryType.esriGeometryPolyline && (layer.Name=="地铁线"))
  {
   
  IGeometry geo;//我需要在这个位置捕捉鼠标点击事件,然后进行画线。
  geo= axMapControl1.TrackLine();//这是那个画线的函数。
  IFeature feature = fc.CreateFeature();
  feature.set_Value(fc.FindField("NAME"),"newPolyline");
  feature.Shape = geo;
  feature.Store();
  }
  }

  }

------解决方案--------------------
一下代码创建一个PictureBox ,并且在上面通过鼠标划线,你可以将PictureBox 改为其他你自己的需要划线的对象

C# code

        int x, y;
        bool mousedown=false;

        private void button1_Click(object sender, EventArgs e)
        {
            PictureBox pic = new PictureBox();
            pic.Image = Image.FromFile("c:\\1.bmp");
            pic.Parent = this;
            pic.MouseDown += new MouseEventHandler
                (
                delegate(object obj, MouseEventArgs ex)
                {
                    if (ex.Button == MouseButtons.Left)
                    {
                        x = ex.X;
                        y = ex.Y;
                        mousedown = true;
                    }
                }
                );
            pic.MouseUp += new MouseEventHandler
                (
                delegate(object obj, MouseEventArgs ex)
                {
                    mousedown = false;
                }
                );
            pic.MouseMove += new MouseEventHandler
                (
                delegate(object obj, MouseEventArgs ex)
                {
                    if (mousedown)
                    {
                        ((PictureBox)obj).Left += ex.X - x;
                        ((PictureBox)obj).Top += ex.Y - y;
                    }
                }
                );
        }

------解决方案--------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
System.Collections.Generic.List<mouselocation> mouse = new List<mouselocation>();
protected override void OnMouseMove(MouseEventArgs e)
{
if (flag)
{
mouselocation ms = new mouselocation();
ms.x = e.X;
ms.y = e.Y;
mouse.Add(ms);
}
Grap();
base.OnMouseMove(e);
}

private void Grap()
{
// Refresh();
Graphics gp = this.CreateGraphics();
Brush bsh = new SolidBrush(Color.Red);
Pen p = new Pen(bsh);
for (int i = 0; i < mouse.Count-1; i++)
{
gp.DrawLine(p,mouse[i].x,mouse[i].y,mouse[i+1].x,mouse[i+1].y);
}
}
public bool flag = false;
private void button1_Click(object sender, Event