Control PointToClient() vs PointToScreen()

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.


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.

Hope that clears the confusion


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.


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.

Tags:

.Net

Winforms