【问题标题】:Create a ContainerControl with padding included创建一个包含填充的 ContainerControl
【发布时间】:2014-03-06 21:11:53
【问题描述】:

我想创建一个包含 25px Header 的控件。当我添加子控件时,我希望它们的行为就像在 GroupBox 中使用一样。 当控件的 DockStyle 设置为 Fill in a groupbox 时,它的位置自动设置为 3;16,其大小自动设置为 Width-6; Height-19。 我不想使用 Padding 或 Margin,因为它们似乎没有在 GroupBox 中使用。

如何在我自己的控件中实现相同的行为?

【问题讨论】:

    标签: c# .net winforms controls


    【解决方案1】:

    尝试覆盖面板的 DisplayRectangle 属性:

    public class MyContainer : Panel {
    
      public override Rectangle DisplayRectangle {
        get {
          int headerHeight = 25;
          return new Rectangle(
            this.Padding.Left,
            this.Padding.Top + headerHeight,
            this.ClientSize.Width - 
              (this.Padding.Left + this.Padding.Right),
            this.ClientSize.Height - 
              (this.Padding.Top + this.Padding.Bottom + headerHeight));
        }
      }
    }
    

    【讨论】:

    • 感谢您的回答,尽管我已经自己找到了。效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多