Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control PointToClient() vs PointToScreen()

Tags:

.net

winforms

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.

like image 847
serhio Avatar asked Dec 16 '09 10:12

serhio


3 Answers

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.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.

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 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.

Keep out of trouble by only ever passing an absolute coordinate to PointToClient and a relative coordinate to PointToScreen.

like image 144
Hans Passant Avatar answered Nov 02 '22 23:11

Hans Passant


The PointToClient method is the reverse of the PointToScreen method.

(If it wasn't so long and repetitive, they would be named ScreenPointToClientPoint and 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.

like image 40
Guffa Avatar answered Nov 03 '22 00:11

Guffa


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.

PointToScreen converts client coordinates to screen coordinates. PointToClient does the reverse: it converts screen coordinates to client coordinates.

like image 38
Thomas Avatar answered Nov 03 '22 01:11

Thomas