如何将 WPF 大小转换为物理像素?
将 WPF(与分辨率无关)宽度和高度转换为物理屏幕像素的最佳方法是什么?
我正在 WinForms 表单中显示 WPF 内容(通过 ElementHost)并尝试制定一些大小调整逻辑。当操作系统以默认 96 dpi 运行时,我可以正常工作。但当操作系统设置为 120 dpi 或其他分辨率时,它将不起作用,因为就 WinForms 而言,报告其宽度为 96 的 WPF 元素实际上将是 120 像素宽。
我在 System.Windows.SystemParameters 上找不到任何“每英寸像素”设置。我确信我可以使用 WinForms 等效项(System.Windows.Forms.SystemInformation),但是有没有更好的方法来做到这一点(阅读:使用 WPF API 的方法,而不是使用 WinForms API 并手动进行数学计算)?将 WPF“像素”转换为真实屏幕像素的“最佳方法”是什么?
编辑:我还希望在 WPF 控件显示在屏幕上之前执行此操作。看起来 Visual.PointToScreen 可以给我正确的答案,但我无法使用它,因为该控件尚未成为父级,并且我收到 InvalidOperationException“此 Visual 未连接到PresentationSource”。
What's the best way to convert a WPF (resolution-independent) width and height to physical screen pixels?
I'm showing WPF content in a WinForms Form (via ElementHost) and trying to work out some sizing logic. I've got it working fine when the OS is running at the default 96 dpi. But it won't work when the OS is set to 120 dpi or some other resolution, because then a WPF element that reports its Width as 96 will actually be 120 pixels wide as far as WinForms is concerned.
I couldn't find any "pixels per inch" settings on System.Windows.SystemParameters. I'm sure I could use the WinForms equivalent (System.Windows.Forms.SystemInformation), but is there a better way to do this (read: a way using WPF APIs, rather than using WinForms APIs and manually doing the math)? What's the "best way" to convert WPF "pixels" to real screen pixels?
EDIT: I'm also looking to do this before the WPF control is shown on the screen. It looks like Visual.PointToScreen could be made to give me the right answer, but I can't use it, because the control isn't parented yet and I get InvalidOperationException "This Visual is not connected to a PresentationSource".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将已知大小转换为设备像素
如果您的视觉元素已附加到PresentationSource(例如,它是屏幕上可见的窗口的一部分),则可以通过以下方式找到转换:
如果没有,使用 HwndSource 创建临时 hWnd:
请注意,这比使用 IntPtr.Zero 的 hWnd 构造效率低,但我认为它更可靠,因为 HwndSource 创建的 hWnd 将与实际新创建的 hWnd 附加到同一显示设备窗户会。这样,如果不同的显示设备具有不同的 DPI,您一定会获得正确的 DPI 值。
完成转换后,您可以将任何大小从 WPF 大小转换为像素大小:
将像素大小转换为整数
如果要将像素大小转换为整数,您可以简单地执行以下操作:
但是ElementHost 使用的解决方案更强大:
获取 UIElement 的所需大小
要获取 UIElement 的所需大小,您需要确保对其进行测量。在某些情况下,它已经被测量了,要么是因为:
如果尚未测量您的视觉元素,则应根据需要对控件或其祖先之一调用 Measure,并传入可用大小(或
new Size(double.PositivieInfinity, double.PositiveInfinity)
如果你想调整内容的大小:一旦测量完成,所需要的就是使用矩阵来转换 DesiredSize:
将它们放在一起
这是一个简单的方法,展示了如何获得元素的像素大小:
请注意,在此代码中,仅当不存在 DesiredSize 时,我才调用 Measure。 这提供了一种执行所有操作的便捷方法,但有几个缺陷:
由于这些原因,我倾向于省略 GetElementPixelSize 中的 Measure 调用,而让客户端执行此操作。
Transforming a known size to device pixels
If your visual element is already attached to a PresentationSource (for example, it is part of a window that is visible on screen), the transform is found this way:
If not, use HwndSource to create a temporary hWnd:
Note that this is less efficient than constructing using a hWnd of IntPtr.Zero but I consider it more reliable because the hWnd created by HwndSource will be attached to the same display device as an actual newly-created Window would. That way, if different display devices have different DPIs you are sure to get the right DPI value.
Once you have the transform, you can convert any size from a WPF size to a pixel size:
Converting the pixel size to integers
If you want to convert the pixel size to integers, you can simply do:
but a more robust solution would be the one used by ElementHost:
Getting the desired size of a UIElement
To get the desired size of a UIElement you need to make sure it is measured. In some circumstances it will already be measured, either because:
If your visual element has not been measured yet, you should call Measure on the control or one of its ancestors as appropriate, passing in the available size (or
new Size(double.PositivieInfinity, double.PositiveInfinity)
if you want to size to content:Once the measuring is done, all that is necessary is to use the matrix to transform the DesiredSize:
Putting it all together
Here is a simple method that shows how to get the pixel size of an element:
Note that in this code I call Measure only if no DesiredSize is present. This provides a convenient method to do everything but has several deficiencies:
Because of these reasons, I would be inclined to omit the Measure call in GetElementPixelSize and just let the client do it.
Screen.WorkingArea 和之间的简单比例SystemParameters.WorkArea:
这适用于多个监视器也是如此。
Simple proportion between Screen.WorkingArea and SystemParameters.WorkArea:
This works fine with multiple monitors as well.
我找到了一种方法来做到这一点,但我不太喜欢它:
我不喜欢它,因为 (a) 它需要引用 System.Drawing,而不是使用 WPF API; (b) 我必须自己进行数学计算,这意味着我要复制 WPF 的实现细节。在 .NET 3.5 中,我必须截断计算结果以匹配 ElementHost 在 AutoSize=true 时所做的操作,但我不知道这在 .NET 的未来版本中是否仍然准确。
这似乎确实有效,所以我将其发布,以防对其他人有帮助。但如果有人有更好的答案,请留言。
I found a way to do it, but I don't like it much:
I don't like it because (a) it requires a reference to System.Drawing, rather than using WPF APIs; and (b) I have to do the math myself, which means I'm duplicating WPF's implementation details. In .NET 3.5, I have to truncate the result of the calculation to match what ElementHost does with AutoSize=true, but I don't know whether this will still be accurate in future versions of .NET.
This does seem to work, so I'm posting it in case it helps others. But if anyone has a better answer, please, post away.
刚刚在ObjectBrowser中进行了快速查找,发现了一些非常有趣的内容,您可能想查看一下。
System.Windows.Form.AutoScaleMode,它有一个名为DPI的属性。这是文档,它可能就是您正在寻找的内容:
将其应用到您的表单中,它应该可以解决问题。
{享受}
Just did a quick lookup in the ObjectBrowser and found something quite interesting, you might want to check it out.
System.Windows.Form.AutoScaleMode, it has a property called DPI. Here's the docs, it might be what you are looking for :
Apply that to your form, it should do the trick.
{enjoy}