Where does the coordinate system of window forms stop and start?

I use VB.NET to write a game that works in the form of a window that uses conflict detection. For this, I must understand the positioning system. I know that the coordinates of the windows are formed in the upper left corner and do not include the lower or right edges. But at what numbers do the coordinates start and stop? (What I mean is the coordinate of the upper left corner, that is, the coordinate is almost in the lower corner)

+5
source share
3 answers

The coordinate system depends on whether you are talking about client coordinates or screen coordinates. This is the main task of the Windows UI manager, and WinForms wrappers follow the same pattern.

When you are dealing with client coordinates , the start point (top left) has coordinates (0, 0) . Always. The degree is determined by the width and height of your form, accessible through Me.ClientSize.Width and Me.ClientSize.Height , respectively. Client rectangle, therefore:
{ (0, 0) x times; (ClientSize.Width, ClientSize.Height) } can also be extracted using the ClientRectangle property.

The unique thing about the client area is that it eliminates non-client areas of the form โ€” borders, headers, and other system-dependent properties.


(image taken for illustrative purposes from Jose Menendez Pรณo article on creating the Aero ToolStrip)

You do not need to worry about calculating these sizes (and you should not do this, as they can be changed). You simply work in client coordinates, and the structure will take care of the rest. You use the clientโ€™s coordinates when placing child objects (for example, controls) in your parent form, and you can resize the form by specifying the clientโ€™s size. Its actual size will be calculated automatically, taking into account the non-client area.

Quite rarely, you will have to deal with screen coordinates . You only need those who want to move the form (window) on the screen (which should also be rare, because you do not know what screen size the user has, and you should not try to control where she places her windows). In the screen coordinates, the upper left corner of the main monitor has coordinates (0, 0) . The rest of the coordinate system is based on a virtual screen that takes into account configurations with multiple monitors.

The properties of the Location and Size form give you values โ€‹โ€‹in screen coordinates. If you need to map (convert) between the client and screen coordinates, there are PointToClient and PointToScreen . Pass this location, defined either in terms of screen or client coordinates, respectively, and they will convert it to another coordinate system.

The only other complication that should be noted is that Windows uses exclusive endpoint rectangles . The WinForms wrapper stores this convention in its Rectangle structure. You should almost never have to worry about this, as it is indeed a very natural system when you understand it. In addition, all parts and parts of the WinForms structure use the convention, so if you just walk around dots, sizes and rectangles, you are unlikely to run into problems. But this is what you need to know. Think of it this way: your client area has a rectangle { (0, 0) & times; (ClientSize.Width, ClientSize.Height) }, as we saw earlier. If you must fill this rectangle with solid color, the fill will extend from the point (0, 0) to the point (ClientSize.Width - 1, ClientSize.Height - 1) .

+5
source

If you stay in your form, you can calculate it by "width" and "height". Also you have a "left" and a "top".

The start is (left = 0 and top = 0), and it ends in the lower right corner with the coordinates of the values โ€‹โ€‹"width" and "height".

0
source

The Windows Forms application determines the position of the window on the screen in screen coordinates. For screen coordinates, the origin is the upper left corner of the screen. The full position of the 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. ( MSDN )

Thus, the upper left corner is (0, 0) , and the lower right corner is (Form1.Width, Form1.Height) .

0
source

Source: https://habr.com/ru/post/1241812/


All Articles