本文介绍了具有自定义TextColor,BorderColor和Transparent BackColor的自定义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);
画笔borderBrush =新的SolidBrush(borderColor);
Pen borderPen =新的Pen(borderBrush);
SizeF strSize = g.MeasureString(box.Text,box.Font);
Rectangle rect =新Rectangle(box.ClientRectangle.X,
box.ClientRectangle.Y +(int)(strSize.Height / 2),
box.ClientRectangle.Width-1,
box.ClientRectangle.Height-(int)(strSize.Height / 2)-1);

//清除文字和边框
g.Clear(BackColor = Color.Transparent);

//绘制文本
g.DrawString(box.Text,box.Font,textBrush,box.Padding.Left,0);

//绘制边框
//左
g.DrawLine(borderPen,rect.Location,new Point(rect.X,rect.Y + rect.Height));
//右
g.DrawLine(边框笔,新Point(矩形X +矩形宽度,矩形Y),新Point(矩形X +矩形宽度,矩形Y +矩形)高度));
//底部
g.DrawLine(borderPen,新点(rect.X,rect.Y + rect.Height),新Point(rect.X + rect.Width,rect.Y + rect。高度));
// 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) +矩形宽度,矩形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。透明);



如果我这样做,我会得到:

解决方案


一些注意事项控件




  • GroupBox 控件支持透明背景,除非您使用系统作为 FlatStyle

  • 您也可以从<$ c $继承c> Panel ,因为它是一个容器控件,并且还支持透明的背景色。

  • 如果您需要制作一个继承表单控件以支持透明背景,则应在构造函数中添加 SetStyle(ControlStyles.SupportsTransparentBackColor,true);

  • 以上代码基于原始 GroupBox 的绘图代码,我进行了一些更改以满足您的要求,并且主要像原始的 GroupBox

  • BorderColor 属性已添加以支持自定义边框颜色。

  • 使用 GroupBox ForeColor 属性来呈现控件标题令人讨厌,因为 ForeColor 是一个环境属性,它将被子控件继承。为此,我创建了另一个属性,例如 TextColor 。 (除非更改了box box的前景色属性,否则box box的孩子将默认使用box box的前景色。)


I'm using WinForms. In my form I have a GroupBox. This is a custom group box. I wanted a transparent background for the groupbox. I'm having issues creating a transparent background for the groupbox The problem with this code is i keep on getting an error when i set the group box backcolor to transparent.

g.Clear(BackColor = Color.Transparent); (This is the line that is giving me the problem)

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

If i do this i get:

解决方案

The GroupBox control supports transparent background unless you use System as FlatStyle, but for the border color, you need to paint the group box yourself.

You can inherit from GroupBox and then because GroupBox supports Transparent background, so you can simply override the OnPaint and render your group box without doing any thing about background.

Code

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

Screenshot

Some note about the control

  • The GroupBox control supports transparent background unless you use System as FlatStyle.
  • You can also inherit from Panel because is is a container control and also supports transparent back color.
  • If you need to make a custom control that inherits form Control to support transparent background, you should add SetStyle(ControlStyles.SupportsTransparentBackColor, true); in constructor.
  • Above code is based on drawing code of original GroupBox and I made some changes to fit your requirements and also remains like original GroupBox.
  • BorderColor property added to support custom border color.
  • Using ForeColor property of GroupBox to render the title of control may be annoying because ForeColor is an ambient property and will be inherited by child controls. So I created another property like TextColor for this purpose. (Children of group box will use fore color of group box by default, unless you change their fore color property.)

这篇关于具有自定义TextColor,BorderColor和Transparent BackColor的自定义GroupBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:34