控制PointToClient()与PointToScreen() [英] Control PointToClient() vs PointToScreen()
问题描述
恕我直言,MSDN没有提供 Control.PointToScreen (链接)和 Control.PointToClient (链接)方法。
The MSDN does not provide, IMHO, a clear difference between Control.PointToScreen(link) and Control.PointToClient(link) methods.
有人可以用几个简单的词解释这些方法之间的区别。
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".
我了解PointToScreen的真实屏幕坐标(在屏幕的左上角带有[0,0]),对我来说尤其不清楚。
I understand PointToScreen the real screen coordinate (with [0, 0] in the left upper corner of the screen) of the given point.
例如,调试一些我拥有的代码
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}
谢谢。
推荐答案
最好的解决方法是:相对坐标和绝对坐标。相对坐标是从窗口客户区的左上角开始的相对坐标。窗口的工作区是一个减去边界的窗口。相对坐标很有用,因为用户移动窗口时它们不会改变,并且不依赖于窗口的边框和标题大小。
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.
Winforms中的大多数坐标是相对坐标,例如MouseEventArgs.Location。有些是绝对的,例如Cursor.Position。如果您将一个相对坐标传递给PointToClient,则会出现垃圾,就像在调试会话中看到的那样。它必须是绝对坐标。
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.
某些坐标属性似乎可以兼具,例如Control.Location。在子控件上,它表示控件相对于其容器的位置。表单的位置是绝对的。当您将Control.Location视为来自控件的父级的亲戚时,这种看似矛盾就消失了。窗体的父级是桌面。
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.
常见用法是将相对于一个控件的坐标映射到另一个控件。首先使用 control1.PointToScreen()
映射到绝对屏幕坐标,然后使用 control2.PointToClient()$ c将结果映射到另一个控件$ c>。无论控件的父母是谁,Point值都将根据控件之间的偏移量而变化。
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.
仅通过将绝对坐标传递给PointToClient并将相对坐标传递给PointToScreen来避免麻烦。
Keep out of trouble by only ever passing an absolute coordinate to PointToClient and a relative coordinate to PointToScreen.
这篇关于控制PointToClient()与PointToScreen()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!