透明.NET表单中的抗锯齿文本

我有一个C#应用程序,它以透明的.NET表单显示当前时间。 表格没有控件也没有边框。 其属性TransparencyKey设置为Form的背景颜色“浅灰色”以使其透明。
因此用户只能看到文本(当前时间)。

该文本在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”显示为磨损。 对于浅色背景,文字没问题

此图显示了问题和好的情况:

文字显示由于深色背景的抗锯齿而产生的条纹http://sofzh.miximages.com/c%23/antiAliasing_bad.png

显然,Windows确实以适合Form自身背景颜色的方式呈现文本,而不是以适合透明表单背后的背景的方式呈现文本。

是否有可能让Windows在渲染文本时考虑到Form背后的背景,以便摆脱边缘?

一个“解决方案”可能是通过相应地设置TextRenderingHint来关闭抗锯齿。 但到目前为止,这不是我首选的“解决方案”。

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

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

我最终做的是有两个选择:

  1. 通过临时将其不透明度设置为0来复制应用程序后面的背景,然后将抗锯齿文本绘制到该应用程序上。 如果窗口及其下的窗口不经常移动,则此方法很有效。
  2. 使用分层窗口 。 比TransparencyKey工作得更好,但仍然适用于非抗锯齿文本。 (只是避免使用ClearType字体,你会没事的)

在您的Display_Paint方法中,试试这个:

 this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles();