如何在WPF中使用不同的坐标系? (仅缩放)

发布于 2024-07-13 00:48:47 字数 485 浏览 8 评论 0原文

(让我给你一些背景) 我目前正在设计一个应用程序,该应用程序应该根据一些数据生成可打印的 A4 页面。 当然,WPF 的设备独立像素(96 像素/英寸)并不是纸张世界中非常自然的测量单位。 像毫米这样的数字会更合适。 于是我拿出计算器得出了大约 3.779 的比例因子。

事实证明,简单地将页面上应该出现的所有内容都放在 ScaleTransform 中会产生一个令人讨厌的副作用:字体大小也会缩放(自然地)。 然而,这并不是我的本意。 我希望 12pt Arial 能够像 12pt Arial 正常渲染一样渲染。

是否有其他方法可以更改坐标系而无需调用扩展或任何其他方法来转换每个坐标、长度、厚度等? - 或者 - 有什么方法可以动态映射字体大小,即 DependencyProperties? 也许是通过一个包含所有纸张内容的自定义控件?

(Let me give you some context)
I am currently designing an application that is supposed to generate a printable A4 page based on some data.
Naturally, the device independent pixels of WPF (96 pixels/inch) are not a very natural unit of measurement in the paper world. Something like millimetres would be more appropriate. So I got my calculator out and arrived at a scaling factor of something around 3.779.

It turns out that simply slapping everything that's supposed to go on the page in a ScaleTransform has one nasty side effect: Font sizes are scaled too (naturally). This, however, is not what I intended. I would like 12pt Arial to render like 12pt Arial would render normally.

Is there any other way to change the coordinate system without having to call into extensions or whatever to convert each and every coordinate, length, thickness and so on?
- or -
Is there any way to map font sizes on-the-fly, being DependencyProperties? Via a custom control that wraps all the paper content, maybe?

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

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

发布评论

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

评论(3

纵山崖 2024-07-20 00:48:47

对于概述的要求,您根本不需要做任何特殊的事情,只需继续使用厘米作为 WPF 元素本身的测量单位(即无需任何转换) - WPF 设备独立性的本质允许您执行以下操作:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="29.7cm" Width="21cm" FontSize="16pt">
    <Grid>
        <TextBlock Text="Sample" Height="1in" Width="1in" FontSize="12pt" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            TextAlignment="Center"/>
    </Grid>
</Window>

也就是说:您将获得一个在“cm”中指定的 A4 窗口,其中包含在“in”中指定的居中方形文本框以及在“pt”中指定的字体'。 如果需要的话,所有这些都将通过您可能额外应用的任何变换来正确缩放(例如,通过用户视口的缩放滑块),尊重它们的相对大小,无论每个都指定不同的单位(即随意混合使用)。

可用单位为 px(默认)、incmpt,请参见示例 FrameworkElement.Height 了解有关其规范的详细信息。

For the outlined requirements you do not need to do anything special at all, just go ahead and use centimeters as unit of measurement for the WPF elements themselves (i.e. without any transform) - the very nature of WPF device independence allows you to to the following:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="29.7cm" Width="21cm" FontSize="16pt">
    <Grid>
        <TextBlock Text="Sample" Height="1in" Width="1in" FontSize="12pt" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            TextAlignment="Center"/>
    </Grid>
</Window>

That is: you'll get a A4 window specified in 'cm' with a centered square TextBox specified in 'in' and a font specified in 'pt'. All these will scale properly by whatever transform you might apply additionally, if need be (e.g. via a zoom slider for the users view port), respecting their relative sizes regardless of being specified with different units each (i.e. mixed usage at will).

Available units are px (default), in, cm and pt, see for example FrameworkElement.Height for details on their specification.

若无相欠,怎会相见 2024-07-20 00:48:47

您还可以以磅 (FontSize="10pt")、英寸 (FontSize="10in") 或厘米 (FontSize="10cm") 为单位设置大小。 当然,在这种情况下,实际尺寸取决于Windows的DPI设置以及显示器或打印机的DPI。

You can also set sizes in points (FontSize="10pt"), in inches (FontSize="10in") or in centimeters (FontSize="10cm"). Of course the real size depends on the DPI setting of Windows and the DPI of your monitor or printer in this case.

动听の歌 2024-07-20 00:48:47

只需将字体大小设置即可12 / 3.779 = 3.175,不是吗? 将它分配给包含对象,它应该渗透到所有子对象。

Just set the font size to 12 / 3.779 = 3.175, no? Assign it to the containing object and it should trickle down to all children.

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