Collapsible GroupBox

GroupBox с возможностью сворачивания и разворачивания, реализован по аналогии с проектом найденным на просторах http://www.codeproject.com в который были внесены некоторые изменения:

  • Высота свернутого GroupBox’а указывается в дизайнере.
  • Состояние GroupBox’а указывается в дизайнере.
  • Кнопка сворачивания рисуется как в TreeView.

Collapsible GroupBox

Исходный код: CollapsibleGroupBoxExample (zip, 6.03 Кб)


Описание класса:

    1 using System.ComponentModel;

    2 using System.Drawing;

    3 using System.Windows.Forms.VisualStyles;

    4 

    5 namespace System.Windows.Forms

    6 {

    7     /// <summary>

    8     /// GroupBox with collapsible feature

    9     /// </summary>

   10     public partial class CollapsibleGroupBox : GroupBox

   11     {

   12         #region Members

   13 

   14         private bool m_IsCollapsed = false;

   15         private int m_CollapsedHeight = 20;

   16         private int m_ExpandedHeight = 20;

   17         private Rectangle m_GlyphRect = Rectangle.Empty;

   18 

   19         #endregion

   20 

   21         #region Properties

   22 

   23         /// <summary>

   24         /// GroupBox collapsed state

   25         /// </summary>

   26         [DefaultValue(false)]

   27         public bool IsCollapsed

   28         {

   29             get { return m_IsCollapsed; }

   30             set

   31             {

   32                 if (m_IsCollapsed != value)

   33                 {

   34                     m_IsCollapsed = value;

   35 

   36                     if (m_IsCollapsed)

   37                     {

   38                         m_ExpandedHeight = Height;

   39                         Height = m_CollapsedHeight;

   40                     }

   41                     else

   42                         Height = m_ExpandedHeight;

   43 

   44                     foreach (Control control in Controls)

   45                         control.Visible = !m_IsCollapsed;

   46                 }

   47             }

   48         }

   49 

   50         /// <summary>

   51         /// GroupBox colapsed height

   52         /// </summary>

   53         [DefaultValue(20)]

   54         public int CollapsedHeight

   55         {

   56             get { return m_CollapsedHeight; }

   57             set

   58             {

   59                 if (m_CollapsedHeight != value)

   60                 {

   61                     m_CollapsedHeight = value;

   62                     if (m_IsCollapsed)

   63                         Height = m_CollapsedHeight;

   64                 }

   65             }

   66         }

   67 

   68         #endregion

   69 

   70         #region Overrides

   71 

   72         /// <summary>

   73         /// Change collapsed state in runtime mode

   74         /// </summary>

   75         /// <param name="e">

   76         /// A <seealso cref="System.Windows.Forms.MouseEventArgs"/> that contains the event data

   77         /// </param>

   78         protected override void OnMouseUp(MouseEventArgs e)

   79         {

   80             if (m_GlyphRect.Contains(e.Location))

   81                 IsCollapsed = !IsCollapsed;

   82             else

   83                 base.OnMouseUp(e);

   84         }

   85 

   86         /// <summary>

   87         /// Change collapsed height in design mode

   88         /// </summary>

   89         /// <param name="e">

   90         /// An <seealso cref="System.EventArgs"/> that contains the event data

   91         /// </param>

   92         protected override void OnResize(EventArgs e)

   93         {

   94             if (DesignMode && IsCollapsed)

   95                 CollapsedHeight = Height;

   96             else

   97                 base.OnResize(e);

   98         }

   99 

  100         /// <summary>

  101         /// Paint GroupBox with open/close glyph

  102         /// </summary>

  103         /// <param name="e">

  104         /// A <seealso cref="System.Windows.Forms.PaintEventArgs"/> that contains the event data

  105         /// </param>

  106         protected override void OnPaint(PaintEventArgs e)

  107         {

  108             // Draw GroupBox

  109             Rectangle bounds = new Rectangle(ClientRectangle.X,

                                                   ClientRectangle.Y + Font.Height / 2,

                                                   ClientRectangle.Width,

                                                   ClientRectangle.Height - Font.Height / 2);

  110             GroupBoxRenderer.DrawGroupBox(e.Graphics,

                                                bounds,

                                                Enabled ? GroupBoxState.Normal : GroupBoxState.Disabled);

  111 

  112             // Draw GroupBox glyph

  113             VisualStyleRenderer glyphRenderer = new VisualStyleRenderer(IsCollapsed ?

                                                          VisualStyleElement.TreeView.Glyph.Closed :

                                                          VisualStyleElement.TreeView.Glyph.Opened);

  114             Size glyphSize = glyphRenderer.GetPartSize(e.Graphics, ThemeSizeType.Draw);

  115             m_GlyphRect = new Rectangle(bounds.X + 8,

                                              bounds.Y - glyphSize.Height / 2,

                                              glyphSize.Width,

                                              glyphSize.Height);

  116             glyphRenderer.DrawBackground(e.Graphics, m_GlyphRect);

  117 

  118             // Draw GroupBox text

  119             Point textPoint = new Point(m_GlyphRect.Right + 2, 0);

  120             TextRenderer.DrawText(e.Graphics, Text, Font, textPoint,

                                        Enabled ? SystemColors.ActiveCaption : SystemColors.GrayText, BackColor);

  121         }

  122 

  123         #endregion

  124     }

  125 }

Ответить