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”.
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}
Thanks.
Best way to think of it is: relative vs absolute 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.Locationfor example. Some are absolute,Cursor.Positionfor 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.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.
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 withcontrol2.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.Keep out of trouble by only ever passing an absolute coordinate to PointToClient and a relative coordinate to PointToScreen.