如何在WPF中使用字体大小来缩放表单? [英] How to scale form with font size in WPF?
问题描述
如何在WPF中使用字体缩放表单?
什么是WPF相当于
this.Font = SystemFonts.IconTitleFont;
在WinForms ,如果你是一个很好的开发者,你会尊重用户的字体偏好。一个WinForm开始为:
然后应用用户的字体首选项:
this .Font = new Font(Segoe Print,11,GraphicsUnit.Point);
以及 scale 容纳新的大小:
$ b
注意:
注意:在WinForms中,也可以使用
this.Font = SystemFonts.IconTitleFont;
WPF不支持
Font
,为什么我提供了更清晰的选择。对于下面的例子。
一个类似的WPF表单开始为:
然后应用用户的字体首选项:
this.FontFamily = new FontFamily(Segoe Print);
this.FontSize = 14.666; // 11pt = 14.66
表单上的元素不会缩放以适应新的尺寸:
注意: 这是另一个例子两个大小相同的按钮: Windows Presentation Foundation
$ hr
$ b
$ b WinForms:
红利阅读
WPF不会做原始的基于字体的缩放,因为它是...原始的。您可以在自己的截图中看到它。
这是您的WinForms,更改字体之前截图。看看坐在日志之间有多少空间。以及表单的右边缘。
这里是您的WinForms,更改字体后截图。请注意,在缩放之后,您的保证金减少了多少。
如果你没有留下所有额外的空间,那么你的标签会被新的字体切断。而有了一些字体,即使你做了所有额外的空间,它也会被切断。当我说WinForms的缩放是原始的时,这就是我的意思。 WinForms选择一个单一的比例来适用于所有的东西,而且这个比例并不是选择了你的内容。它是基于字体的平均统计数字,一旦你开始谈论具体细节,它可能会崩溃。
WPF不会阻止你与原始的东西。它给你一个惊人的强大的布局系统,在那里做一个美观的窗户是微不足道的。但相反,您选择使用硬编码大小来削弱该布局系统。硬编码大小有两个巨大的问题:
$ ul
硬编码大小不适用任何东西。您必须在WinForms中使用它们,因为这是所有支持的WinForms。但WPF给了你一个适当的布局系统,所以你不必(也不应该)使用任何粗俗的东西。
所有你需要的是: p>
- A
窗口 with
SizeToContent =WidthAndHeight
。这样,无论使用哪种字体或语言,窗口的大小都适合文本和按钮。
- 由于您只有两个UI元素,一个是上面的另一方面,你可以在
Window
中放置一个StackPanel
。
StackPanel
,您需要:
- A
标签
或TextBlock
来显示您的文本,其中Content (
Label
)或
Text (
TextBlock
);和HorizontalAlignment =Right
和
- A
按钮
在内容
。
- A
- 设置一些
code> StackPanel
,TextBlock
和Button
将空间分配给你喜欢的东西。
- 由于您只有两个UI元素,一个是上面的另一方面,你可以在
就是这样。不要在任何东西上设置任何其他属性,特别是不是宽度
或者高度
。
现在,如果您更改了字体,窗口和按钮的大小仍然正确,并且不会切断文本。如果您将您的应用程序本地化为不同的语言,则窗口和按钮的尺寸将完全正确,并且不会切断您的文本。停止与WPF战斗,它会给你很好的结果。
如果你以后想让你的布局更高级,你可以考虑这样的事情:
- 如果您希望按钮更宽一些(在文本之前和之后有更多的呼吸空间),请尝试使用
填充
,或设置MinWidth
和MinHeight
。 (如果您的按钮包含文字,请勿使用宽度
或高度
。如果你的按钮只包含一个图片,也可以使用它们,但是甚至可能还没有。) - 如果你担心字体可能会使窗口太大以至于不再适合用户屏幕上,并希望启用单词包装,然后使用
MaxWidth
和TextWrapping
。
WPF的布局系统非常强大。学习它。不要使用硬编码的布局来抗争,然后抱怨你的硬编码布局太糟糕了。
How can i scale a Form with font in WPF?
i.e. What is the WPF equivalent of
this.Font = SystemFonts.IconTitleFont;
In WinForms, if you're a good developer, you honor the user's font preferences. A WinForm that starts out as:
You then apply the user's font preferences:
this.Font = new Font("Segoe Print", 11, GraphicsUnit.Point);
and elements on the form scale to accommodate the new size:
Notice:
- the form is wider and taller
- the label is positioned further down, and to the right
- the label is wider and taller
- the text of the label is not cut off on the right or on the bottom edge
- the button is wider and taller
- but button is positioned further down, and to the right
Note: In WinForms you can also use the line:
this.Font = SystemFonts.IconTitleFont;
WPF doesn't support
Font
, which is why i provided the clearer alternative. For the example below.
A similar WPF form starts out as:
You then apply the user's font preferences with:
this.FontFamily = new FontFamily("Segoe Print");
this.FontSize = 14.666; //11pt = 14.66
and elements on the form don't scale to accommodate the new size:
Notice:
- the label's position has not changed
- the button's position has not changed
- the form is not wider or taller (text is cut off)
- the label is not any wider (text is cut off on the right)
- the label is not any taller (cutting off text along the bottom edge)
- the button is not any wider (text is cut off)
Here is another example of two buttons that are the same size:
WinForms:
Windows Presentation Foundation:
Bonus Reading
- WPF: How to specify units in Dialog Units?
- How to prevent WPF from scaling with the Windows font size options?
- WPF version of .ScaleControl?
WPF doesn't do primitive font-based scaling because it's... well, primitive. You can see it in your own screenshots.
Here's your "WinForms, before changing font" screenshot. Take a look at how much space there is between "sat on a log." and the right edge of the form.
And here's your "WinForms, after changing font" screenshot. Notice how much less margin you have after "scaling".
If you hadn't left all that extra space, then your label would be cut off with the new font. And with some fonts, it would be cut off even though you did leave all that extra space. That's what I mean when I say WinForms' scaling is "primitive". WinForms picks a single scale to apply to everything, and that scale is not chosen with any awareness of your content; it's based on average statistics for the font, which can and will fall apart once you start talking about specifics.
WPF doesn't hobble you with something that primitive. It gives you an amazingly powerful layout system, where it would be trivial to make a window that scales beautifully. But instead, you're choosing to cripple that layout system by using hard-coded sizes. Stop it.
Hard-coded sizes have two huge problems:
- They don't adapt to different fonts. You've noticed this already.
- They don't adapt to different content. (What happens when you want to make a German version of your app, and the German text doesn't fit into your hard-coded button size?)
Hard-coded sizes just don't adapt. To anything. You had to use them in WinForms because that's all WinForms supported. But WPF gives you a proper layout system, so you don't have to (and shouldn't) use anything that crude.
All you need is this:
- A
Window
withSizeToContent="WidthAndHeight"
. That way, the window will be exactly the right size to accommodate the text and button, no matter what font or language you use. - Since you only have two UI elements, and one is above the other, you would put a
StackPanel
inside yourWindow
. - Inside the
StackPanel
, you need:- A
Label
orTextBlock
to show your text, with the text inContent
(Label
) orText
(TextBlock
); and - A
Button
withHorizontalAlignment="Right"
, and the text inContent
.
- A
- Set some
Margin
s on theStackPanel
,TextBlock
, andButton
to space things out to your liking.
That's it. Don't set any other properties on anything -- especially not Width
or Height
.
Now, if you change your font, the window and the button will still be exactly the right size, and won't cut off your text. If you localize your app into a different language, the window and the button will be exactly the right size, and won't cut off your text. Stop fighting WPF, and it will give you great results.
If you later want to make your layout more advanced, you could consider things like:
- If you want the button to be a little wider (to have more breathing room before and after the text), try playing with the
Padding
, or set aMinWidth
andMinHeight
. (Don't useWidth
orHeight
if your button contains text. You might consider using them if your button only contains an image, but maybe not even then.) - If you're worried that the font might make the window so large that it no longer fits on the user's screen, and want to enable word-wrapping, then play around with
MaxWidth
andTextWrapping
.
WPF's layout system is amazingly powerful. Learn it. Don't fight it by using hard-coded layouts and then complaining that your hard-coded layouts suck.
这篇关于如何在WPF中使用字体大小来缩放表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!