I have a Picture Box with a picture loaded and I want to read the location (as in x,y inside the Picture Box) when I click the image; is this possible ? Even more, can i read these coordinates (Points) when i mouse over ?
我有一个装有图片的图片框,当我点击图片时,我想要读取位置(如图片框中的x,y);这可能吗 ?更重要的是,当我鼠标悬停时,我可以读取这些坐标(点数)吗?
I know i have to use the events given (Mouse Click and Mouse Over) but don't know how to read the coordinates where the mouse pointer happens to be.
我知道我必须使用给定的事件(鼠标单击和鼠标悬停),但不知道如何读取鼠标指针恰好是的坐标。
4 个解决方案
#1
21
Though other answers are correct let me add my point to it. You've pointed that you need to hook up MouseClick
or MouseOver
events for this purpose. Actually that is no need to hook those events to get Coordinates
, you can get the Coordinates
in just Click
event itself.
虽然其他答案是正确的,但我要加上我的观点。您已指出需要为此目的连接MouseClick或MouseOver事件。实际上,不需要将这些事件挂钩来获取坐标,您可以在Click事件本身中获取坐标。
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
}
The above code works since Click event's e
argument wraps MouseEventArgs
you can just cast it and make use of it.
上面的代码可以工作,因为Click事件的参数包装了MouseEventArgs,你可以将它强制转换并使用它。
#2
4
i'll just sum up the answers:
我只想总结一下答案:
in MouseClick
, MouseUp
and a lot of other events you have the MouseEventArgs
which contains Location
of the mouse.
在MouseClick,MouseUp和许多其他事件中,你有MouseEventArgs,它包含鼠标的位置。
in MouseHover
however you don't have MouseEventArgs
and therefor, if you need the cursor's location, use Coder example:
但是在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;
}
#3
2
You can get the X and Y coordinates as follows,
你可以得到如下的X和Y坐标,
this.Cursor = new Cursor(Cursor.Current.Handle);
int xCoordinate = Cursor.Position.X;
int yCoordinate = Cursor.Position.Y;
If you want to get the coordinate within the picture box, use the following code,
如果要获取图片框中的坐标,请使用以下代码,
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
}
#4
1
What about hooking up the MouseUp event and then getting the location from the MouseEventArgs?
如何连接MouseUp事件然后从MouseEventArgs获取位置?
Like this:
喜欢这个:
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
Point mousePointerLocation = e.Location;
}
#1
21
Though other answers are correct let me add my point to it. You've pointed that you need to hook up MouseClick
or MouseOver
events for this purpose. Actually that is no need to hook those events to get Coordinates
, you can get the Coordinates
in just Click
event itself.
虽然其他答案是正确的,但我要加上我的观点。您已指出需要为此目的连接MouseClick或MouseOver事件。实际上,不需要将这些事件挂钩来获取坐标,您可以在Click事件本身中获取坐标。
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
}
The above code works since Click event's e
argument wraps MouseEventArgs
you can just cast it and make use of it.
上面的代码可以工作,因为Click事件的参数包装了MouseEventArgs,你可以将它强制转换并使用它。
#2
4
i'll just sum up the answers:
我只想总结一下答案:
in MouseClick
, MouseUp
and a lot of other events you have the MouseEventArgs
which contains Location
of the mouse.
在MouseClick,MouseUp和许多其他事件中,你有MouseEventArgs,它包含鼠标的位置。
in MouseHover
however you don't have MouseEventArgs
and therefor, if you need the cursor's location, use Coder example:
但是在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;
}
#3
2
You can get the X and Y coordinates as follows,
你可以得到如下的X和Y坐标,
this.Cursor = new Cursor(Cursor.Current.Handle);
int xCoordinate = Cursor.Position.X;
int yCoordinate = Cursor.Position.Y;
If you want to get the coordinate within the picture box, use the following code,
如果要获取图片框中的坐标,请使用以下代码,
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
}
#4
1
What about hooking up the MouseUp event and then getting the location from the MouseEventArgs?
如何连接MouseUp事件然后从MouseEventArgs获取位置?
Like this:
喜欢这个:
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
Point mousePointerLocation = e.Location;
}