我正在使用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
.
该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); } } }
截图
关于控制的一些注意事项
该GroupBox
控件支持透明背景,除非你用System
的FlatStyle
.
您也可以继承,Panel
因为它是一个容器控件,并且还支持透明的背面颜色.
如果您需要创建一个继承表单Control
以支持透明背景的自定义控件,则应添加SetStyle(ControlStyles.SupportsTransparentBackColor, true);
构造函数.
上面的代码是基于原始的绘图代码,GroupBox
我做了一些更改,以满足您的要求,也保持原样GroupBox
.
BorderColor
添加属性以支持自定义边框颜色.
使用ForeColor
属性GroupBox
来呈现控件的标题可能很烦人,因为它ForeColor
是一个环境属性,并且将由子控件继承.所以我TextColor
为此创建了另一个属性.(组合框的孩子默认使用组合框的前色,除非你改变他们的前色属性.)