我有一个带有图片加载的图片框,当我单击该图像时,我想读取位置(如在图片框中的x,y);这可能吗?更重要的是,当我鼠标通过时,我可以读取这些坐标(点)吗?
我知道我必须使用给定的事件(鼠标单击和鼠标结束),但不知道如何读取鼠标指针所在的坐标。
发布于 2013-08-04 08:35:21
虽然其他答案是正确的,但让我补充一下我的观点。您已经指出,为此您需要连接MouseClick或MouseOver事件。实际上,这不需要将这些事件挂钩以获得Coordinates,您可以在Click事件本身中获取Coordinates。
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
}上面的代码可以工作,因为Click的e参数包装了MouseEventArgs,您可以直接转换并使用它。
发布于 2013-08-04 08:03:39
你可以得到X和Y坐标如下,
this.Cursor = new Cursor(Cursor.Current.Handle);
int xCoordinate = Cursor.Position.X;
int yCoordinate = Cursor.Position.Y;如果要获取图片框中的坐标,请使用以下代码,
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
}发布于 2013-08-04 08:14:41
我只想总结一下答案:
在MouseClick、MouseUp和许多其他事件中,有一个包含鼠标Location的MouseEventArgs。
但是,在MouseHover中没有MouseEventArgs,因此,如果需要游标的位置,请使用Coder示例:
private void Form1_MouseHover(object sender, EventArgs e)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
int xCoordinate = Cursor.Position.X;
int yCoordinate = Cursor.Position.Y;
}https://stackoverflow.com/questions/18040945
复制相似问题