I have been watching Charles Petzold's Programming Microsoft Windows with C # Over the past few days. There are many details such as Dongdong and dual, which are easy to confuse or make mistakes, so I will make a summary. This article focuses on the coordinate system in Windows and the representation of points in different coordinate systems.
In Windows, there are three Coordinate systems (four if Desktop Coordinate is used ):
1. Client Coordinate: the Coordinate of the vertex is relative to the upper left corner of the customer zone.
2. Form Coordinate: the Coordinate of the vertex is relative to the upper left corner of the Form.
3. Screen Coordinate: the coordinates of the point are relative to the upper left corner of the Screen.
In most cases, Desktop Coordinate and Screen Coordinate are the same, but they are different if the taskbar is placed on the left or top (if xsreen> xdesktop on the left, similarly, if yscreen> ydesktop) on the top ).
OK. Let's take a look at how to convert in different coordinate systems. Note that.. NET Framework System. windows. forms. control. in the remarks of the Location property, if the Control is Form, the Location property value indicates the upper left corner of Form (represented by screen coordinates ). Therefore, to convert the coordinates (xscreen and yscreen) under Screen Coordinate to the coordinates (xform and yform) under Form Coordinate, the formula is as follows:
Xscreen = xform + Location. X
Yscreen = yform + Location. Y
In the remarks section of the System. Windows. Forms. Form. Platform toplocation attribute of the. NET Framework, the desktop coordinates are based on the screen workspace, And the taskbar is not included in this area. We can convert the coordinates under Desktop Coordinate (xdesktop, ydesktop) and Form Coordinate (xform, yform) to each other:
Xdesktop = xform + export toplocation. X
Ydesktop = yform + export toplocation. Y
After performing an algebraic operation, you can convert between Desktop Coordinate and Screen Coordinate:
Xdesktop = xscreen + export toplocation. X-Location. X
Ydesktop = yscreen + export toplocation. Y-Location. Y
If you want to convert the coordinates under the Client Coordinate, you have to first use the Control instance method PointToClient and PointToScreen to convert the coordinates under the Client Coordinate to those under the Screen Coordinate, then, use the formula above to perform other conversions.