使用GetWindowRect在不同的DPI中的坐标错误 [英] Coordinate Error using GetWindowRect in different DPI

查看:1039
本文介绍了使用GetWindowRect在不同的DPI中的坐标错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获组件在我的MFC程序的坐标。

I want to capture the coordinate of components in my MFC program.

现在我可以使用 GetWindowRect
然而,当我将我的Windows dpi设置为150%(120 dpi)时,我得到 GetWindowRect 的不同坐标。

Now I can perfectly complete this by using GetWindowRect. However, when I set my windows dpi to 150% (120 dpi), I get different coordinates from GetWindowRect.

因此,我调查一些方法将新坐标转换为默认dpi(96 dpi)。

Therefore, I survey some method to convert new coordinates to that in default dpi (96 dpi).

最后,我发现有一些错误尝试:

Finally, I found there was some error when I tried:

Rect.top = Rect.top * (DEFAULT_DPIY / CURRENT_DPIY);
Rect.left = Rect.left * (DEFAULT_DPIX / CURRENT_DPIX);

转换的值非常接近,但不等于。

The converted value is very close, but not equal.

有没有任何方法可以无错误地转换?

Is there any method to convert it without error ?

推荐答案

正确的处理方式是让您的程序高DPI感知,但是这可能涉及更多的更改,比你准备尝试。

Your program is subject to DPI virtualization. The right way to deal with this is to make your program high DPI aware but that may well involve more changes than you are prepared to attempt.

如果是高DPI感知是不是你想要解决的东西,那么你至少可以让你的算术更好。您的代码使用整数除法。但这将是不准确的。为了尽量减少这种不准确,你应该在乘法后执行除法:

If being high DPI aware is not something you wish to tackle, then you can at least make your arithmetic better. Your code uses integer divide. But that's going to be inaccurate. In order to minimise that inaccuracy you should perform the division after the multiplication:

Rect.top = (Rect.top * DEFAULT_DPIY) / CURRENT_DPIY;
Rect.left = (Rect.left * DEFAULT_DPIX) / CURRENT_DPIX;

当然,括号可以在这里省略而不改变意思,但我认为很高兴

Of course, the parentheses could be omitted here without changing the meaning, but I think it's nice to be explicit about the ordering of the operations in this case.

另一种选择是使用 MulDiv

Another option would be to use MulDiv:

Rect.top = MulDiv(Rect.top, DEFAULT_DPIY, CURRENT_DPIY);
Rect.left = MulDiv(Rect.left, DEFAULT_DPIX, CURRENT_DPIX);

这篇关于使用GetWindowRect在不同的DPI中的坐标错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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