Windows API中的逻辑坐标和设备坐标之间混淆 [英] Confused between logical coordinates and device coordinates in Windows API

查看:171
本文介绍了Windows API中的逻辑坐标和设备坐标之间混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究使用两个功能SetWindowExt (...)SetViewportExt (...)的Visual Studio C ++ Windows应用程序项目.我对这两个功能的作用以及为什么要使用它们感到困惑.在搜索这些功能时,我想到了逻辑坐标和设备坐标的概念.

任何人都可以解释这两个概念的重要性吗?

解决方案

设备坐标是最容易理解的.它们与您正在使用的设备(例如屏幕或打印机)直接相关.

例如,让我们看一下屏幕上显示的窗口.设备坐标是相对于特定设备定义的,因此在窗口的情况下,所有内容都将在客户端坐标中.这意味着原点将是窗口工作区的左上角,而y轴将从上到下.由于这是一个屏幕上的元素,因此所有单位均以像素为单位.

您一直都在使用这些工具,因此您可能已经比您想像的更了解它们.例如,每当您处理鼠标事件或调整窗口大小时,都将获取并设置设备坐标.

逻辑坐标考虑了当前的映射模式.每个设备上下文(DC)都可以应用映射模式(GetMapModeSetMapMode).各种可用的映射模式由MM_Xxx值定义.这些不同的映射模式中的每一个都将导致对原点和y轴方向的解释不同. 文档会告诉您它们的确切工作方式. /p>

当您操作设备上下文(例如在其上绘制)时,会考虑当前的映射模式,因此可以使用逻辑坐标.

在默认的MM_TEXT映射模式下,每个逻辑单元都映射到一个设备单元(请记住,对于一个窗口,这将是一个像素),因此不需要转换.在这种映射模式下,逻辑坐标系和设备坐标系的工作方式完全相同.而且由于这是默认设置,并且可能是您大多数时候使用的默认设置,所以这可能是您感到困惑的根源.

相关阅读:坐标空间和变换(MSDN)

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.

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.

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.

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.

Relevant reading: Coordinate Spaces and Transformations (MSDN)

这篇关于Windows API中的逻辑坐标和设备坐标之间混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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