C# 继承的GroupBox具有OnPaint抖动
我整个上午都在搜索,不幸的是,我不确定这个问题的技术术语是什么,所以我无法找到解决方案 当我从一个GroupBox派生并重写onPaint函数时,GroupBox会在前面的GroupBox之上重新绘制自己。子控件正确地控制绘制,只影响groupboxC# 继承的GroupBox具有OnPaint抖动,c#,winforms,derived,groupbox,C#,Winforms,Derived,Groupbox,我整个上午都在搜索,不幸的是,我不确定这个问题的技术术语是什么,所以我无法找到解决方案 当我从一个GroupBox派生并重写onPaint函数时,GroupBox会在前面的GroupBox之上重新绘制自己。子控件正确地控制绘制,只影响groupbox class ExtendedComponents { public partial class extendedGroupBox : GroupBox { private Color borderColor; public
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);
}
}
}
任何帮助都将不胜感激 简单的答案是不要使用GroupBox控件——它本身就是轻快的 尝试使用面板控件代替DoubleBuffer设置样式等 对于当前的实现,不要使用
e.ClipRectangle
:
//Rectangle borderRect = e.ClipRectangle;
Rectangle borderRect = this.ClientRectangle;
//Rectangle textRect = e.ClipRectangle;
Rectangle textRect = this.ClientRectangle;
另一件需要注意的事情是,您应该覆盖OnPaintBackground以避免闪烁。在这里,您可以不执行任何操作,也可以绘制控件的前颜色 此.ClientRectangle无效,但我决定采用您的面板控制想法。谢谢@哈奇很高兴你能用上它。不知道为什么这个.ClientRectangle无效。我使用了你们的代码,用this.ClientRectangles替换了e.ClipRectangles,那个些时髦的绘图就消失了。