| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
-
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 可折叠分组控件,该控件显示围绕一组具有可选标题的控件的框架。
- /// </summary>
- [ToolboxBitmap(typeof(GroupBox))]
- public class GrpCollapsible : GrpGroupBox
- {
- #region 事件声明
- #region GroupBoxCollapsing
- /// <summary>
- /// 扩展或折叠事件
- /// </summary>
- private static readonly object EventGroupBoxCollapsing = new object();
- /// <summary>
- /// GroupBoxCollapsing 事件
- /// </summary>
- [Description("当 Collapsed 属性的值更改时发生。"), Category("CustomerEx")]
- public event GroupBoxCollapsingEventHandler GroupBoxCollapsing
- {
- add
- {
- base.Events.AddHandler(EventGroupBoxCollapsing, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventGroupBoxCollapsing, value);
- }
- }
- #endregion
- #region GroupBoxCollapseLinking
- /// <summary>
- /// 当 Collapsed 属性的值更改时发生。
- /// </summary>
- private static readonly object EventGroupBoxCollapseLinking = new object();
- /// <summary>
- /// 当 Collapsed 属性的值更改时发生。
- /// </summary>
- [Description("当 Collapsed 属性的值更改时发生。"), Category("CustomerEx")]
- public event GroupBoxCollapseLinkingEventHandler GroupBoxCollapseLinking
- {
- add
- {
- base.Events.AddHandler(EventGroupBoxCollapseLinking, value);
- }
- remove
- {
- base.Events.RemoveHandler(EventGroupBoxCollapseLinking, value);
- }
- }
- #endregion
- #endregion
- #region 成员变量
- /// <summary>
- /// 允许折叠
- /// </summary>
- private bool _allowCollapse = true;
- /// <summary>
- /// 折叠状态
- /// </summary>
- private bool _collapsed = false;
- /// <summary>
- /// 折叠时标志
- /// </summary>
- private string _collapseMark = Constant.GB_CLOSING_MARK;
- /// <summary>
- /// 展开时标志
- /// </summary>
- private string _expandingMark = Constant.GB_OPENING_MARK;
- /// <summary>
- /// 文本
- /// </summary>
- private string _text = string.Empty;
- /// <summary>
- /// 控件折叠前高度
- /// </summary>
- private int _expandinglHeight = 0;
- /// <summary>
- /// 控件折叠后高度
- /// </summary>
- private int _collapsedHeight = 0;
- /// <summary>
- /// 控件折叠调整高度
- /// </summary>
- private int _adjustHeight = 0;
- ///// <summary>
- ///// 折叠连锁反应
- ///// </summary>
- //private CollapseChainReaction _chainReaction = CollapseChainReaction.Below;
- private bool _isSizeSetting = false;
- #endregion
- #region 构造函数
- /// <summary>
- /// 可折叠分组控件,该控件显示围绕一组具有可选标题的控件的框架。
- /// </summary>
- public GrpCollapsible()
- {
- this.CollapseLinked = true;
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取或设置控件是否允许折叠。
- /// </summary>
- [Description("获取或设置控件是否允许折叠。"), Category("CustomerEx")]
- [DefaultValue(true)]
- public bool AllowCollapse
- {
- get
- {
- return this._allowCollapse;
- }
- set
- {
- if (this._allowCollapse != value)
- {
- this._allowCollapse = value;
- this.SetText();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件折叠时标志。
- /// </summary>
- [Description("获取或设置控件折叠时标志。"), Category("CustomerEx")]
- [DefaultValue("▲")]
- public string CollapseMark
- {
- get
- {
- return _collapseMark;
- }
- set
- {
- if (this._collapseMark != value)
- {
- this._collapseMark = value;
- this.SetText();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件展开时标志。
- /// </summary>
- [Description("获取或设置控件展开时标志。"), Category("CustomerEx")]
- [DefaultValue("▼")]
- public string ExpandingMark
- {
- get
- {
- return _expandingMark;
- }
- set
- {
- if (this._expandingMark != value)
- {
- this._expandingMark = value;
- this.SetText();
- }
- }
- }
- /// <summary>
- /// 获取或设置控件折叠状态。
- /// </summary>
- [Description("获取或设置控件折叠状态。"), Category("CustomerEx")]
- [DefaultValue(false)]
- public bool Collapsed
- {
- get
- {
- return this._collapsed;
- }
- set
- {
- if (this._collapsed != value)
- {
- if (value)
- {
- this.Collapse();
- }
- else
- {
- this.Expand();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示控件在折叠/展开时,其他控件是否连动。
- /// </summary>
- [Description("获取或设置一个值,该值指示控件在折叠/展开时,其他控件是否连动。"), Category("CustomerEx")]
- [DefaultValue(true)]
- public bool CollapseLinked
- {
- get;
- set;
- }
- ///// <summary>
- ///// 获取或设置控件在折叠/展开时,其他控件的动作。
- ///// </summary>
- //[Description("获取或设置控件在折叠/展开时,其他控件的动作。"), Category("CustomerEx")]
- //[DefaultValue(typeof(CollapseChainReaction), "Below")]
- //public CollapseChainReaction ChainReaction
- //{
- // get
- // {
- // return this._chainReaction;
- // }
- // set
- // {
- // if (this._chainReaction != value)
- // {
- // this._chainReaction = value;
- // }
- // }
- //}
- #endregion
- #region 重写属性
- /// <summary>
- /// 获取和设置文本
- /// </summary>
- public new string Text
- {
- get
- {
- return this._text;
- }
- set
- {
- if (this._text != value)
- {
- this._text = value;
- this.SetText();
- }
- }
- }
- #endregion
- #region 重写事件
- #region 属性改变
- /// <summary>
- /// 改变控件大小
- /// </summary>
- /// <param name="e"></param>
- protected override void OnSizeChanged(System.EventArgs e)
- {
- if (this._isSizeSetting)
- {
- return;
- }
- if (this._collapsed && this.Height != this._collapsedHeight)
- {
- this._isSizeSetting = true;
- this.Height = this._collapsedHeight;
- this._isSizeSetting = false;
- return;
- }
- base.OnSizeChanged(e);
- }
- /// <summary>
- /// 鼠标点击,折叠或展开
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseClick(MouseEventArgs e)
- {
- base.OnMouseClick(e);
- if (this._allowCollapse && e.Button == MouseButtons.Left)
- {
- if (0 < e.Y && e.Y < this.FontHeight && 3 < e.X && e.X < this.Width - 3)
- {
- this.Collapsed = !this._collapsed;
- }
- }
- }
- #endregion
- #endregion
- #region 公有方法
- /// <summary>
- /// 折叠控件
- /// </summary>
- public void Collapse()
- {
- if (!this._allowCollapse || this._collapsed)
- {
- return;
- }
- if (this.Dock == DockStyle.Left ||
- this.Dock == DockStyle.Right ||
- this.Dock == DockStyle.Fill)
- {
- return;
- }
- GroupBoxCollapsingEventArgs e = new GroupBoxCollapsingEventArgs(true);
- this.OnGroupBoxCollapsing(e);
- if (e.Cancel)
- {
- return;
- }
- this._collapsed = true;
- this.SetText();
- int bottom = this.Bottom;
- this._expandinglHeight = this.Height;
- this._collapsedHeight = this.FontHeight + 3;
- this.Height = this._collapsedHeight;
- if (!this.CollapseLinked)
- {
- return;
- }
- if (this.Parent == null)
- {
- return;
- }
- //if (this._chainReaction == CollapseChainReaction.None ||
- // this.Parent == null)
- //{
- // this.Height = this._collapsedHeight;
- // return;
- //}
- this._adjustHeight = this._expandinglHeight - this._collapsedHeight;
- //if (this._chainReaction == CollapseChainReaction.Parent)
- //{
- // GroupBoxCollapseLinkingEventArgs ep = new GroupBoxCollapseLinkingEventArgs(this.Parent, true);
- // this.OnGroupBoxCollapseLinking(ep);
- // if (ep.Cancel)
- // {
- // return;
- // }
- // if (this.Dock == DockStyle.None ||
- // this.Dock == DockStyle.Top ||
- // this.Dock == DockStyle.Bottom)
- // {
- // this.Height = this._collapsedHeight;
- // }
- // this.Parent.Height -= this._adjustHeight;
- // return;
- //}
- foreach (Control control in this.Parent.Controls)
- {
- if (!control.Visible || control.Top < bottom)
- {
- continue;
- }
- //if (this._chainReaction == CollapseChainReaction.JustBelow &&
- // control.Right > this.Right || control.Left < this.Left)
- //{
- // continue;
- //}
- if (control.Right < this.Left || control.Left > this.Right)
- {
- continue;
- }
- GroupBoxCollapseLinkingEventArgs ec = new GroupBoxCollapseLinkingEventArgs(control, true);
- this.OnGroupBoxCollapseLinking(ec);
- if (ec.Cancel)
- {
- continue;
- }
- if (control.Anchor.HasFlag(AnchorStyles.Top))
- {
- control.Top -= this._adjustHeight;
- if (control.Anchor.HasFlag(AnchorStyles.Bottom))
- {
- control.Height += this._adjustHeight;
- }
- }
- }
- }
- /// <summary>
- /// 展开控件
- /// </summary>
- public void Expand()
- {
- if (!this._allowCollapse || !this._collapsed)
- {
- return;
- }
- if (this.Dock == DockStyle.Left ||
- this.Dock == DockStyle.Right ||
- this.Dock == DockStyle.Fill)
- {
- return;
- }
- GroupBoxCollapsingEventArgs e = new GroupBoxCollapsingEventArgs(false);
- this.OnGroupBoxCollapsing(e);
- if (e.Cancel)
- {
- return;
- }
- this._collapsed = false;
- this.SetText();
- int bottom = this.Bottom;
- this.Height = this._expandinglHeight;
- if (!this.CollapseLinked)
- {
- return;
- }
- if (this.Parent == null)
- {
- return;
- }
- //if (this._chainReaction == CollapseChainReaction.None ||
- // this.Parent == null)
- //{
- // this.Height = this._expandinglHeight;
- // return;
- //}
- //if (this._chainReaction == CollapseChainReaction.Parent)
- //{
- // GroupBoxCollapseLinkingEventArgs ep = new GroupBoxCollapseLinkingEventArgs(this.Parent, false);
- // this.OnGroupBoxCollapseLinking(ep);
- // if (ep.Cancel)
- // {
- // return;
- // }
- // if (this.Dock == DockStyle.None ||
- // this.Dock == DockStyle.Top ||
- // this.Dock == DockStyle.Bottom)
- // {
- // this.Height = this._expandinglHeight;
- // }
- // this.Parent.Height += this._adjustHeight;
- // return;
- //}
- //this.Height = this._expandinglHeight;
- foreach (Control control in this.Parent.Controls)
- {
- if (!control.Visible || control.Top < bottom)
- {
- continue;
- }
- //if (this._chainReaction == CollapseChainReaction.JustBelow &&
- // control.Right > this.Right || control.Left < this.Left)
- //{
- // continue;
- //}
- if (control.Right < this.Left || control.Left > this.Right)
- {
- continue;
- }
- GroupBoxCollapseLinkingEventArgs ec = new GroupBoxCollapseLinkingEventArgs(control, false);
- this.OnGroupBoxCollapseLinking(ec);
- if (ec.Cancel)
- {
- continue;
- }
- if (control.Anchor.HasFlag(AnchorStyles.Top))
- {
- if (control.Anchor.HasFlag(AnchorStyles.Bottom))
- {
- control.Height -= this._adjustHeight;
- }
- control.Top += this._adjustHeight;
- }
- }
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 引发 GroupBoxCollapsing 事件
- /// </summary>
- /// <param name="e">包含事件数据的 GroupBoxCollapsingEventArgs</param>
- protected virtual void OnGroupBoxCollapsing(GroupBoxCollapsingEventArgs e)
- {
- GroupBoxCollapsingEventHandler eventHandler =
- (GroupBoxCollapsingEventHandler)base.Events[EventGroupBoxCollapsing];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- /// <summary>
- /// 引发 GroupBoxCollapseLinking 事件
- /// </summary>
- /// <param name="e">包含事件数据的 GroupBoxCollapseLinkingEventArgs</param>
- protected virtual void OnGroupBoxCollapseLinking(GroupBoxCollapseLinkingEventArgs e)
- {
- GroupBoxCollapseLinkingEventHandler eventHandler =
- (GroupBoxCollapseLinkingEventHandler)base.Events[EventGroupBoxCollapseLinking];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 设置显示文本
- /// </summary>
- private void SetText()
- {
- if (this._allowCollapse)
- {
- if (this._collapsed)
- {
- base.Text = this._text + Constant.S_SPACE + this._collapseMark;
- }
- else
- {
- base.Text = this._text + Constant.S_SPACE + this._expandingMark;
- }
- }
- else
- {
- base.Text = this._text;
- }
- }
- #endregion
- }
- }
|