【发布时间】: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 但没有答案。
请帮助我。谢谢。
【问题讨论】:
我想在 Label 控件中创建文本的淡入淡出效果。我更改了 Label 的 ForeColor 中的 Alpha 值,但不受影响。
我在这里看到了同样的问题: http://phorums.com.au/showthread.php?190812-Alpha-value-of-the-forecolor-of-vs-2005-controls 但没有答案。
请帮助我。谢谢。
【问题讨论】:
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);
}
}
}
【讨论】: