2011-12-01 36 views
1

午前中に検索しましたが、残念ながらtechincalという用語がこの問題の原因であるかどうかはわかりませんので、解決策を見つけることができません。Inherited GroupBoxにOnPaintジッタがあります

GroupBoxから派生し、onPaint関数をオーバーライドすると、グループボックスは前のグループボックスの上に再描画されます。子コントロールは、すべてのヘルプははるかに高く評価されるだろう

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コントロールを使用しないことです。これは本質的にフリッキーです。

e.ClipRectangleを使用していない、あなたの現在の実装では

など、あなたのDoubleBuffer SetStylesで代わりにPanelコントロールを使用してみてください:

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

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

this.ClientRectangleは有効ではありませんでしたが、代わりにPanelコントロールの考え方を採用することにしました。ありがとう! – Hutch

+0

@Hutch Gladあなたはそれを使うことができます。なぜthis.ClientRectangleが有効でないのか分かりません。私はあなたのコードを使用し、this.ClientRectanglesとe.ClipRectanglesを置き換え、ファンキーな描画が消えました。 – LarsTech

1

注意すべきもう一つは、あなたがOnPaintBackgroundオーバーライドしなければならないことですちらつきを避けるために。あなたは何もしないか、コントロールを前の色に描画します。

+0

Aquaherdに感謝します。この方法についても勉強します。 – Hutch

関連する問題