单击时读取图片框鼠标坐标

Raz*_*van 10 c# mouse picturebox coordinates

我有一个装有图片的图片框,我想在单击图像时读取位置(如图片框中的x,y); 这可能吗 ?更重要的是,当我鼠标悬停时,我可以读取这些坐标(点数)吗?

我知道我必须使用给定的事件(鼠标单击和鼠标悬停),但不知道如何读取鼠标指针恰好是的坐标.

Sri*_*vel 26

虽然其他答案都是正确的,但我要加上我的观点.你已经指出你需要为此目的挂钩MouseClickMouseOver事件.实际上,没有必要将这些事件挂钩Coordinates,你可以CoordinatesClick事件本身中获得.

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你可以只是强制转换它并使用它.


Kur*_*ran 5

您可以按以下方式获取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)


No *_*ame 5

我只是总结一下答案:

以及许多其他包含MouseClick鼠标的事件。MouseUpMouseEventArgsLocation

但是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)