绘制透明控件(所有内容也变得透明)

时间:2012-01-25 23:00:37

标签: c# winforms controls icons gdi+

我刚刚制作了一个使用alpha淡化的控件。问题是,我绘制的“Icon”(来自SystemIcons),其他控件(一个按钮atm)不透明。所有文本和背景都以我在“Graphics.DrawXXX(...)”中使用的alpha值淡出。

我怎么能用相同的alpha值绘制那些东西?

非常感谢!

protected override void OnPaint(PaintEventArgs e)
{
    if (!Visible) return;

    Graphics g = e.Graphics;

    Color borderColor;
    Color gradientTopColor;
    Color gradientBottomColor;
    Color textColor;

    if (_displayColorStyle == IB_DisplayColorStyle.Normal)
    {
        borderColor = Color.FromArgb(fade_currentAlpha, 187, 191, 196);
        gradientTopColor = Color.FromArgb(fade_currentAlpha, 246, 249, 251);
        gradientBottomColor = Color.FromArgb(fade_currentAlpha, 233, 236, 240);
        textColor = Color.FromArgb(fade_currentAlpha, 106, 106, 106);
    }
    else if (_displayColorStyle == IB_DisplayColorStyle.Info)
    {
        borderColor = Color.FromArgb(fade_currentAlpha, 171, 186, 208);
        gradientTopColor = Color.FromArgb(fade_currentAlpha, 224, 236, 249);
        gradientBottomColor = Color.FromArgb(fade_currentAlpha, 210, 225, 240);
        textColor = Color.FromArgb(fade_currentAlpha, 106, 106, 106);
    }
    else if (_displayColorStyle == IB_DisplayColorStyle.Warning)
    {
        borderColor = Color.FromArgb(fade_currentAlpha, 106, 106, 106);
        gradientTopColor = Color.FromArgb(fade_currentAlpha, 240, 225, 53);
        gradientBottomColor = Color.FromArgb(fade_currentAlpha, 239, 194, 37);
        textColor = Color.FromArgb(fade_currentAlpha, 106, 106, 106);
    }
    else
    {
        borderColor = Color.FromArgb(fade_currentAlpha, 106, 106, 106);
        gradientTopColor = Color.FromArgb(fade_currentAlpha, 255, 0, 0);
        gradientBottomColor = Color.FromArgb(fade_currentAlpha, 217, 0, 0);
        textColor = Color.FromArgb(fade_currentAlpha, 224, 236, 249);
    }

    //Draw Background
    LinearGradientBrush backgroundBrush = new LinearGradientBrush(ClientRectangle, gradientTopColor,
                                                                    gradientBottomColor, 90f);
    g.FillRectangle(backgroundBrush, 1, 1, ClientRectangle.Width - 2, ClientRectangle.Height - 2);

    //Draw Border
    g.DrawRectangle(new Pen(new SolidBrush(borderColor), 1f), 0, 0, ClientRectangle.Width - 1,
                    ClientRectangle.Height - 1);

    //Select Icon to use
    Icon usedIcon = _displayColorStyle == IB_DisplayColorStyle.Normal
                        ? null
                        : _displayColorStyle == IB_DisplayColorStyle.Info
                                ? SystemIcons.Information
                                : _displayColorStyle == IB_DisplayColorStyle.Warning
                                    ? SystemIcons.Warning
                                    : SystemIcons.Error;

    if (usedIcon != null) //If icon is used, draw this
        g.DrawIcon(usedIcon, 5, Size.Height / 2 - (SystemIcons.Error.Height / 2));

    if (!string.IsNullOrEmpty(_title)) //If a title should be displayed
    {
        if (usedIcon == null) //No Icon, text left aligned
        {
            g.DrawString(_text, new Font("Arial", 8f, FontStyle.Regular), new SolidBrush(textColor),
                            new RectangleF(5f, 20f, (Width - _closeButton.Width - 10), Height - 12f));
            g.DrawString(_title, new Font("Arial", 10f, FontStyle.Bold), new SolidBrush(textColor),
                            new RectangleF(5f, 5f, (Width - _closeButton.Width - 10), 15f));
        }
        else
        {
            g.DrawString(_text, new Font("Arial", 8f, FontStyle.Regular), new SolidBrush(textColor),
                            new RectangleF(10f + usedIcon.Width, 20f,
                                        (Width - _closeButton.Width - 20 - usedIcon.Width), Height - 12f));
            g.DrawString(_title, new Font("Arial", 10f, FontStyle.Bold), new SolidBrush(textColor),
                            new RectangleF(10f + usedIcon.Width, 5f, (Width - _closeButton.Width - 20 - usedIcon.Width), 15f));
        }
    }
    else
    {
        if (usedIcon == null) //No Icon, text left aligned
            g.DrawString(_text, new Font("Arial", 8f, FontStyle.Regular), new SolidBrush(textColor),
                            new RectangleF(5f, 5f, (Width - _closeButton.Width - 10), Height - 2f));
        else
            g.DrawString(_text, new Font("Arial", 8f, FontStyle.Regular), new SolidBrush(textColor),
                            new RectangleF(10f + usedIcon.Width, 5f, (Width - _closeButton.Width - 20 - usedIcon.Width), Height - 2f));
    }

    base.OnPaint(e);
}

0 个答案:

没有答案
相关问题