Reputation: 409
I am using VB.NET to write a game that runs in a windows form that uses collision detection. In order to achieve this, I have to be able to understand the positioning system. I know that windows form coordinates start at the top-left, and don't include the bottom or right edges. But at what numbers do the coordinates start and stop? (What i mean is What is the top left corner coordinate, what is the almost bottom right corner coordinate)
Upvotes: 5
Views: 2701
Reputation: 244742
The coordinate system depends on if you're talking about client coordinates or screen coordinates. This is a basic Windows UI manager thing, and the WinForms wrappers follow the same pattern.
When you're dealing with client coordinates, the origin (top-left) point has coordinates (0, 0)
. Always. The extent is defined by the width and height of your form, accessible via Me.ClientSize.Width
and Me.ClientSize.Height
, respectively. The client rectangle is, therefore:
{ (0, 0)
× (ClientSize.Width, ClientSize.Height)
}, also retrievable using the ClientRectangle
property.
The unique thing about the client area is that it excludes the non-client areas of the form—the borders, the title bars, and other system-dependent properties.
(Image taken for illustrative purposes from Jose Menendez Póo's article on creating an Aero ToolStrip)
You don't have to worry about calculating these sizes (and you shouldn't, either, since they're subject to change). You just work in client coordinates, and the framework will take care of the rest. You use client coordinates when positioning child objects (such as controls) on their parent form, and you can even resize the form by specifying a client size. Its actual size will be calculated automatically, taking into account the non-client area.
It is quite rare that you will ever have to deal in screen coordinates. You only need those if you want to move a form (window) around on the screen (which should also be rare, because you have no idea what size screen the user has nor should you try to control where she places her windows). In screen coordinates, the top-left corner of the primary monitor has coordinates (0, 0)
. The rest of the coordinate system is based on the virtual screen, which takes into account multiple-monitor configurations.
A form's Location
and Size
properties give you values in screen coordinates. Should you need to map (convert) between client and screen coordinates, there are PointToClient
and PointToScreen
methods. Pass these a location defined either in terms of screen or client coordinates, respectively, and they will convert it to the other coordinate system.
The only other complication to note is that Windows uses endpoint-exclusive rectangles. The WinForms wrapper retains that convention in its Rectangle
structure. You hardly ever have to worry about this, since this is really a very natural system once you understand it. Plus, all of the pieces and parts of the WinForms framework use the convention, so if you're just passing around points and sizes and rectangles, you aren't likely to run into trouble. But it is something to be aware of. Think of it this way: your client area has the rectangle { (0, 0)
× (ClientSize.Width, ClientSize.Height)
}, as we saw earlier. If you were to fill in this rectangle with a solid color, the fill would extend from point (0, 0)
to point (ClientSize.Width - 1, ClientSize.Height - 1)
.
Upvotes: 5
Reputation: 2107
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. (MSDN)
So upper left corner is (0, 0)
and lower right corner is (Form1.Width, Form1.Height)
.
Upvotes: 0
Reputation: 984
If you stay within your form, you can calculate it by "width" and "height". Also you have "left" and "top".
Starting is (left = 0 and top = 0) and it ends on the right bottom with the coordinates of the values "width" and "height".
Upvotes: 0