Windows 窗体坐标Windows Forms Coordinates

Windows 窗体坐标系统取决于设备坐标,并在 Windows 窗体中绘制时的度量值的基本单位是将设备单位 (通常情况下,像素)。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). 在屏幕上的点的 x 坐标和 y 坐标对所述增加到右侧和 y 坐标增加从上到下的 x 坐标。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

Windows 窗体应用程序在屏幕坐标中屏幕上指定窗口的位置。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. 窗口的完整位置通常描述由Rectangle结构,它包含两个点定义窗口的左上角和右下角的屏幕坐标。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

Windows 窗体应用程序窗体或控件使用客户端坐标中指定点的位置。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.

客户端区域的尺寸,还介绍了通过Rectangle结构,其中包含工作区坐标中的区域。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. 例如FillRectangle方法将被填满到指定的矩形中,右和下边缘,但不是会包括这些边缘。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. 您可以轻松地实现此目的通过使用PointToClientPointToScreen方法中提供Control类。You can easily accomplish this by using the PointToClient and PointToScreen methods available in the Control class. 例如,MousePosition属性的Control屏幕坐标中报告但你可能想要将它们转换成工作区坐标中。For example, the MousePosition property of Control is reported in screen coordinates, but you may want to convert these to client coordinates.

请参阅See also