2012-12-07 35 views
0

有没有什么办法像在以前的Windows版本中一样在Windows 8上渲染Tahoma字体文本?我们在WinForms应用程序中使用GDI Graphics.DrawString()来绘制它,但结果看起来很不一样。字符间距很大。在Windows 8上的Tahoma字体渲染8

谢谢。

回答

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); 
    } 
} 

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

enter image description here

+0

文本渲染器不支持透明度(在透明位图上绘制黑色文本),你知道这个解决方法吗? – abenci

+1

它的确如此。点击提问按钮以正确记录您的新问题。 –

+0

问题是,如果您在透明背景位图上绘制白色字符串,则抗锯齿​​将以黑色完成,而不是调制Alpha通道... – abenci