/******************************************************************************* * 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 控件实例化 /// /// 控件实例化 /// 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控件属性 /// /// 原始高度属性 /// [Browsable(false)] [Description("dkGroupBox控件原始高度的设定和取得。")] public int OriginalHeight { get { return _originalHeight; } set { _originalHeight = value; } } /// /// 控件是否展开属性 /// [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(); } } } } } /// /// 控件可否展开属性 /// [DefaultValue(true)] public bool CanExpand { get { return _canExpand; } set { _canExpand = value; } } /// /// 控件高度差属性 /// [Bindable(false)] [DefaultValue(0)] [Description("dkGroupBox控件的高度差的取得。")] public int DifferenceHeight { get { return _differenceHeight; } } /// /// 控件是否展开属性 /// [Browsable(false)] [DefaultValue(true)] public bool IsExpanding { get { return _isExpanding; } } /// /// 控件收缩时图标属性 /// [Browsable(true)] [DefaultValue(ControlsConst.CLOSING_MARK)] [Description("dkGroupBox控件关闭时图标的设定和取得。")] public string ClosingMark { get { return _closingMark; } set { _closingMark = value; } } /// /// 控件展开时图标属性 /// [Browsable(true)] [DefaultValue(ControlsConst.OPENING_MARK)] [Description("dkGroupBox控件展开时图标的设定和取得。")] public string OpeningMark { get { return _openingMark; } set { _openingMark = value; } } /// /// 控件所属分组控件属性 /// [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; } /// /// 删除右侧字符 /// /// 源字符串 /// 目标字符串 /// 结果字符串 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); } } } } /// /// 控件展开收缩处理 /// /// 展开属性 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 私有方法 } }