在一个透明的NET的形式消除锯齿的文本 [英] Anti-Aliased Text in a Transparent .NET Form

查看:212
本文介绍了在一个透明的NET的形式消除锯齿的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#应用程序,它显示当前时间的 透明NET的形式。该表没有控制,没有边界。 它的属性TransparencyKey设置窗体的背景颜色 浅灰色,使之透明。
所以,用户只能看到文本(当前时间)。

I have a C# application which shows the current time in a transparent .NET Form. The Form has no controls and no border. Its property TransparencyKey is set to the Form's background color "light gray" to make it transparent.
So the user can only see the text (current time).

文本绘制在PaintEventHandler:

The text is drawn in the PaintEventHandler:

private void Display_Paint( object sender, PaintEventArgs e )
{
    Graphics formGraphics = e.Graphics;

    Font myFont = new Font( "Microsoft Sans Serif", 24, FontStyle.Bold );

    formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    //formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;

    formGraphics.DrawString( "00:00:00", myFont, Brushes.Green, 0.0F, 0.0F );

    myFont.Dispose();
}

由于抗锯齿文本00:00:00显示磨损时, 形式是在一个黑暗的背景。对于浅色背景的文本o.k。

Due to the anti-aliasing the text "00:00:00" is shown frayed when the Form is over a dark background. For light backgrounds the text is o.k.

此图显示了问题,正确的情形:

This image shows the problem and the good case:

显然,Windows不会呈现文本的方式,它适合于 形式本身的背景颜色,而不是在一种方式,它适合于 背景它是透明窗体后面

Obviously Windows does render the text in a way that it fits to the Form's own background color and not in a way that it fits to the background which is behind the transparent Form.

是否有可能让Windows拿后面的背景 渲染文本时,使我得到的形态进去 摆脱条纹?

Is it possible to let Windows take the background behind the Form into account when rendering the text so that I get rid of the fringes?

一个解决方案可以通过设置TextRenderingHint关掉抗锯齿 因此。但目前这不是我的preferred解决方案。

One "solution" could be to switch off anti-aliasing by setting TextRenderingHint accordingly. But up to now this is not my preferred "solution".

系统:
Windows XP中,SP 3,.NET 3.5,VS 2008

System:
Windows XP, SP 3, .NET 3.5, VS 2008

推荐答案

我问了一个类似的问题几个月前的。

我最终什么做什么是有两种选择:

What I ended up doing was having two options:

  1. 将其不透明度设置为0暂时,然后绘制反锯齿文字上的复制应用程序后面的背景。这种方法效果很好,如果窗口和那些在它不经常移动。
  2. 使用href="http://blogs.msdn.com/mswanson/archive/2005/07/07/436618.aspx" rel="nofollow">分层窗口一个
  1. Copy the background behind the application by setting its opacity to 0 temporarily, then draw antialiased text onto that. This approach works well if the window and those under it don't move often.
  2. Using a layered window. Works better than TransparencyKey, but still works best with non-antialiased text. (just avoid using a ClearType font and you will be fine)

这篇关于在一个透明的NET的形式消除锯齿的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆