C# 查找屏幕上点的坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1438283/
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-06 16:32:33  来源:igfitidea点击:

Find Coordinates for point on screen?

c#graphicscoordinatespoint

提问by user170644

The thing is I have some graphics shown in a form, rectangle for example, and I want to capture when the point gets over thees fields. So I Thoght I try to find the corrrdinates for thees rectangles, but as thay are the coords in the form it dose not match the ones with the mouse location.

问题是我有一些以表格形式显示的图形,例如矩形,我想在点越过这些字段时进行捕获。所以我认为我试图找到这些矩形的 corrrdinates,但是由于形式中的坐标,它与鼠标位置的坐标不匹配。

So I wonder is there a way to find what coord on the screen a Point has on the screen and not in the form or controller?

所以我想知道有没有办法在屏幕上找到 Point 在屏幕上而不是在表单或控制器中的坐标?

采纳答案by Henk Holterman

Each control hs PointToFoo methods for conversion. Note that you should call this from the parent of the object who's location you want:

每个控件都有用于转换的 PointToFoo 方法。请注意,您应该从您想要的位置的对象的父级调用它:

Point scrPos = this.PointToScreen(panel1.Location);

Alternatively, you can get the panel's screen coordinates with:

或者,您可以使用以下命令获取面板的屏幕坐标:

Point scrPos = panel1.PointToScreen(new Point(0,0));

Note that the above two examples could gve different result due to the border-size of the panel.

请注意,由于面板的边框大小,上述两个示例可能会给出不同的结果。

回答by Fiona - myaccessible.website

If you are using the graphics object for the form by calling this.CreateGraphics()within the form, then the coordinates used when you draw the rectangle should be exactly the same as those returned by the click event on the form.

如果通过在窗this.CreateGraphics()体内调用来使用窗体的图形对象,则绘制矩形时使用的坐标应与窗体上单击事件返回的坐标完全相同。

回答by Jon Cage

Do you know what coordinates your pointer is in? You can get the coordinates for your window with a call to GetWindowRect()and subtract the top/left from your mouse cursor to get client coordinates.

你知道你的指针在什么坐标吗?您可以通过调用GetWindowRect()并从鼠标光标中减去顶部/左侧以获得客户端坐标来获取窗口的坐标。

I seem to remember there being a function to do that for you in fact, but it's been some time since I dabbled in custom GUI controls.

我似乎记得实际上有一个函数可以为您执行此操作,但是自从我涉足自定义 GUI 控件以来已经有一段时间了。