| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:C_GroupBox.cs
- * 2.功能描述:扩展的控件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/08/13 1.00 新建
- *******************************************************************************/
- using System;
- using System.ComponentModel;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.Library;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- public partial class C_GroupBox : DKGroupBox
- {
- #region 变量定义
- private bool _isExpand = true; // 展开关闭标识
- private bool _canExpand = true;
- private string _closingMark = ControlsConst.CLOSING_MARK; // 关闭时的图标
- private string _openingMark = ControlsConst.OPENING_MARK; // 展开时的图标
- private bool _isExpanding = true; // GroupBox展开中标识
- private int _originalHeight = -1; // 控件原始高度
- private int _differenceHeight = 0; // 计算高度差
- private bool _isAdjustingHeaderText = false;
- private bool _originalExpand = true; // 原始展开状态
- #endregion
- #region 控件实例化
- /// <summary>
- /// 控件实例化
- /// </summary>
- public C_GroupBox()
- {
- InitializeComponent();
- this.TextChanged += new EventHandler(dkGroupBox_TextChanged);
- this.MouseClick += new MouseEventHandler(dkGroupBox_MouseClick);
- this.BackColor = System.Drawing.Color.Transparent;
- }
- #endregion
- #region dkGroupBox控件属性
- /// <summary>
- /// 原始高度属性
- /// </summary>
- [Browsable(false)]
- [Description("dkGroupBox控件原始高度的设定和取得。")]
- public int OriginalHeight
- {
- get
- {
- return _originalHeight;
- }
- set
- {
- _originalHeight = value;
- }
- }
- /// <summary>
- /// 控件是否展开属性
- /// </summary>
- [Bindable(true)]
- [DefaultValue(true)]
- [Description("dkGroupBox控件是否是展开状态的设定和取得。")]
- public bool IsExpand
- {
- get
- {
- return _isExpand;
- }
- set
- {
- if (_isExpand != value)
- {
- _isExpand = value;
- if (!_isExpand)
- {
- _originalExpand = _isExpanding;
- if (!_isExpanding)
- {
- this.Switch(true);
- }
- else
- {
- AdjustHeaderText();
- }
- }
- else
- {
- if (_originalExpand != _isExpanding)
- {
- this.Switch(_originalExpand);
- }
- else
- {
- AdjustHeaderText();
- }
- }
- }
- }
- }
- /// <summary>
- /// 控件可否展开属性
- /// </summary>
- [DefaultValue(true)]
- public bool CanExpand
- {
- get
- {
- return _canExpand;
- }
- set
- {
- _canExpand = value;
- }
- }
- /// <summary>
- /// 控件高度差属性
- /// </summary>
- [Bindable(false)]
- [DefaultValue(0)]
- [Description("dkGroupBox控件的高度差的取得。")]
- public int DifferenceHeight
- {
- get
- {
- return _differenceHeight;
- }
- }
- /// <summary>
- /// 控件是否展开属性
- /// </summary>
- [Browsable(false)]
- [DefaultValue(true)]
- public bool IsExpanding
- {
- get
- {
- return _isExpanding;
- }
- }
- /// <summary>
- /// 控件收缩时图标属性
- /// </summary>
- [Browsable(true)]
- [DefaultValue(ControlsConst.CLOSING_MARK)]
- [Description("dkGroupBox控件关闭时图标的设定和取得。")]
- public string ClosingMark
- {
- get
- {
- return _closingMark;
- }
- set
- {
- _closingMark = value;
- }
- }
- /// <summary>
- /// 控件展开时图标属性
- /// </summary>
- [Browsable(true)]
- [DefaultValue(ControlsConst.OPENING_MARK)]
- [Description("dkGroupBox控件展开时图标的设定和取得。")]
- public string OpeningMark
- {
- get
- {
- return _openingMark;
- }
- set
- {
- _openingMark = value;
- }
- }
- /// <summary>
- /// 控件所属分组控件属性
- /// </summary>
- [Browsable(true)]
- [DefaultValue(null)]
- [Description("dkGroupBox控件所属的RadioButton组的值的设定和取得。")]
- public Object RadioBindingValue
- {
- get
- {
- foreach (Control control in this.Controls)
- {
- RadioButton radioButton = control as RadioButton;
- if (radioButton != null)
- {
- if (radioButton.Checked)
- {
- return radioButton.Tag;
- }
- }
- }
- return null;
- }
- set
- {
- if (Utility.IsNull(value))
- {
- return;
- }
- foreach (Control control in this.Controls)
- {
- RadioButton radioButton = control as RadioButton;
- if (radioButton != null)
- {
- if (radioButton.Tag != null)
- {
- if (value.ToString().Equals(radioButton.Tag.ToString()))
- {
- radioButton.Checked = true;
- return;
- }
- }
- }
- }
- }
- }
- public void CloseExpand()
- {
- this.Switch(false);
- }
- #endregion
- #region 受保护的方法
- protected void AdjustHeaderText()
- {
- if (_isAdjustingHeaderText)
- {
- return;
- }
- _isAdjustingHeaderText = true;
- this.Text = DeleteRightString(this.Text, ClosingMark);
- this.Text = DeleteRightString(this.Text, OpeningMark);
- if (_isExpand)
- {
- // 控件打开的情况
- if (_isExpanding)
- {
- // 控件打开的情况
- this.Text = string.Format("{0}{1}{2}"
- , this.Text
- , ControlsConst.SPACE_MARK
- , OpeningMark);
- }
- else
- {
- // 控件打开的情况
- this.Text = string.Format("{0}{1}{2}"
- , this.Text
- , ControlsConst.SPACE_MARK
- , ClosingMark);
- }
- }
- _isAdjustingHeaderText = false;
- }
- /// <summary>
- /// 删除右侧字符
- /// </summary>
- /// <param name="source">源字符串</param>
- /// <param name="search">目标字符串</param>
- /// <returns>结果字符串</returns>
- protected string DeleteRightString(string source, string search)
- {
- if (IsMatchRightString(source, search))
- {
- source = source.Substring(0,
- source.Length - search.Length - ControlsConst.SPACE_MARK.Length);
- }
- return source;
- }
- protected bool IsMatchRightString(string source, string search)
- {
- if (string.IsNullOrEmpty(source))
- {
- return false;
- }
- if (string.IsNullOrEmpty(search))
- {
- return false;
- }
- if (ControlsConst.SPACE_MARK.Length + search.Length <= source.Length)
- {
- if (source.LastIndexOf(string.Format("{0}{1}", ControlsConst.SPACE_MARK, search))
- == source.Length - search.Length - ControlsConst.SPACE_MARK.Length)
- {
- return true;
- }
- }
- return false;
- }
- #endregion 受保护的方法
- #region 私有方法
- private void dkGroupBox_TextChanged(Object sender, EventArgs e)
- {
- AdjustHeaderText();
- }
- private void dkGroupBox_MouseClick(Object sender, MouseEventArgs e)
- {
- if (this._isExpand
- && this.CanExpand)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (0 < e.Y && e.Y < this.FontHeight)
- {
- this.Switch(!this.IsExpanding);
- }
- }
- }
- }
- /// <summary>
- /// 控件展开收缩处理
- /// </summary>
- /// <param name="isExpand">展开属性</param>
- private void Switch(bool isExpand)
- {
- if (!_isExpand && _isExpanding)
- {
- return;
- }
- if (isExpand)
- {
- // 处理展开情况
- if (0 < _originalHeight)
- {
- // 原始高度被记住的情况,恢复原来的高度
- this._differenceHeight = _originalHeight - this.Height;
- this.Height = _originalHeight;
- this._isExpanding = true;
- if (this.Parent != null)
- {
- int baseY = this.Parent.Controls[this.Name].Top;
- foreach (Control control in this.Parent.Controls)
- {
- if (baseY < control.Top
- && !(control.Right < this.Left || this.Right < control.Left)
- && !"NotFollow".Equals(control.Tag))
- {
- if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
- {
- control.Top += this._differenceHeight;
- }
- if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
- {
- if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
- {
- control.Height -= this._differenceHeight;
- }
- }
- }
- }
- }
- }
- }
- else
- {
- _originalHeight = this.Height;
- this._differenceHeight = _originalHeight - this.FontHeight - 1;
- this.Height = this.FontHeight + 1;
- this._isExpanding = false;
- if (this.Parent != null)
- {
- int baseY = this.Parent.Controls[this.Name].Top + _originalHeight;
- foreach (Control control in this.Parent.Controls)
- {
- if (baseY < control.Top
- && !(control.Right < this.Left || this.Right < control.Left)
- && !"NotFollow".Equals(control.Tag))
- {
- if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
- {
- control.Top -= this._differenceHeight;
- }
- if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
- {
- if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
- {
- control.Height += this._differenceHeight;
- }
- }
- }
- }
- }
- }
- AdjustHeaderText();
- }
- #endregion 私有方法
- }
- }
|