我整个早上都在搜索,不幸的是我不确定这个问题的技术术语是什么,所以我找不到解决方案。
当我从 GroupBox 派生并覆盖 onPaint 函数时,groupbox 会在先前的 groupbox 之上重新绘制自己。子控件正确绘制,只是组框受到影响..
class ExtendedComponents
{
public partial class extendedGroupBox : GroupBox
{
private Color borderColor;
public extendedGroupBox()
{
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ContainerControl, true);
this.borderColor = Color.Black;
}
[NotifyParentProperty(true)]
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text, this.Font);
Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Dotted);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width + 5;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
}
}
任何帮助将非常感激!