自定义GroupBox,带有自定义TextColor,BorderColor和Transparent BackColor

时间:2016-01-02 02:42:32

标签: c# .net winforms transparency groupbox

我正在使用WinForms。在我的表格中,我有一个GroupBox。这是 自定义 组框。我想要groupbox的透明背景。我在为groupbox创建透明背景时遇到问题此代码的问题是当我将组框backcolor设置为透明时,我继续收到错误。

  

错误:控件不支持透明背景颜色。

g.Clear(BackColor = Color.Transparent);(这是给我问题的那条线)

    private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
    {
        if (box != null)
        {
            Brush textBrush = new SolidBrush(textColor);
            Brush borderBrush = new SolidBrush(borderColor);
            Pen borderPen = new Pen(borderBrush);
            SizeF strSize = g.MeasureString(box.Text, box.Font);
            Rectangle rect = new Rectangle(box.ClientRectangle.X,
                                           box.ClientRectangle.Y + (int)(strSize.Height / 2),
                                           box.ClientRectangle.Width - 1,
                                           box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);

            // Clear text and border
            g.Clear(BackColor = Color.Transparent);

            // Draw text
            g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0);

            // Drawing Border
            //Left
            g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height));
            //Right
            g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height));
            //Bottom
            g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height));
            //Top1
            g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y));
            //Top2
            g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y));
        }
    }

    private void groupBox1_Paint(object sender, PaintEventArgs e)
    {
        GroupBox box = sender as GroupBox;
        DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue);
    }

g.Clear(groupBox1.BackColor = Color.Transparent);

如果我这样做,我得到:

  

此示例包含Panel,面板内有骰子图像,   和自定义Groupbox

enter image description here

3 个答案:

答案 0 :(得分:7)

GroupBox控件支持透明背景,除非您使用System作为FlatStyle,但对于边框颜色,您需要自己绘制组框。

您可以继承GroupBox,然后GroupBox支持Transparent背景,因此您可以简单地覆盖OnPaint并渲染您的群组框,而无需对背景做任何事情

<强>代码

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
public class GroupBoxEx : GroupBox
{
    private Color borderColor = Color.Black;
    [DefaultValue(typeof(Color), "Black")]
    public Color BorderColor
    {
        get { return borderColor; }
        set { borderColor = value; this.Invalidate(); }
    }
    private Color textColor = Color.Black;
    [DefaultValue(typeof(Color), "Black")]
    public Color TextColor
    {
        get { return textColor; }
        set { textColor = value; this.Invalidate(); }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        GroupBoxState state = base.Enabled ? GroupBoxState.Normal : 
            GroupBoxState.Disabled;
        TextFormatFlags flags = TextFormatFlags.PreserveGraphicsTranslateTransform | 
            TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.TextBoxControl | 
            TextFormatFlags.WordBreak;
        Color titleColor = this.TextColor;
        if (!this.ShowKeyboardCues) 
            flags |= TextFormatFlags.HidePrefix;
        if (this.RightToLeft == RightToLeft.Yes) 
            flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
        if (!this.Enabled) 
            titleColor = SystemColors.GrayText;
        DrawUnthemedGroupBoxWithText(e.Graphics, new Rectangle(0, 0, base.Width,
            base.Height), this.Text, this.Font, titleColor, flags, state);
        RaisePaintEvent(this, e);
    }
    private void DrawUnthemedGroupBoxWithText(Graphics g, Rectangle bounds, 
        string groupBoxText, Font font, Color titleColor, 
        TextFormatFlags flags, GroupBoxState state)
    {
        Rectangle rectangle = bounds;
        rectangle.Width -= 8;
        Size size = TextRenderer.MeasureText(g, groupBoxText, font, 
            new Size(rectangle.Width, rectangle.Height), flags);
        rectangle.Width = size.Width;
        rectangle.Height = size.Height;
        if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
            rectangle.X = (bounds.Right - rectangle.Width) - 8;
        else
            rectangle.X += 8;
        TextRenderer.DrawText(g, groupBoxText, font, rectangle, titleColor, flags);
        if (rectangle.Width > 0)
            rectangle.Inflate(2, 0);
        using (var pen = new Pen(this.BorderColor))
        {
            int num = bounds.Top + (font.Height / 2);
            g.DrawLine(pen, bounds.Left, num - 1, bounds.Left, bounds.Height - 2);
            g.DrawLine(pen, bounds.Left, bounds.Height - 2, bounds.Width - 1,
                bounds.Height - 2);
            g.DrawLine(pen, bounds.Left, num - 1, rectangle.X - 3, num - 1);
            g.DrawLine(pen, rectangle.X + rectangle.Width + 2, num - 1, 
                bounds.Width - 2, num - 1);
            g.DrawLine(pen, bounds.Width - 2, num - 1, bounds.Width - 2,
               bounds.Height - 2);
        }
    }
}

<强>截图

enter image description here

关于控件的一些说明

  • GroupBox控件支持透明背景,除非您将System用作FlatStyle
  • 您也可以从Panel继承,因为它是一个容器控件,并且还支持透明的背景颜色。
  • 如果您需要创建一个继承表单Control以支持透明背景的自定义控件,则应在构造函数中添加SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  • 以上代码基于原始GroupBox的绘图代码,我做了一些更改以符合您的要求,并且仍然像原始GroupBox一样。
  • 添加了
  • BorderColor属性以支持自定义边框颜色。
  • 使用ForeColor GroupBox属性来呈现控件标题可能很烦人,因为ForeColor是一个环境属性,并且将由子控件继承。所以我为此创建了另一个属性TextColor。 (组合框的孩子默认使用组合框的前色,除非你改变他们的前色属性。)

答案 1 :(得分:0)

试试这个调整:

在表单的构造函数中:

this.TransparencyKey = Color.Red;

然后在你的代码中:

g.Clear(groupBox1.TransparencyKey = Color.Red);

答案 2 :(得分:0)

您是否尝试过SetStyle

public partial class myGroupBox : GroupBox
{
    public TranspBackground()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    }
}