Windows 8上的Tahoma字体渲染

abe*_*nci 0 c# fonts gdi windows-8

有没有办法像以前的Windows版本一样在Windows 8上呈现Tahoma字体文本?我们在WinForms应用程序中使用GDI Graphics.DrawString()来绘制它,但结果看起来有很大不同.人物间隔很差.

谢谢.

Han*_*ant 5

是的,您应该始终支持TextRenderer类.它修复了低DPI设备(如监视器)上Graphics.DrawString()的破坏行为.TextRenderer.DrawText()使用GDI的DrawTextEx()winapi函数,该函数与许多本机Windows程序用于呈现文本的函数相同.

两种不同之处的一个很好的证明是这个样本形式:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e) {
        var s = "Hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
        e.Graphics.DrawString(s, this.Font, Brushes.Black, 0, 0);
        TextRenderer.DrawText(e.Graphics, s, this.Font, new Point(0, this.Font.Height), Color.Black);
        base.OnPaint(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

在96 dpi显示器上看起来像这样:

在此输入图像描述