WinForms ToolStrip change font size automatically - winforms

I have a ToolStrip control docked to the top of a Windows Form. Throughout the program, I have the font sizes on controls not specified so that they inherit from the base form. That way I can set the font size larger when the user selects "Large Font Mode" and it automatically changes throughout. The ToolStrip seems completely unaffected by this and sticks to the default size.
Is there a way have the ToolStrip inherit the base font size? Or do I need to write something to go through and find all the ToolStrips and update their .Font property?
Picture for reference:
Little update - what led me down this road is that sales people got touch-capable 1080p 10" tablets running Win8. Before making any adjustments to the display settings, the ToolStrip was an impressive 3mm tall, very difficult for anyone with large hands to hit with any accuracy. The rest of the form was difficult to interact with too. Apple has suggested 7mm for touch targets, and even Microsoft suggests 9mm. Changing the settings in Control Panel > Display up to 160% resulted in touch targets of about 6.5mm, good enough to hit without making the rest of the form too big.

Related

Why does the default font for a WinForm only change when display settings is set to "Medium (125%)?

When the Display Settings is set to either Small or Large the default font for a WinForm is set to Microsoft Sans Serif, 8.25pt. And everything on the form scales properly. However, when it is set to Medium it changes the default font to Microsoft Sans Serif, 7.8pt, which causes various breaking issues on the form.
Is there a specific reason why the font's size only changes on the Medium setting?
Yes, by default if you go past Medium (anything more than 125%) then Windows starts to help and emulates a video adapter set at 96 dots per inch. Same as Small. This works by Windows letting the program actually draw into an in-memory bitmap and rescaling the bitmap before blitting it to the screen. This is an appcompat feature and the result is in general not considered pretty, particularly text becomes 'fuzzy'. It does however help keep the program usable on very high resolution screens and prevents the main window from having the size of a postage stamp. Disabling this feature is the subject of this answer, otherwise the exact opposite of what you are looking for.
It works this way because 125% scaling has been around for a long time already, at least as far back as 2001 with the XP release. So programs are expected to know how to deal with it. Enabling automatic DPI scalling at 125% is not an option.

How to scale form with font size in 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 with SizeToContent="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 your Window.
Inside the StackPanel, you need:
A Label or TextBlock to show your text, with the text in Content (Label) or Text (TextBlock); and
A Button with HorizontalAlignment="Right", and the text in Content.
Set some Margins on the StackPanel, TextBlock, and Button 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 a MinWidth and MinHeight. (Don't use Width or Height 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 and TextWrapping.
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.

How do I remove the border of a WPF window in the Design view/tab?

I am trying to remove the border of my WPF window in the design view/tab in Visual Studio. Please don't mistake this as a request to create a border less WPF window. I did that and it is working fine. What bothers me is that even if you have set WindowStyle = None, ResizeMode = NoResize, the design view/tab still shows a border around your window in the preview.
Is there a way to remove said border and have a 1:1 preview of the border less window as in Windows Forms?
Every question I have found in regards to this only asks how to remove the border of the actual application. I would like to remove it in the preview.
Any help would be very much appreciated :)
here is a screenshot of my problem:
This cannot be done as this is just how Visual Studio renders a window in design view (I think the frame is probably there so that you can distinguish when you are editing a Window rather than a UserControl).
Rather than try and find a solution to this I would ask myself if this is something I need to be spending time figuring out - after all you say that your program works correctly when being run. I think your time will be better spent writing code for your program rather than trying to play with the design time environment.
Update: In response to you comment, consider that the window frame will be different on every users machine depending on their operating system version (XP vs. Win7) or the theme the user has installed.
My computer has XP installed so the side borders are a lot thinner than those shown in design time so any content will be smaller (but only my a few pixels - 4 in my case; does your user interface design really depend on 4 pixels?).
When using a technology such as WPF you should not be designing your UI to fit to exact pixel sizes; you should be designing with min / max values or using layout containers that adjust to the size of the window as set by the user. Any regions in your UI (E.G. sidebar and main content) should be expressed as a ratio or percentage of one another; instead of saying "The side bar is 150 pixels wide and the main content area is 350 pixels wide" you should be saying "The side bar takes up a third of the window width and the main content takes two thirds".
Although the question is very old and have already been answered (kind of), I just realized: if you set WindowStyle="None", your undesired border is gone.

How can I re-size controls based on resolution?

Inside a WinForms application what is the best way to handle re-sizing controls to match based on screen resolution and maximizing and re-sizing the window. I have 3 columns that are set like this. LABEL TEXTBOX in each column. You could count that as 6 columns. I have tried anchoring to the right but the problem there is the textbox in column 1will overlap the next 2 columns. I have also tried docking but that does not seem to do the trick. Is there any easy way of doing this?
TL;DR: You can't without manually coding resizing logic. As a side note, you probably shouldn't be trying to do this.
In traditional WinForms there is no automatic way to my knowledge without rolling your own solution. Sean87 suggested the AutoScaleMode property, but while it is then automatic it doesn't support auto-sizing by resolution. It gives an option for scaling based on Font size settings in Windows or the DPI setting. Neither of these are directly changed when simply changing the screen resolution and, honestly, most typical users (and even probably power users) probably never modify the DPI or font size settings.
Besides, the whole point, from a basic user perspective, of increasing screen resolution is to give more virtual desktop "space" to place application windows in. True, most monitors are LCD-based now and thus have native resolutions that make changing this around mostly a thing of the CRT past. But still, the act of buying a new, larger monitor would let a typical user increase this virtual screen space so that they could see more windows on the screen simultaneously. An application that always maintains the same relative size to the screen resolution would be quite uncommon I think and goes against the expectations of most users, including even us advanced developer users.
Typically, you are to code your application to adjust its contents intelligently and usefully if the window is resized, but generally you defer to the user for managing that as it is their machine and their preference (think of it as the "my house, my rules" principle). This is also why most (all?) applications don't change window positions except when directly manipulated by the user (via dragging the window frame and such). In fact, all of this likely explains why Microsoft chose not to include any automatic sizing rule based on resolution.
You don't define what you mean by "column". Generally, you should use a TableLayoutPanel and inside each "cell", set the anchors of the controls.
you can change AutoScaleMode of the form and also auto size property of each control. And you might use splitcontainer or panels to form your columns.

WinForms Control position in Simplified Chinese

We have a WinForms application that includes controls such as picture boxes that are positioned on a form. The base application is in English.
We've translated this application to a number of different languages (French, Spanish, Danish, Greek, etc.) and most recently to Simplified Chinese. The translated application works perfectly on our operation systems (English).
One of our customers installed the application on their operation system, Windows XP in Simplified Chinese. The layout of our application is broken. Simply put, the elements are pushed to the bottom right by a factor that is proportional to the distance between the element and the top left corner. For example, an element at the top right corner in design view is pushed off screen to the right whereas the items at the bottom of the page are pushed downwards and to the right.
The application supports switching languages while in use. When the locale is en-US, there are no layout issues. When switching to Simplified Chinese, the issue appears, but only on the Simplified Chinese operating system. The screen resolution and DPI are the same.
Do you have any ideas? I'm sure it must be a simple configuration setting somewhere, but I have been unable to solve this issue.
The size of the system base font matters as well. Which is indeed something you can change on XP. This will invoke the form's auto-scaling logic, designed to ensure that the controls grow larger to fit the larger font size.
This is by design, controlled by the form's AutoScaleMode property. Don't change it, rescaling is important. Just make sure the form layout still looks good, use properties like Anchor and Dock, controls like TableLayoutPanel, FlowLayoutPanel. Or the Resize event for tricky ones.
Paste this into your form to test this logic without having to change system settings:
protected override void OnLoad(EventArgs e) {
this.Font = new Font(this.Font.FontFamily, this.Font.SizeInPoints * 125 / 96);
}

Resources