.net 控制 PointToClient() 与 PointToScreen()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1913682/
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
Control PointToClient() vs PointToScreen()
提问by serhio
The MSDN does not provide, IMHO, a clear difference between Control.PointToScreen(link) and Control.PointToClient(link) methods.
恕我直言,MSDN 没有提供Control.PointToScreen(链接)和Control.PointToClient(链接)方法之间的明显区别。
Is there somebody who could explain in a few simple words what is the difference between these methods. Especially is unclear for me the notion of "Client".
有没有人可以用几个简单的词来解释这些方法之间的区别。对我来说,“客户”的概念尤其不清楚。
I understand PointToScreen the real screen coordinate (with [0, 0] in the left upper corner of the screen) of the given point.
我理解 PointToScreen 给定点的真实屏幕坐标(屏幕左上角为 [0, 0])。
By example, debugging some code I have
例如,调试我有的一些代码
?click.Location
{X = 3 Y = 9}
?shapeSender.PointToClient(click.Location)
{X = -470 Y = -565}
?shapeSender.PointToScreen(click.Location)
{X = 476 Y = 583}
Thanks.
谢谢。
回答by Hans Passant
Best way to think of it is: relative vs absolution coordinates. Where the relative coordinate is relative from the upper left corner of the client area of a window. The client area of a window is a window minus its border. Relative coordinates are useful because they don't change when the user moves a window and don't depend on the border and caption size of the window.
最好的思考方式是:相对坐标与绝对坐标。其中相对坐标是相对于窗口客户区左上角的。窗口的客户区是一个窗口减去它的边框。相对坐标很有用,因为它们在用户移动窗口时不会改变,也不依赖于窗口的边框和标题大小。
Most coordinates in Winforms are relative coordinates, MouseEventArgs.Location for example. Some are absolute, Cursor.Position for example. If you pass a relative coordinate to PointToClient you'll get garbage, as you saw in your debug session. It must be an absolute coordinate.
Winforms 中的大多数坐标都是相对坐标,例如 MouseEventArgs.Location。有些是绝对的,例如 Cursor.Position。如果您将相对坐标传递给 PointToClient,您将得到垃圾,正如您在调试会话中看到的那样。它必须是一个绝对坐标。
Some coordinate properties can seemingly be both, Control.Location for example. On a child control it represents the control's location relative from its container. A form's Location is absolute. That seeming contradiction disappears when you think a Control.Location as relative from a control's Parent. The Parent of a form is the desktop.
一些坐标属性似乎可以两者兼而有之,例如 Control.Location。在子控件上,它表示控件相对于其容器的位置。表单的位置是绝对的。当您认为 Control.Location 相对于控件的父级时,这种看似矛盾的现象就消失了。窗体的父级是桌面。
A common usage is to map a coordinate relative to one control to another control. First map to absolute screen coordinates with control1.PointToScreen(), then map the result to the other control with control2.PointToClient(). The Point value changes by the offset between the controls, regardless of who their parents are. Doing it any other way is very painful.
一种常见的用法是将相对于一个控件的坐标映射到另一个控件。首先使用 映射到绝对屏幕坐标control1.PointToScreen(),然后使用 将结果映射到另一个控件control2.PointToClient()。无论他们的父母是谁,Point 值都会随着控件之间的偏移而变化。以任何其他方式这样做是非常痛苦的。
Keep out of trouble by only ever passing an absolute coordinate to PointToClient and a relative coordinate to PointToScreen.
只需将绝对坐标传递给 PointToClient,将相对坐标传递给 PointToScreen,就可以避免麻烦。
回答by Guffa
The PointToClientmethod is the reverse of the PointToScreenmethod.
该PointToClient方法与该PointToScreen方法相反。
(If it wasn't so long and repetitive, they would be named ScreenPointToClientPointand ClientPointToScreenPoint.)
(如果它不是那么长,重复,它们将被命名为ScreenPointToClientPoint和ClientPointToScreenPoint。)
You use the conversions when you have one kind of coordinates and need the other, for example if you have the coordinates of a mouse click relative to the screen, and need to know where in the control the user clicked.
当您有一种坐标而需要另一种坐标时,您可以使用转换,例如,如果您有鼠标点击相对于屏幕的坐标,并且需要知道用户在控件中点击的位置。
If you convert a screen point that is outside the client area, you will get coordinate components that are either negative or larger than the size of the control client area.
如果您转换客户区之外的屏幕点,您将获得负数或大于控件客户区大小的坐标分量。
回答by Thomas
The "client" coordinates are relative to the top left of a control's client area. The "screen" coordinates are relative to the top left of the (primary) monitor.
“客户”坐标相对于控件客户区的左上角。“屏幕”坐标相对于(主)监视器的左上角。
The "client area" is the area of a control in which child controls can be placed. The client rectangle of a form is the area inside the form, excluding the borders and title bar. For most other controls, the client area is the same as the area that the control occupies on the screen.
“客户区”是可以放置子控件的控件区域。窗体的客户端矩形是窗体内部的区域,不包括边框和标题栏。对于大多数其他控件,客户区与控件在屏幕上占据的区域相同。
PointToScreenconverts client coordinates to screen coordinates. PointToClientdoes the reverse: it converts screen coordinates to client coordinates.
PointToScreen将客户端坐标转换为屏幕坐标。PointToClient反过来:它将屏幕坐标转换为客户端坐标。
回答by A9S6
Suppose a screen is 800x600 and a window is at 50,50 having a size of 200x200 pixels. If the point P lies at 10,10 relative to the window's upper-left then PointToScreen(P) will return 60,60. If this P is given to PointToClient(P) with the window handle then we will get 10,10 again.
假设屏幕为 800x600,窗口大小为 50,50,大小为 200x200 像素。如果点 P 位于相对于窗口左上角的 10,10,则 PointToScreen(P) 将返回 60,60。如果将此 P 与窗口句柄一起提供给 PointToClient(P),那么我们将再次获得 10,10。
Hope that clears the confusion
希望能消除困惑

