jw_
jw_

Reputation: 1785

WinForms: Disable text anti-aliasing of RichTextBox

I use RichTextBox to display small texts which is much sharper without anti-aliasing. Texts in TextBox is not anti-aliased which gives a sharp outline. But texts in RichTextBox is anti-aliased which is blured. So I want to prevent RichTextBox from anti-aliasing the texts.

I think this is only possible if RichTextBox can also render bitmap-text since for small text, if it is rendered without anti-aliasing, the result won't be readable. So the question is indeed can RichTextBox render text in bitmap mode instead of vector mode?

Environment: Windows 10 x64, VS2017

This question's Disable Anti-aliasing on WinForms text rendering answer doesn't affect RichTextBox.

Upvotes: 1

Views: 422

Answers (1)

jw_
jw_

Reputation: 1785

It turns out that RichTextBox can render bitmap font, it depends on the font that you use, if you use a bitmap font, then RitchTextBox will just render bitmap font...

I use NetRtfWriter https://sourceforge.net/projects/netrtfwriter/ to generate RTF text then put it in RichTextBox's Rtf property.

Win forms test code (txtMain is a RichTextBox)

    public Form1()
    {
        InitializeComponent();

        var doc = new RtfDocument(PaperSize.A4, PaperOrientation.Portrait, Lcid.English);
        string rtf;
        for (int i = 1; i < 20; i++)
        {                
            RtfCharFormat fmt;
            //basic text use bitmap font "roman"
            RtfParagraph par = doc.addParagraph();
            par.setText("("+i+") This is paragraph");
            par.DefaultCharFormat.FontSize = i;
            //search in Windows, there is a "roman.fon" in the system
            //which is a bitmap font
            par.DefaultCharFormat.Font = doc.createFont("roman");

            //this section use vector font
            fmt = par.addCharFormat(14, 17);
            fmt.FgColor = doc.createColor(new DW.RtfWriter.Color(0, 0, 255)); ;
            fmt.FontSize = i+10;
            fmt.Font = doc.createFont("Times New Roman");                
        }
        rtf = doc.render();
        txtMain.Rtf=rtf;
    }

If you don't set font for a Rtf section, then RichEditBox will use sort of default font (perhaps the same as Windows common controls), for this defaulft font, it will render with bitmap for some range of font sizes, and render vector font for other sizes that is too small or too big to get a good result from bitmap font.

NetRtfWriter has a default built'in font setting which prevent the above to happen so first modify the source code of NetRtfWriter:

In RtfBasics.cs:

//delete public static string Font = "Times New Roman";
public static string Font = null;//add

In RtfDocument.cs:

//delete rtf.AppendLine(@"{\f" + i + " " + RtfUtility.unicodeEncode(_fontTable[i].ToString()) + ";}");
if (_fontTable[i]!=null) rtf.AppendLine(@"{\f" + i + " " + RtfUtility.unicodeEncode(_fontTable[i].ToString()) + ";}");//add

In the above Winforms test code, remove the font setting to use the "default font":

//delete par.DefaultCharFormat.Font = doc.createFont("roman");

Windows/.NET will decide which size will be rendered with bitmap text, so the final result may depends on system configuration. But whatever the configuration is, you can find (with your face very near the screen and use native resolution of the monitor...) some lines of the result rendered with bitmap (if not, expand the range of i).

For my system which is 4K resolution, 300% system enlarge setting, the default Winforms exe will render 9-13 with bitmap. But when enable "High DPI scaling behavior" and set it to "application" for the executable in Explorer, it will only render 3 and 4 with bitmap.

Upvotes: 2

Related Questions