gpt4 book ai didi

c# - 具有自定义 TextColor、BorderColor 和透明背景颜色的自定义 GroupBox

转载 作者:太空狗 更新时间:2023-10-29 22:57:55 31 4
gpt4 key购买 nike

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

Error: Control does not support transparent background colors.

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

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

This example consists of a Panel with a dice image inside the panel, and the custom Groupbox.

enter image description here

最佳答案

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 属性以支持自定义边框颜色。
  • 使用 GroupBoxForeColor 属性来呈现控件的标题可能会很烦人,因为 ForeColor 是一个环境属性,将由子项继承控制。为此,我创建了另一个属性,如 TextColor。 (默认情况下,组框的子项将使用组框的前景色,除非您更改其前景色属性。)

关于c# - 具有自定义 TextColor、BorderColor 和透明背景颜色的自定义 GroupBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562088/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com