2011-12-01 51 views
1

我一直在搜索整个上午,不幸的是我不知道什么技术术语是这个问题,所以我无法找到一个解决方案。继承GroupBox有OnPaint抖动

当我从一个GroupBox派生并重写onPaint函数时,groupboxes将自己重画在前面的groupboxes之上。该子控件油漆正确,只是分组框中会受到影响..

Screenshot

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

任何帮助,将不胜感激!

回答

2

简单的答案是不使用GroupBox控件 - 它本质上是轻弹的。

尝试使用Panel控件,而不是你的双缓冲SetStyles等

对于您目前的实现,不使用e.ClipRectangle

//Rectangle borderRect = e.ClipRectangle; 
Rectangle borderRect = this.ClientRectangle; 

//Rectangle textRect = e.ClipRectangle; 
Rectangle textRect = this.ClientRectangle; 
+0

this.ClientRectangle无效,但我决定改为使用Panel控件。谢谢! – Hutch

+0

@荷兰很高兴你可以使用它。不知道为什么this.ClientRectangle无效。我用你的代码,用this.ClientRectangles和时髦的图纸取代了e.ClipRectangles。 – LarsTech

1

另外一点需要注意的是,你应该重写OnPaintBackground避免闪烁。在那里,你要么不做任何事情,要么控制前面的颜色。

+0

谢谢Aquaherd,我也会研究这个方法。 – Hutch