【问题标题】:WinForms: Disable text anti-aliasing of RichTextBoxWinForms:禁用 RichTextBox 的文本抗锯齿
【发布时间】:2019-09-09 09:43:03
【问题描述】:

我使用 RichTextBox 来显示没有抗锯齿的更清晰的小文本。 TextBox 中的文本没有消除锯齿,从而提供清晰的轮廓。但是 RichTextBox 中的文本是抗锯齿的,它是模糊的。所以我想阻止 RichTextBox 对文本进行抗锯齿处理。

我认为这只有在 RichTextBox 也可以渲染位图文本时才有可能,因为对于小文本,如果在没有抗锯齿的情况下渲染,结果将无法读取。那么问题来了,RichTextBox 确实可以用位图模式而不是矢量模式来渲染文本吗?

环境:Windows 10 x64,VS2017

此问题的Disable Anti-aliasing on WinForms text rendering 答案不会影响 RichTextBox。

【问题讨论】:

  • WinForms 利用 Windows 附带的 RichEdit 控件。我们在写字板中得到相同的结果吗?

标签: winforms richtextbox


【解决方案1】:

原来RichTextBox可以渲染位图字体,这取决于你使用的字体,如果你使用的是位图字体,那么RitchTextBox只会渲染位图字体...

我使用 NetRtfWriter https://sourceforge.net/projects/netrtfwriter/ 生成 RTF 文本,然后将其放入 RichTextBox 的 Rtf 属性中。

Win 表单测试代码(txtMain 是 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;
    }

如果您没有为 Rtf 部分设置字体,那么 RichEditBox 将使用某种默认字体(可能与 Windows 常用控件相同),对于此默认字体,它将使用位图呈现某些字体大小范围,并为其他尺寸过小或过大而无法从位图字体获得良好效果的矢量字体渲染。

NetRtfWriter 有一个默认的内置字体设置,可以防止上述情况发生,所以首先修改 NetRtfWriter 的源代码:

在 RtfBasics.cs 中:

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

在 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

在上面的Winforms测试代码中,去掉字体设置使用“默认字体”:

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

Windows/.NET 将决定使用位图文本呈现的大小,因此最终结果可能取决于系统配置。但无论配置是什么,您都可以找到(您的脸非常靠近屏幕并使用显示器的原始分辨率...)用位图渲染的结果的一些行(如果没有,请扩大 i 的范围)。

对于我的系统是 4K 分辨率,300% 系统放大设置,默认的 Winforms exe 将渲染 9-13 位图。但是当在资源管理器中为可执行文件启用“高 DPI 缩放行为”并将其设置为“应用程序”时,它只会用位图渲染 3 和 4。

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2011-08-30
    • 2014-11-16
    • 2011-01-26
    相关资源
    最近更新 更多