Windows API 中逻辑坐标和设备坐标之间的混淆
我一直在研究一个 Visual Studio C++ Windows 应用程序项目,它使用了两个函数 SetWindowExt (...)
和 SetViewportExt (...)
.我对这两个功能的作用以及为什么它们是必要的感到困惑.搜索这些函数,我想到了逻辑坐标和设备坐标的概念.
I have been looking into a Visual Studio C++ Windows application project which used two functions SetWindowExt (...)
and SetViewportExt (...)
. I am confused about what these two functions do and why they are necessary. Searching about these functions, I came to the concept of logical coordinates and device coordinates.
谁能解释一下这两个概念的重要性?
Can anyone please explain what is the importance of these two concepts?
推荐答案
设备坐标是最容易理解的.它们与您使用的设备直接相关,例如屏幕或打印机.
Device coordinates are the simplest to understand. They are directly related to the device that you're using―e.g., the screen or a printer.
例如,让我们看一下屏幕上显示的窗口.设备坐标是相对于特定设备定义的,因此在窗口的情况下,一切都将在客户端坐标中.这意味着原点将是窗口客户区的左上角,y 轴将从上到下运行.所有单位都以像素为单位,因为这是一个屏幕元素.
For an example, let's look at a window displayed on the screen. Device coordinates are defined relative to a particular device, so in the case of a window, everything will be in client coordinates. That means the origin will be the upper-left corner of the window's client area and the y-axis will run from top to bottom. All units are measured in pixels, since this is an on-screen element.
你一直在使用这些,所以你可能已经比你想象的更了解它们了.例如,每当您处理鼠标事件或调整窗口大小时,您都会获取并设置设备坐标.
You use these all the time, so you probably already understand them better than you think. For example, whenever you handle a mouse event or a window resize, you get and set device coordinates.
逻辑坐标考虑当前的映射模式.每个设备上下文 (DC) 都可以应用一个映射模式(GetMapMode
和 SetMapMode
).各种可用的映射模式由 MM_Xxx
值定义.这些不同的映射模式中的每一种都会导致原点和 y 轴方向的解释不同.文档会告诉你它们是如何工作的.
Logical coordinates take the current mapping mode into account. Each device context (DC) can have a mapping mode applied to it (GetMapMode
and SetMapMode
). The various available mapping modes are defined by the MM_Xxx
values. Each of these different mapping modes will cause the origin and y-axis direction to be interpreted differently. The documentation will tell you exactly how they work.
当您操作设备上下文(例如,在其上绘制)时,会考虑当前的映射模式,因此您可以使用逻辑坐标.
When you manipulate a device context (e.g., draw onto it), the current mapping mode is taken into account and thus you work with logical coordinates.
使用默认的 MM_TEXT
映射模式,每个逻辑单元映射到一个设备单元(请记住,对于窗口,这将是一个像素),因此不需要转换.在这种映射模式下,逻辑坐标系和设备坐标系的工作方式完全相同.由于这是默认设置,并且可能是您大部分时间使用的默认设置,因此它可能是您困惑的根源.
With the default MM_TEXT
mapping mode, each logical unit maps to one device unit (remember, for a window, this would be one pixel), so no conversion is required. In this mapping mode, the logical and device coordinate systems work exactly the same way. And since this is the default and probably the one you work with most of the time, it is probably the source of your confusion.
相关阅读: 坐标空间和变换一个>(MSDN)
相关文章