.ScaleControl 的 WPF 版本?

发布于 2024-11-16 04:52:43 字数 1840 浏览 8 评论 0原文

Control 的 WPF 版本是什么.ScaleControl


我试图通过将字体设置为 IconTitleFont

private void ApplyUserFontPreferences()
{
   this.FontFamily = SystemFonts.IconFontFamily;
   this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

与WinForms不同,表单的内容不会随着字体变化而缩放:

Before
在此处输入图像描述

之后(不好)
在此处输入图像描述

实际上表单上的所有控件(包括按钮的大小、列表视图列的宽度等)应缩放以匹配新布局:

之后(好)
在此处输入图像描述

因为 WPF 没有 (与 WinForms 不同)响应字体大小更改,我打算通过尝试缩放来解决该问题我自己使用 WPF 形式,使用 的假设 WPF 版本ScaleControl

private void ApplyUserFontPreferences()
{
   Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize); //i.e. new / old
   this.ScaleControl(scaleFactor); //doesn't exist

   this.FontFamily = SystemFonts.IconFontFamily;
// this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

想要缩放控件(和所有子控件)的另一个示例是当我需要缩放控件(和所有子控件)以适合给定大小时。在这种情况下,我不想缩放整个表单,我只想缩放特定的控件。

What is the WPF version of Control.ScaleControl?


i am trying to honor the user's font preference by setting the font to the IconTitleFont:

private void ApplyUserFontPreferences()
{
   this.FontFamily = SystemFonts.IconFontFamily;
   this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

Unlike WinForms, the contents of the form are not scaled with the font change:

Before
enter image description here

After (bad)
enter image description here

In reality all controls on the form (including the size of buttons, the width of listview columns, etc) should scale to match the new layout:

After (good)
enter image description here

Since WPF doesn't (unlike WinForms) respond to font sizes changes, i was going to workaround the issue by trying to scale the WPF form myself, using a hypothetical WPF version of ScaleControl:

private void ApplyUserFontPreferences()
{
   Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize); //i.e. new / old
   this.ScaleControl(scaleFactor); //doesn't exist

   this.FontFamily = SystemFonts.IconFontFamily;
// this.FontSize = SystemFonts.IconFontSize;
   this.FontStyle = SystemFonts.IconFontStyle;
   this.FontWeight = SystemFonts.IconFontWeight;
}

Another example of wanting to scale a control (and all child controls) is when i need to scale a control (and all child controls) to fit in a given size. In this case i don't want to scale an entire form, i only want to scale a particular control.

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

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

发布评论

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

评论(2

懵少女 2024-11-23 04:52:43

这个解决方案怎么样

<Window
   x:Class="WpfApplication1.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   FontSize="40"
   Loaded="Window_Loaded"
   SizeToContent="WidthAndHeight"
   Title="MainWindow">

   <Grid x:Name="LayoutRoot" Width="525" Height="350">
      <Button Width="300" Height="60" Content="Hello world"/>
      <Grid.LayoutTransform>
         <ScaleTransform x:Name="scaleTransform"/>
      </Grid.LayoutTransform>
   </Grid>
</Window>

?在后面的代码中

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ApplyUserFontPreferences();
}

private void ApplyUserFontPreferences(){ 
    Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize);

    this.scaleTransform.ScaleX = scaleFactor;
    this.scaleTransform.ScaleY = scaleFactor;       

    this.FontFamily = SystemFonts.IconFontFamily; 
    this.FontStyle = SystemFonts.IconFontStyle;
    this.FontWeight = SystemFonts.IconFontWeight;
}

What about this solution

<Window
   x:Class="WpfApplication1.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   FontSize="40"
   Loaded="Window_Loaded"
   SizeToContent="WidthAndHeight"
   Title="MainWindow">

   <Grid x:Name="LayoutRoot" Width="525" Height="350">
      <Button Width="300" Height="60" Content="Hello world"/>
      <Grid.LayoutTransform>
         <ScaleTransform x:Name="scaleTransform"/>
      </Grid.LayoutTransform>
   </Grid>
</Window>

And in the code behind

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ApplyUserFontPreferences();
}

private void ApplyUserFontPreferences(){ 
    Double scaleFactor = (SystemFonts.IconFontSize / this.FontSize);

    this.scaleTransform.ScaleX = scaleFactor;
    this.scaleTransform.ScaleY = scaleFactor;       

    this.FontFamily = SystemFonts.IconFontFamily; 
    this.FontStyle = SystemFonts.IconFontStyle;
    this.FontWeight = SystemFonts.IconFontWeight;
}
戏剧牡丹亭 2024-11-23 04:52:43

我不确定它是否完全匹配您正在寻找的内容,但 WPF 确实包含自动缩放控件:Viewbox

这有点严厉,所以 YMMV。最终,您可能会发现您需要更精确的控制,因此您必须谨慎设计模板等。但是,Viewbox 将为您提供一些基本的缩放功能。

另请参阅:分辨率WPF 中的独立性

I'm not sure if it matches exactly what you are looking for, but WPF does include an automatic scaling control: Viewbox.

It's a bit heavy-handed, so YMMV. Eventually, you'll probably find that you want more precise control, so you'll have to instead take care in designing your templates and so forth. However, Viewbox will get you some basic scaling functionality.

See also: Resolution Independance in WPF

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