Alpha在ForeColor中 [英] Alpha in ForeColor

查看:51
本文介绍了Alpha在ForeColor中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Label控件中创建文本的褪色效果.我在Label的ForeColor中更改了Alpha值,但是它不受影响.

I want to create a fading effect of text in Label control. I change Alpha value in Label's ForeColor but it is not affected.

我在这里看到了一个相同的问题: http://phorums.com.au/showthread.php?190812-alpha-value-of-fore-forecolor-of-vs-2005-controls 但没有答案.

I see a same question at here: http://phorums.com.au/showthread.php?190812-Alpha-value-of-the-forecolor-of-vs-2005-controls but no answer.

请帮助我.谢谢.

推荐答案

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

The TextRenderer class uses GDI's DrawTextEx() function, it doesn't support transparency. Setting UseCompatibleTextRendering to true doesn't help either, the Label class forces the foreground color to an alpha of 255 to keep it compatible with TextRenderer. All you can do is write your own paint override.

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

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Beware that I took a few short-cuts, it doesn't implement alignment, padding and Enabled.

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);
    }
  }
}

这篇关于Alpha在ForeColor中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆