Results 1 to 7 of 7

Thread: Use non ClearType or Non TTF Fonts in VB

  1. #1

    Thread Starter
    Member
    Join Date
    May 2020
    Posts
    36

    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.

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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):
    Name:  AliasedText.jpg
Views: 592
Size:  113.8 KB
    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2020
    Posts
    36

    Re: Use non ClearType or Non TTF Fonts in VB

    Quote Originally Posted by boops boops View Post
    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):
    Name:  AliasedText.jpg
Views: 592
Size:  113.8 KB
    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Use non ClearType or Non TTF Fonts in VB

    Quote Originally Posted by isaacthedeveloper View Post
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2020
    Posts
    36

    Re: Use non ClearType or Non TTF Fonts in VB

    OK figured it out thanks!

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  7. #7

    Thread Starter
    Member
    Join Date
    May 2020
    Posts
    36

    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
  •  



Click Here to Expand Forum to Full Width