Author Topic: Font rendering in labels on Windows Forms  (Read 3760 times)

0 Members and 1 Guest are viewing this topic.

huiz

  • Swamp Rat
  • Posts: 895
  • Certified Prof C3D
Font rendering in labels on Windows Forms
« on: July 29, 2014, 05:16:48 AM »
Below a screenshot of a dialog, slightly zoomed in. On the left the dialog in Visual Studio, on the right running in AutoCAD.

The font on the dialog and buttons are correctly rendered and conform the preview in Visual Studio but all other controls look different. In Windows I have ClearType on, but it looks like labels do not use this setting, other controls do.

Why is that and is there some option to change this?
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #1 on: July 30, 2014, 05:03:05 AM »
In the settings for each control (Labels in your case) try setting the FlatStyle property to System, it is not the default. I manually set my font to Segoe UI, 8pt in the project I'm working on at the moment, mostly to annoy any XP adherents. Segoe UI isn't in any systems pre-Vista so there is another way ... http://wyday.com/blog/2009/windows-vista-7-font-segoe-ui-in-windows-forms/ The (perhaps) important takeaway from that post is ... Font = SystemFonts.MessageBoxFont; ... in context ...

Code - C#: [Select]
  1. public Form1()
  2. {
  3.     // use Segoe UI in Vista & 7
  4.     Font = SystemFonts.MessageBoxFont;
  5.  
  6.     InitializeComponent();
  7. }

Just for fun, the font change will mess with text spacing and layout.

huiz

  • Swamp Rat
  • Posts: 895
  • Certified Prof C3D
Re: Font rendering in labels on Windows Forms
« Reply #2 on: July 30, 2014, 05:47:01 AM »
Flatstyle set to System is doing the trick, thanks!

But unfortunately I get another problem... The alignment of the text.  :(


The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #3 on: July 30, 2014, 08:06:27 AM »
But unfortunately I get another problem... The alignment of the text.  :(
Just for fun, the font change will mess with text spacing and layout.
Um, yeah, it breaks. After I discovered the Flatstyle thing I had to shuffle everything in my UI around a bit. Settle on a Font size first - I compared my window' fonts etc. to the Properties Palette in AutoCAD ie. I NETLOADed it and ran it.

You could perhaps set the default font size in the constructor after the Font = SystemFonts.MessageBoxFont; line. I selected all the controls and set their size in the Visual Studio GUI Properties palette. You could hand-edit the .Designer.cs file if you really feel like breaking it. It is about then that I thank $Deity for source control.

huiz

  • Swamp Rat
  • Posts: 895
  • Certified Prof C3D
Re: Font rendering in labels on Windows Forms
« Reply #4 on: July 30, 2014, 08:39:27 AM »
I didn't use the Font setting in the constructor, I don't have XP users and I have set Segoe UI in the Form Font, so it would not affect anything.

For Labels where I do not set the text alignment the flatstyle setting works great! Searching the internet gave me solutions like painting the text but that takes a lot of extra code to do, so just a setting is much easier. Thanks for that tip :-)

I already found other weird behaviors, like setting a label to white background color and disable it, then the forcolor becomes blue, whatever your color setting is.

Maybe it's time to seriously dive into WPF...
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #5 on: August 02, 2014, 07:06:31 AM »
I didn't use the Font setting in the constructor...
Me neither, it totally messed up the layout.

Maybe it's time to seriously dive into WPF...
I don't like to rush into these newfangled things ... ;) Yeah, next time. My current project suits the winforms look and feel... Right up until I change my mind.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Font rendering in labels on Windows Forms
« Reply #6 on: August 04, 2014, 04:20:29 PM »
I don't like to rush into these newfangled things ... ;) Yeah, next time. My current project suits the winforms look and feel... Right up until I change my mind.

There is no look and feel that you get in WinForms that you cannot get better in WPF.  Write one WPF application,  I suggest going ahead and adopting MVVM at the same time if you're not using it,  and you'll never use WinForms again.
Revit 2019, AMEP 2019 64bit Win 10

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #7 on: August 04, 2014, 05:30:11 PM »
There is no look and feel that you get in WinForms that you cannot get better in WPF.  Write one WPF application,  I suggest going ahead and adopting MVVM at the same time if you're not using it,  and you'll never use WinForms again.

Yes, you are right, of course, and I am mocking myself here. I have taken the path of least-resistance on this project, mostly because it was the easiest way to prototype a working app (I use Lucidchart for my initial prototypes) but you will be happy to know it's pretty-much MVVM anyway -  the Winforms part is just the view, all it does is despatch commands with a bunch of parameters and display the results. Replacing it with WPF won't be particularly hard.

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #8 on: January 20, 2015, 04:04:07 PM »
There is no look and feel that you get in WinForms that you cannot get better in WPF.  Write one WPF application,  I suggest going ahead and adopting MVVM at the same time if you're not using it,  and you'll never use WinForms again.
Replacing it with WPF won't be particularly hard.
So I did. Well, I'm still in the process of doing so. I wrote Windows.Forms extension method that goes through all your controls on a Windows form and vomits out some XAML that you can edit and get back to where you started. It's still a bit rough but it's at https://gist.github.com/CADbloke/0a3121af11c1d46f34c9

MVVM, here I come.
« Last Edit: January 20, 2015, 10:00:51 PM by CADbloke »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Font rendering in labels on Windows Forms
« Reply #9 on: January 21, 2015, 07:57:04 AM »
At a glance, if this is for more than converting this one project then your default container should be a Grid not a StackPanel. It's a neat idea but if you already have the look of the UI from your windows form.  Then you can probably scratch this out manually in less time than it took to create this utility. Nice work though.
Revit 2019, AMEP 2019 64bit Win 10

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #10 on: January 21, 2015, 03:27:44 PM »
At a glance, if this is for more than converting this one project then your default container should be a Grid not a StackPanel. It's a neat idea but if you already have the look of the UI from your windows form.  Then you can probably scratch this out manually in less time than it took to create this utility. Nice work though.

Changing container types is an easy right-click in Blend so I wasn't overly concerned about the actual type. Also, a Grid needs row & columns defined and figured out which would involve figuring out exactly where the controls lie on the Windows.Form - boring! In a StackPanel or similar you can just drag them around in the Tree in Blend. You should have seen the mess when I tried using Canvas and placing everything absolutely. Don't go there.

The utility was a good exercise in why Windows.Forms doesn't translate directly into WPF. HTML + CSS is a close analogy.

For bonus point I managed to get Caliburn.Micro working in a Palette control. It works but I'm not sure I'll stick with it, the project isn't that big and the convention-driven indirection makes me think too much. Besides, I'm getting faster at typing INotifyPropertyChanged;

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Font rendering in labels on Windows Forms
« Reply #11 on: January 26, 2015, 08:04:28 AM »
Besides, I'm getting faster at typing INotifyPropertyChanged;

LOL.  Yeah, now anytime I create an app with WPF in it.  I automatically create a base class with INotifyPropertyChanged. Oh, and you'll get used to INotifyDataErrorInfo too.  Thats another interface you'll be using a lot of in WPF.
Revit 2019, AMEP 2019 64bit Win 10

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #12 on: January 27, 2015, 08:05:52 AM »
INotifyDataErrorInfo  is only in .NET 4.5 so that precludes any versions of AutoCAD pre-2015, unless you're happy running 4.5 in versions that officially only support 4.0 (if you've release something that does this I'd love to hear about it, it works on my machine [tm] but I'm not so sure about letting it outside) and definitely pre-2012. For oldies you need to use some variation on IDataErrorInfo

Related: coming soon to the getting started thread... 120++ links to WPF info's I have spent the last week or 2 trawling. Rather than dump it and run I would like to add some sort of explanation and...what's the opposite of insights? I'll start a meta-thread about it too so we can pick it to bits and build a valuable resource.

Also related: it's a shame AutoCAD hates multi-threading so much because Reactive Extensions is teh awse.

clint78z

  • Mosquito
  • Posts: 5
Re: Font rendering in labels on Windows Forms
« Reply #13 on: April 22, 2022, 04:58:16 PM »
Cadbloke can you help me out with Caliburn micro I have a big WPF project trying to get into a tool paltete. For some reason I can't get the configure () proceedure to fire.

At a glance, if this is for more than converting this one project then your default container should be a Grid not a StackPanel. It's a neat idea but if you already have the look of the UI from your windows form.  Then you can probably scratch this out manually in less time than it took to create this utility. Nice work though.

Changing container types is an easy right-click in Blend so I wasn't overly concerned about the actual type. Also, a Grid needs row & columns defined and figured out which would involve figuring out exactly where the controls lie on the Windows.Form - boring! In a StackPanel or similar you can just drag them around in the Tree in Blend. You should have seen the mess when I tried using Canvas and placing everything absolutely. Don't go there.

The utility was a good exercise in why Windows.Forms doesn't translate directly into WPF. HTML + CSS is a close analogy.

For bonus point I managed to get Caliburn.Micro working in a Palette control. It works but I'm not sure I'll stick with it, the project isn't that big and the convention-driven indirection makes me think too much. Besides, I'm getting faster at typing INotifyPropertyChanged;

CADbloke

  • Bull Frog
  • Posts: 337
  • Crash Test Dummy
Re: Font rendering in labels on Windows Forms
« Reply #14 on: April 22, 2022, 07:43:40 PM »
Cadbloke can you help me out with Caliburn micro I have a big WPF project trying to get into a tool paltete. For some reason I can't get the configure () proceedure to fire.

Sorry, I canít help there. I never got into Caliburn, Catel etc. all those frameworks were just way too much superfluous stuff to remember, I use MVVM light & Fody.PropertyChanged.

MVVM Light is now obsolete. See https://github.com/lbugnion/mvvmlight