How to scale form with font size in WPF?

How can I scale a font form in WPF?

i.e. What is the WPF equivalent?

this.Font = SystemFonts.IconTitleFont; 

In WinForms, if you are a good developer, you follow the user's font preferences. WinForm starting as:

enter image description here

Then custom font settings are applied:

 this.Font = new Font("Segoe Print", 11, GraphicsUnit.Point); 

and scale- shaped elements to accommodate the new size:

enter image description here

Note:

  • shape wider and higher
  • the label is further down and to the right
  • label wider and higher
  • label text is not clipped to the right or bottom.
  • wider and taller button
  • but the button is further down and to the right

Note In WinForms, you can also use the line:

 this.Font = SystemFonts.IconTitleFont; 

WPF does not support Font , so I gave a clearer alternative. An example is below.

A similar form of WPF starts as:

enter image description here

Then you apply custom font settings with:

  this.FontFamily = new FontFamily("Segoe Print"); this.FontSize = 14.666; //11pt = 14.66 

and the elements in the form are not scaled to accommodate the new size:

enter image description here

Note:

  • tag position has not changed
  • button position has not changed
  • the shape is not wider or higher (the text is cropped)
  • the label is not wider (text is trimmed to the right)
  • Label not higher (cutting off text along the bottom edge)
  • the button is not wider (text disabled)

Here is another example of two buttons of the same size :

Winforms:

enter image description here

Windows Presentation Foundation:

enter image description here

Reading bonuses

+6
source share
1 answer

WPF does not do primitive font-based scaling because it is ... well, primitive. You can see this in your own screenshots.

Here is your screenshot of "WinForms before changing the font." See how much space is between "sitting on the log". and the right edge of the form.

And here is your screenshot of "WinForms, after changing the font." Notice how much less you get after scaling.

If you had not left too much space, your label would have been cut off with a new font. And with some fonts, this will be cut off even if you leave all the extra space. This is what I mean when I say that scaling WinForms is "primitive." WinForms selects a single scale to apply to everything, and this scale is not selected with awareness of your content; it is based on average font statistics, which can and will fall apart after you start talking about specifics.

WPF doesn't bother you with something primitive. This gives you an amazingly powerful layout system where it would be trivial to create a window that scales perfectly. But instead, you choose to cripple this layout system using hard-coded sizes. Stop him.

Hard-coded sizes have two huge problems:

  • They do not adapt to different fonts. You have already noticed this.
  • They do not adapt to other content. (What happens when you want to make a German version of your application, and the German text does not fit your hard-coded button size?)

Hard-coded sizes simply do not adapt. For anything. You should have used them in WinForms because all WinForms were supported. But WPF gives you the right layout system, so you don't need (and shouldn't) use anything that's rude.

All you need is:

  • A Window with SizeToContent="WidthAndHeight" . Thus, the window will exactly match the size of the text and button, regardless of which font or language you use.
  • Since you have only two interface elements, and one on top of the other, you must put a StackPanel inside your Window .
  • Inside the StackPanel you need:
    • A Label or TextBlock to display text with text Content ( Label ) or Text ( TextBlock ); and
    • A Button with HorizontalAlignment="Right" , and the text in Content .
  • Set < Margin to StackPanel , TextBlock and Button to touch things to your liking.

What is it. Do not set any other properties on anything, especially not Width or Height .

Now, if you change the font, the window and button will still be the right size and will not cut your text. If you localize your application in another language, the window and button will exactly fit the size and will not cut your text. Stop struggling with WPF and it will give you great results.

If you later want to make your layout more advanced, you can consider things like:

  • If you want the button to be a little wider (to have more breathing space before and after the text), try playing with Padding or set MinWidth and MinHeight . (Do not use Width or Height if your button contains text. You might want to use them if your button contains only an image, but maybe not even then.)
  • If you are worried that the font may make the window so large that it no longer fits the user’s screen and you want to enable word wrap, then play with MaxWidth and TextWrapping .

The WPF layout system is surprisingly powerful. Get to know him. Do not fight it using hard-coded layouts, and then complain that your hard-coded layouts suck.

+11
source

Source: https://habr.com/ru/post/909945/


All Articles