原来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。