GDI win32 中从 TopLeft 0,0 到 BottomLeft 0,0

发布于 2024-12-22 12:29:11 字数 899 浏览 0 评论 0原文

如何将窗口中的坐标从 0,0 左上角更改为 0,0 左下角。

我尝试了各种解决方案
SetMapMode(hdc,MM_TEXT);,
SetViewportExtEx(hdc,0,-clientrect.bottom,NULL);
SetViewPortOrgEx(hdc,0,-clientrect.bottom,NULL);
SetWindowOrgEx(hdc,0,-clientrect.bottom,NULL);
SetWindowExtEx(hdc,0,-clientrect.bottom,NULL);
我什至尝试过谷歌寻找解决方案,但没有成功,所以我向互联网上更有经验的人求助。

我的想法是我正在创建一个用于线性插值的自定义控件,我可以通过右上角的 x,y 反转坐标系,但我希望它是正确的。目前,当我尝试绘制它时,我得到了反向线性插值,因为我无法让坐标位于左下角。

我正在使用 win32 api,我怀疑我可以跳过代码,因为屏幕坐标系在所有系统上几乎相同,我的意思是 0,0 “始终”位于屏幕的左上角,如果您将其保持为标准 2d窗户和框架。

我真的不想要一个完整的代码示例来减轻你们打字的痛苦,但我想要一些方向,因为我似乎无法掌握在 win32 api 中翻转坐标的简单概念。

谢谢,圣诞快乐

编辑!

我想对这个问题添加我自己的答案,因为我使用简单的数学来扭转观点。

例如,如果我得到了值对 x,y (150,57) 和另一对 x,y (100,75),那么我使用了这个公式 height + (-1 * y) 瞧,我得到一个正确的笛卡尔坐标字段:)当然,在这个例子中,高度是未定义的变量,但在我的应用程序中,它的高度是 200px。

How can I change transform the coordinates in a window from 0,0 topleft to 0,0 bottomleft.

I have tried various solutions with
SetMapMode(hdc,MM_TEXT);,
SetViewportExtEx(hdc,0,-clientrect.bottom,NULL);
SetViewPortOrgEx(hdc,0,-clientrect.bottom,NULL);
SetWindowOrgEx(hdc,0,-clientrect.bottom,NULL);
SetWindowExtEx(hdc,0,-clientrect.bottom,NULL);
I have even tried google for a solution but to no prevail, so I turn to you the more experienced people on the internet.

The idea is I'm creating a custom control for linear interpolation and I could reverse the coordinate system by x,y in top right corner but I want it right. At the moment I get a reversed linear interpolation when I try to draw it as I cannot get the coords to be bottomleft.

I'm using win32 api, and I suspect I can skip the code as the screen coordinate system is almost identical on all systems, by that I mean 0,0 is "always" topleft on the screen if you are keeping it to standard 2d window and frames.

I really don't want a whole codesample to ease the typing pain for you guys, but I want some direction as it seems I cannot grasp the simple concept of flipping the coords in win32 api.

Thanks and a merry christmas

EDIT !

I would like to add my own answer to this question as I used simple math to reverse the view so to say.

If for an example I got the valuepair x,y (150,57) and another pair x,y (100,75) then I used this formulae height + (-1 * y) and voila I get a proper cartesian coordinate field :) ofcourse in this example height is undefined variable but in my application its 200px in height.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2024-12-29 12:29:11

根据 SetViewportOrgEx 的文档,您通常希望使用它或 SetWindowOrgEx,但不能同时使用两者。也就是说,您可能希望视口原点为 (0, clientrect.bottom),而不是 -clientrect.bottom

用 GDI 设置变换总是让我抓狂。我认为你最好使用 GDI+。使用它,您可以创建一个矩阵来描述 (0, clientRect.bottom) 的平移和 (1.0, -1.0) 的缩放。然后您可以调用SetWorldTransform

请参阅使用坐标空间和转换。有关变换的一般信息:坐标空间和变换< /a>.

附加信息:

我还没有尝试过直接 Windows API 调用,但如果我使用 Graphics 类(它是 GDI+ 的包装器)在 C# 中执行以下操作,它会起作用

Graphics g = GetGraphics();  // gets a canvas to draw on
SetTranslateTransform(0, clientRect.Bottom);
SetScaleTransform(1.0f, -1.0f);

:在左下角,x 向右增加,y 随着向上而增加。如果您按照我的建议使用 SetWorldTransform,以上内容将适合您。

如果您必须使用 GDI,那么您需要使用 SetViewportOrgEx(0, clientRect.bottom),然后设置缩放比例。我不记得如何使用旧的 GDI 函数进行缩放。

另请注意,SetViewportExtEx 文档说:

当设置以下映射模式时,调用SetWindowExtEx
和 SetViewportExtEx 函数将被忽略。

  • MM_HIENGLISH
  • MM_HIMETRIC
  • MM_LOENGLISH
  • MM_LOMETRIC
  • MM_TEXT
  • MM_TWIPS

According to the documentation for SetViewportOrgEx, you generally want to use it or SetWindowOrgEx, but not both. That said, you probably want the viewport origin to be (0, clientrect.bottom), not -clientrect.bottom.

Setting transforms with GDI always made me crazy. I think you're better off using GDI+. With it, you can create a matrix that describes a translation of (0, clientRect.bottom), and a scaling of (1.0, -1.0). Then you can call SetWorldTransform.

See the example at Using Coordinate Spaces and Transformations. For general information about transforms: Coordinate Spaces and Transformations.

Additional information:

I've not tried this with direct Windows API calls, but if I do the following in C# using the Graphics class (which is a wrapper around GDI+), it works:

Graphics g = GetGraphics();  // gets a canvas to draw on
SetTranslateTransform(0, clientRect.Bottom);
SetScaleTransform(1.0f, -1.0f);

That puts the origin at the bottom left, with x increasing to the right and y increasing as you go up. If you use SetWorldTransform as I suggested, the above will work for you.

If you have to use GDI, then you'll want to use SetViewportOrgEx(0, clientRect.bottom), and then set the scaling. I don't remember how to do scaling with the old GDI functions.

Note also that the documentation for SetViewportExtEx says:

When the following mapping modes are set, calls to the SetWindowExtEx
and SetViewportExtEx functions are ignored.

  • MM_HIENGLISH
  • MM_HIMETRIC
  • MM_LOENGLISH
  • MM_LOMETRIC
  • MM_TEXT
  • MM_TWIPS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文