【问题标题】:Alpha in ForeColor前景色中的 Alpha
【发布时间】:2014-04-06 01:15:59
【问题描述】:

我想在 Label 控件中创建文本的淡入淡出效果。我更改了 Label 的 ForeColor 中的 Alpha 值,但不受影响。

我在这里看到了同样的问题: http://phorums.com.au/showthread.php?190812-Alpha-value-of-the-forecolor-of-vs-2005-controls 但没有答案。

请帮助我。谢谢。

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    TextRenderer 类使用 GDI 的 DrawTextEx() 函数,它不支持透明度。将 UseCompatibleTextRendering 设置为 true 也无济于事,Label 类将前景色强制为 255 的 alpha 以使其与 TextRenderer 兼容。您所能做的就是编写自己的绘制覆盖。

    向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。请注意,我采取了一些捷径,它没有实现对齐、填充和启用。

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyLabel : Label {
      protected override void OnPaint(PaintEventArgs e) {
        Rectangle rc = this.ClientRectangle;
        StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
        using (var br = new SolidBrush(this.ForeColor)) {
          e.Graphics.DrawString(this.Text, this.Font, br, rc, fmt);
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多