Redigera

Dela via


Windows Forms Coordinates

The coordinate system for a Windows Form is based on device coordinates, and the basic unit of measure when drawing in Windows Forms is the device unit (typically, the pixel). Points on the screen are described by x- and y-coordinate pairs, with the x-coordinates increasing to the right and the y-coordinates increasing from top to bottom. The location of the origin, relative to the screen, will vary depending on whether you are specifying screen or client coordinates.

Screen Coordinates

A Windows Forms application specifies the position of a window on the screen in screen coordinates. For screen coordinates, the origin is the upper-left corner of the screen. The full position of a window is often described by a Rectangle structure containing the screen coordinates of two points that define the upper-left and lower-right corners of the window.

Client Coordinates

A Windows Forms application specifies the position of points in a form or control using client coordinates. The origin for client coordinates is the upper-left corner of the client area of the control or form. Client coordinates ensure that an application can use consistent coordinate values while drawing in a form or control, regardless of the position of the form or control on the screen.

The dimensions of the client area are also described by a Rectangle structure that contains client coordinates for the area. In all cases, the upper-left coordinate of the rectangle is included in the client area, while the lower-right coordinate is excluded. Graphics operations do not include the right and lower edges of a client area. For example the FillRectangle method will fill up to the right and lower edge of the specified rectangle, but will not include these edges.

Mapping From One Type of Coordinate to Another

Occasionally, you may need to map from screen coordinates to client coordinates. You can easily accomplish this by using the PointToClient and PointToScreen methods available in the Control class. For example, the MousePosition property of Control is reported in screen coordinates, but you may want to convert these to client coordinates.

See also