是否可以以与设备无关的真实世界单位显示 WPF 控件?例如,毫米? [英] Is it possible to display a WPF Control in real-world, device-independent units? Milimeters, for example?

查看:59
本文介绍了是否可以以与设备无关的真实世界单位显示 WPF 控件?例如,毫米?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 WPF 中设计产品模拟器.我们公司销售一种手持式电子医疗设备,我们希望通过虚拟"、皮肤(某种)GUI 来进行用户交互,以表示按钮和 LED.

I am designing a product emulator in WPF. Our company sells a handheld electronic medical device, and we'd like to experiment with user interaction with a "virtual", skinned (sort of) GUI to represent buttons and leds.

有没有办法强制"WPF 在屏幕上显示具有真实世界维度的内容(通过获取监视器 dpi 信息或类似的信息)?

Is there a way to "force" WPF to display stuff with its real-world dimension on the screen (by getting monitor dpi information, or something like this)?

再举一个例子,假设我可以使用 WPF 控件(DockPanels、Labels、Canvases 等)布局一张塑料标准尺寸的信用卡,我是否可以在不同的桌面上以真实信用卡的尺寸一致地显示它/笔记本电脑?

As another example, suppose I can layout a plastic, standard-sized Credit Card using WPF controls (DockPanels, Labels, Canvases, etc.), could I consistently display it with the dimensions of a real credit card, across different desktops/laptops?

更新:

通过使用 ViewBox 作为窗口的 LayoutRoot,我找到了一种以真实毫米"进行设计的巧妙方法.里面任何东西的大小都可以以毫米为单位设计",并将以物理大小显示,前提是我可以获得正确的 ViewBox 像素大小.这仍然是一个谜:

I've found a clever way to design in "real milimeters" by using a ViewBox as the LayoutRoot of my window. Size of anything inside can be "designed in milimeters", and will be displayed in physical size PROVIDED that I can get the right pixel size for the ViewBox. And THAT remain a certain mistery:

  • 我桌面上的像素和毫米之间的关系似乎是 3.5(这个数字从何而来?
  • 如何实现代码隐藏的 DataBound 属性或 ValueConverter,以便在需要时定义缩放级别(通过滑块或硬编码)以显示设备的缩放(更大)版本?

推荐答案

我用以下策略有点"解决了这个问题:

I "sort of" solved the problem with the following strategy:

  • 我的 MainWindow 有一个 ViweBox 作为 LayoutRoot,它的 Child 是一些容器控件(在本例中为 Border),代表物理对象;
  • ViewBox 及其内部的所有内容都被定义为 WPF 单位是毫米.因此,如果产品的物理宽度为 68mm,则容器控件的 Width 属性为 78.嵌套在内部的其他所有内容都被视为 WPF 单位是实际的毫米;
  • ViewBox 的宽度和高度使用乘以 (96/25.4) 的 ValueConverter 绑定到容器控件的高度和宽度;
  • 由于视图框内的所有内容都被缩放以适应(ViewBox.Stretch=Uniform),值转换器只是调整了视图框的大小,其余的布局重新调整是通过 ViewBox 嵌入式渲染变换实现的(我认为它足够高效).

我的 XAML:

<Window.Resources>
    <local:PixelsToMilimeters x:Key="SizeConverter"/>       
</Window.Resources>

<Viewbox x:Name="LayoutRoot" Stretch="Uniform"
    Width="{Binding Width, ElementName=Case, Converter={StaticResource SizeConverter}}"
    Height="{Binding Height, ElementName=Case, Converter={StaticResource SizeConverter}}">
    <Border x:Name="Case" Width="108" Height="68">
    (...)

我的代码隐藏:

    (...)
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (double)value * (96/25.4);
    }
    (...)

当然是我如何确定 DPI 实际上是 96?"未解决,但可以封装,最终结果与预期的结果足够接近,就我的目的而言.

Of course the "how can I be sure that DPI is actually 96?" is not solved, but that could be encapsulated, and the final result is close enough to the expected one, for my purposes.

这篇关于是否可以以与设备无关的真实世界单位显示 WPF 控件?例如,毫米?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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