我一直在搜索整个上午,不幸的是我不知道什么技术术语是这个问题,所以我无法找到一个解决方案。继承GroupBox有OnPaint抖动
当我从一个GroupBox派生并重写onPaint函数时,groupboxes将自己重画在前面的groupboxes之上。该子控件油漆正确,只是分组框中会受到影响..
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);
}
}
}
任何帮助,将不胜感激!
this.ClientRectangle无效,但我决定改为使用Panel控件。谢谢! – Hutch
@荷兰很高兴你可以使用它。不知道为什么this.ClientRectangle无效。我用你的代码,用this.ClientRectangles和时髦的图纸取代了e.ClipRectangles。 – LarsTech