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, EventArgs e)
{
if (button1.Text.Trim() == "开始")
{
button1.Text = "停止";
flag = true;
}
else
{
button1.Text = "开始";
flag = false;
mouse.Clear();
Refresh();
}
}
}
public class mouselocation
{
public int x
{ get; set; }
public int y
{ get; set; }
}
[解决办法]
具体我不太清楚,
我觉得你使用库的教程里面应该有例子,你可以找找;
流程的话,我想是不是应该是这样的:
1. 点击工具栏上的画线按钮:程序设置一下当前工具为画线
2. 在控件上点击:程序在控件的MouseDown里面判断当前工具是什么,如果是画线,那么执行TrackLine();(保存是否在这个里面,我不确定)
3. 画完:这个可能TrackLine等方法会触发一个类似FinishDrawing事件,在该事件的处理函数里面保存刚刚描画的元素