如何使Glass按钮忽略表格的渐变?

时间:2012-02-29 04:35:51

标签: c# user-interface

这里有一些非常棒的玻璃按钮代码:http://www.lukesw.net/articles/GlassButton.aspx

我对这个按钮唯一的麻烦是,如果我对表格应用渐变,它会影响按钮的颜色,因此它不是我在设计时选择的。我不知道这是否是我正在使用的代码来应用导致此问题的表单渐变,或者按钮是不是完全不透明或什么。我尝试了一下按钮代码,但没有得到任何结果。您可以在我上面发布的链接中获取该按钮的代码。下面是我用于表单渐变的代码,它现在位于表单中:

    private Color _Color1 = Color.Gainsboro;
    private Color _Color2 = Color.Blue;
    private float _ColorAngle = 60f;

    public Color Color1
    {

        get { return _Color1; }
        set {
            _Color1 = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }
    }

    public Color Color2
    {
        get { return _Color2; }
        set {
            _Color2 = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }
    }

    public float ColorAngle
    {
        get { return _ColorAngle; }
        set {
            _ColorAngle = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        Graphics g = pevent.Graphics;
        Rectangle rBackground = new Rectangle(0, 0, this.Width, this.Height);

        System.Drawing.Drawing2D.LinearGradientBrush bBackground
            = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground,
                                              _Color1, _Color2, _ColorAngle);

        g.FillRectangle(bBackground, rBackground);
        bBackground.Dispose();
    }

关于如何让这个按钮在运行时显示与设计时相同的任何指针都将非常感谢!

1 个答案:

答案 0 :(得分:2)

DrawButtonBackground中的GlowButton.cs方法中,只需将不透明度更改为完全不透明(255):

#region " content "
using (GraphicsPath bb = CreateRoundRectangle(rect, 2))
{
    //int opacity = pressed ? 0xcc : 0x7f;
    int opacity = 255;
    using (Brush br = new SolidBrush(Color.FromArgb(opacity, backColor)))
    {
        g.FillPath(br, bb);
    }
}
#endregion