Raz*_*van 10 c# mouse picturebox coordinates
我有一个装有图片的图片框,我想在单击图像时读取位置(如图片框中的x,y); 这可能吗 ?更重要的是,当我鼠标悬停时,我可以读取这些坐标(点数)吗?
我知道我必须使用给定的事件(鼠标单击和鼠标悬停),但不知道如何读取鼠标指针恰好是的坐标.
Sri*_*vel 26
虽然其他答案都是正确的,但我要加上我的观点.你已经指出你需要为此目的挂钩MouseClick
或MouseOver
事件.实际上,没有必要将这些事件挂钩Coordinates
,你可以Coordinates
在Click
事件本身中获得.
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,因为Click事件的e
参数包装MouseEventArgs
你可以只是强制转换它并使用它.
您可以按以下方式获取X和Y坐标,
this.Cursor = new Cursor(Cursor.Current.Handle);
int xCoordinate = Cursor.Position.X;
int yCoordinate = Cursor.Position.Y;
Run Code Online (Sandbox Code Playgroud)
如果要在图片框中获取坐标,请使用以下代码,
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
}
Run Code Online (Sandbox Code Playgroud)
我只是总结一下答案:
以及许多其他包含MouseClick
鼠标的事件。MouseUp
MouseEventArgs
Location
但是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;
}
Run Code Online (Sandbox Code Playgroud)