-
Oct 19th, 2020, 08:49 PM
#1
Thread Starter
Member
Use non ClearType or Non TTF Fonts in VB
Hello
I have a vb.net windows forms application that I am making, and I need some of the fonts in it to be not antialiased. When I try to select a select a non truetype font for the label text, it says that only truetype fonts are supported. There is no option to disable cleartype to use non antialiased fonts within the program's label settings, and disabling it system wide is not an option because I need cleartype on for other applications and this should be possible to disable antialiasing for some labels and button text in my application because other applications can do this and when cleartype is disabled systemwide it works properly.
-
Oct 20th, 2020, 06:54 PM
#2
Re: Use non ClearType or Non TTF Fonts in VB
I think System.Drawing can only render TTF fonts. But you can disable anti-aliasing and ClearType by setting the TextRenderingHint property of the Graphics object that draws the text to SingleBitPerPixel. Setting this property in the Paint event of the control concerned and using DrawString springs to mind.
But in the case of Labels, I would prefer to derive a class that sets the TextRenderingHint in the base class. Here's an example:
Code:
Public Class AliasedLabel
Inherits Label
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Me.UseCompatibleTextRendering = True
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel
MyBase.OnPaint(e)
End Sub
End Class
Add this to your project or a Class Library. The AliasedLabel control will appear at the top of the ToolBox after you build the project. Here is a magnified example of a normal Label (top) and an AliasedLabel (bottom):
Note: if you are using Windows 10 and have your text setting scaled to more or less than 100%, you should set your Form's AutoScaleMode property to None.
BB
Last edited by boops boops; Oct 20th, 2020 at 07:18 PM.
-
Oct 20th, 2020, 08:09 PM
#3
Thread Starter
Member
Re: Use non ClearType or Non TTF Fonts in VB
Originally Posted by boops boops
I think System.Drawing can only render TTF fonts. But you can disable anti-aliasing and ClearType by setting the TextRenderingHint property of the Graphics object that draws the text to SingleBitPerPixel. Setting this property in the Paint event of the control concerned and using DrawString springs to mind.
But in the case of Labels, I would prefer to derive a class that sets the TextRenderingHint in the base class. Here's an example:
Code:
Public Class AliasedLabel
Inherits Label
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Me.UseCompatibleTextRendering = True
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel
MyBase.OnPaint(e)
End Sub
End Class
Add this to your project or a Class Library. The AliasedLabel control will appear at the top of the ToolBox after you build the project. Here is a magnified example of a normal Label (top) and an AliasedLabel (bottom):
Note: if you are using Windows 10 and have your text setting scaled to more or less than 100%, you should set your Form's AutoScaleMode property to None.
BB
Where do I add the code? At the top of form 1 or in app.manifest? Can you send a screenshot?
-
Oct 20th, 2020, 08:16 PM
#4
Re: Use non ClearType or Non TTF Fonts in VB
Originally Posted by isaacthedeveloper
Where do I add the code? At the top of form 1 or in app.manifest? Can you send a screenshot?
Neither. It's a new class. You add a new class to your project and that will add a new code file. That's where you add the code. Once you build, that new control will be added to your Toolbox and you can use it instead of a regular Label control. If you already have lots of Labels and you don't want to replace them, you can open the relevant designer code files and replace System.Windows.Forms.Label with your new class name.
-
Oct 20th, 2020, 08:17 PM
#5
Thread Starter
Member
Re: Use non ClearType or Non TTF Fonts in VB
OK figured it out thanks!
-
Oct 21st, 2020, 05:38 PM
#6
Re: Use non ClearType or Non TTF Fonts in VB
As an alternative to editing the form designer code, you might consider making the derived Label "switchable". The class could then look something like this:
Code:
Public Class AALabel
Inherits Label
Public Property TextAntiAliased As Boolean = True
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Me.UseCompatibleTextRendering = Not TextAntiAliased
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixel
MyBase.OnPaint(e)
End Sub
End Class
I changed the class name to AALabel because it can go either way. Now you can use AALabel all the time instead of Label, and switch the aliasing for individual controls whenever you want, at design time or at run time. TextAntiAliased is practically an antonym for UseCompatibleTextRendering, I admit, but I prefer it because it's more self-explanatory.
I just tried this out for a Button and it works equally well. Add a new Class and inherit from Button instead of Label; the rest can be the same.
BB
Last edited by boops boops; Oct 21st, 2020 at 05:50 PM.
-
Nov 25th, 2020, 06:24 PM
#7
Thread Starter
Member
Re: Use non ClearType or Non TTF Fonts in VB
I also figured out that SingleBitPerPixelGridFit looks better, just for you nerds out there (unlike me)
Last edited by isaacthedeveloper; Nov 26th, 2020 at 01:42 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|