无需重新启动应用程序即可处理主 DPI 的更改 [英] Handling change of primary DPI without restarting the app

查看:53
本文介绍了无需重新启动应用程序即可处理主 DPI 的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 项目,它根据 APP 的某些元素的位置在不同的应用程序APP"上绘制窗口(如弹出窗口).
这些窗口的位置是根据系统 DPI(又名主显示器的 DPI)计算的.
当我在显示设置中更改主 DPI 时,窗口的位置是使用旧"主 DPI 计算的,这会导致位置错误.
有什么方法可以执行这些计算,这取决于主要 DPI,使用新"主要 DPI 而不是使用旧的?

I have a WPF project which draws windows (like popups) over a different app 'APP', according to some elements' position of APP.
Those windows' position is calculated according to the system DPI (aka primary monitor's DPI).
When I change the primary DPI in the display settings, the windows' position is calculated using the 'old' primary DPI, which results in wrong location.
Is there some way to perform these calculations, which depend on the primary DPI, using the 'new' primary DPI and not using the old one?

我知道在更改主 DPI 时,Windows 会提醒我某些应用程序不会响应缩放更改,直到您关闭并重新打开它们."但我需要找到解决方法.

I know that when changing the primary DPI, windows alerts me that "Some app won't respond to scaling changes until you close and reopen them.", but I need to find a way around it.

谢谢

推荐答案

您需要查看 SystemEvents.DisplaySettingsChanged 事件,并编写一个处理该事件的方法.我以前在 WPF 中做过这个,所以我知道它有效.

You'll want to look into the SystemEvents.DisplaySettingsChanged event, and write a method that handles that event when it happens. I've done this before in WPF, so I know that it works.

然后,使用 Ana Betts 的答案计算主要 DPI:

Then, use the answer here by Ana Betts to calculate the primary DPI:

PresentationSource source = PresentationSource.FromVisual(this);

double dpiX, dpiY;
if (source != null) {
    dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
    dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
}

这篇关于无需重新启动应用程序即可处理主 DPI 的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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