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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18040945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:07:23  来源:igfitidea点击:

Read picture box mouse coordinates on click

c#mousecoordinatespicturebox

提问by Razvan

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.

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

采纳答案by Sriram Sakthivel

Though other answers are correct let me add my point to it. You've pointed that you need to hook up MouseClickor MouseOverevents for this purpose. Actually that is no need to hook those events to get Coordinates, you can get the Coordinatesin just Clickevent itself.

尽管其他答案是正确的,但让我添加我的观点。你已经指出你需要为此目的连接MouseClickMouseOver事件。其实这是没有必要挂钩这些事件得到Coordinates,你可以得到Coordinates的只是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 eargument wraps MouseEventArgsyou can just cast it and make use of it.

上面的代码有效,因为 Click 事件的e参数包装了MouseEventArgs你可以直接转换它并使用它。

回答by jmelhus

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;
}

回答by Kurubaran

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;
}

回答by No Idea For Name

i'll just sum up the answers:

我只是总结一下答案:

in MouseClick, MouseUpand a lot of other events you have the MouseEventArgswhich contains Locationof the mouse.

in MouseClickMouseUp以及许多其他MouseEventArgs包含Location鼠标的事件。

in MouseHoverhowever you don't have MouseEventArgsand therefor, if you need the cursor's location, use Coder example:

MouseHover但是你没有MouseEventArgs和为此,如果你需要将光标的位置,使用编码器例如:

  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;
  }