1

午前中ずっと検索してきましたが、残念ながらこの問題の専門用語が何なのかわからないため、解決策を見つけることができません。

GroupBox から派生させて onPaint 関数をオーバーライドすると、グループボックスは以前のグループボックスの上に再描画されます。子コントロールは正しく描画されますが、グループ ボックスだけが影響を受けます。

スクリーンショット

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

どんな助けでも大歓迎です!

4

2 に答える 2

1

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

于 2011-12-01T17:37:44.160 に答える