当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《自定义GroupBox,带有自定义TextColor,BorderColor和TransparentBackColor》经验,为你挑选了1个好方法。

我正在使用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.

在此输入图像描述



1> Reza Aghaei..:

GroupBox控件支持透明背景,除非你用SystemFlatStyle,但边框颜色,你需要自己画的组框.

您可以继承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);
        }
    }
}

截图

在此输入图像描述

关于控制的一些注意事项

GroupBox控件支持透明背景,除非你用SystemFlatStyle.

您也可以继承,Panel因为它是一个容器控件,并且还支持透明的背面颜色.

如果您需要创建一个继承表单Control以支持透明背景的自定义控件,则应添加SetStyle(ControlStyles.SupportsTransparentBackColor, true);构造函数.

上面的代码是基于原始的绘图代码,GroupBox我做了一些更改,以满足您的要求,也保持原样GroupBox.

BorderColor 添加属性以支持自定义边框颜色.

使用ForeColor属性GroupBox来呈现控件的标题可能很烦人,因为它ForeColor是一个环境属性,并且将由子控件继承.所以我TextColor为此创建了另一个属性.(组合框的孩子默认使用组合框的前色,除非你改变他们的前色属性.)

推荐阅读
无名有名我无名_593
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有