windows窗体的坐标系在哪里停止和开始? [英] Where does the coordinate system for windows forms stop and start?

查看:30
本文介绍了windows窗体的坐标系在哪里停止和开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.NET 编写一个以使用碰撞检测的 Windows 形式运行的游戏.为了实现这一点,我必须能够理解定位系统.我知道窗口窗体坐标从左上角开始,不包括底部或右侧边缘.但是坐标在什么数字开始和停止?(我的意思是什么是左上角坐标,什么是几乎右下角坐标)

解决方案

坐标系取决于您谈论的是客户端坐标还是屏幕坐标.这是一个基本的 Windows UI 管理器,WinForms 包装器遵循相同的模式.

当您处理客户端坐标时,原点(左上角)的坐标为(0, 0).总是.范围由表单的宽度和高度定义,可分别通过 Me.ClientSize.WidthMe.ClientSize.Height 访问.因此,客户端矩形是:
{ (0, 0) ×(ClientSize.Width, ClientSize.Height) },也可以使用 ClientRectangle 属性检索.

客户区的独特之处在于它排除表单的非客户区——边框、标题栏和其他系统相关的属性.

        
        (图片来自 .WinForms 包装器在其 Rectangle 结构中保留了该约定.您几乎不必担心这一点,因为一旦您了解它,这确实是一个非常自然的系统.另外,WinForms 框架的所有部分和部分都使用约定,因此如果您只是传递点、大小和矩形,则不太可能遇到麻烦.但这是需要注意的.可以这样想:您的客户区有一个矩形 { (0, 0) ×(ClientSize.Width, ClientSize.Height) },正如我们之前看到的.如果你用纯色填充这个矩形,填充将从点(0, 0)延伸到点(ClientSize.Width - 1,ClientSize.Height - 1).

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)

解决方案

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).

这篇关于windows窗体的坐标系在哪里停止和开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆