【问题标题】:Anti-Aliased Text in a Transparent .NET Form透明 .NET 表单中的抗锯齿文本
【发布时间】:2009-10-20 20:02:35
【问题描述】:

我有一个 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”在 表格位于深色背景上。对于浅色背景,文本还可以。

这张图片显示了问题和好的案例:


(来源:habermann-net.de

显然,Windows 确实以适合 表单自己的背景颜色,而不是它适合的方式 透明表单后面的背景。

是否可以让 Windows 做后台 呈现文本时考虑到表单,以便我得到 摆脱边缘?

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

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

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    几个月前我问过similar question

    我最终做了两个选择:

    1. 通过将其不透明度临时设置为 0 来复制应用程序背后的背景,然后在其上绘制抗锯齿文本。如果窗口及其下方的窗口不经常移动,这种方法很有效。
    2. 使用layered window。比 TransparencyKey 效果更好,但仍然最适合非抗锯齿文本。 (只要避免使用 ClearType 字体就可以了)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢您的提示,但设置样式无济于事。
      • 我想了解更多关于复制部分的信息
      • 其实我搜了一下,好像在找CopyFromScreen,stackoverflow.com/questions/715094/…
      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2012-03-08
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      相关资源
      最近更新 更多