我想在标签控件中创建文本的淡入淡出效果。我更改了标签前景色中的 Alpha 值,但它不受影响。
我在这里看到同样的问题:http://phorums.com.au/showthread.php?190812-Alpha-value-of-the-forecolor-of-vs-2005-controls 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);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)