C_GroupBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:C_GroupBox.cs
  5. * 2.功能描述:扩展的控件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Windows.Forms;
  13. using Dongke.IBOSS.PRD.Basics.Library;
  14. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  15. {
  16. public partial class C_GroupBox : DKGroupBox
  17. {
  18. #region 变量定义
  19. private bool _isExpand = true; // 展开关闭标识
  20. private bool _canExpand = true;
  21. private string _closingMark = ControlsConst.CLOSING_MARK; // 关闭时的图标
  22. private string _openingMark = ControlsConst.OPENING_MARK; // 展开时的图标
  23. private bool _isExpanding = true; // GroupBox展开中标识
  24. private int _originalHeight = -1; // 控件原始高度
  25. private int _differenceHeight = 0; // 计算高度差
  26. private bool _isAdjustingHeaderText = false;
  27. private bool _originalExpand = true; // 原始展开状态
  28. #endregion
  29. #region 控件实例化
  30. /// <summary>
  31. /// 控件实例化
  32. /// </summary>
  33. public C_GroupBox()
  34. {
  35. InitializeComponent();
  36. this.TextChanged += new EventHandler(dkGroupBox_TextChanged);
  37. this.MouseClick += new MouseEventHandler(dkGroupBox_MouseClick);
  38. this.BackColor = System.Drawing.Color.Transparent;
  39. }
  40. #endregion
  41. #region dkGroupBox控件属性
  42. /// <summary>
  43. /// 原始高度属性
  44. /// </summary>
  45. [Browsable(false)]
  46. [Description("dkGroupBox控件原始高度的设定和取得。")]
  47. public int OriginalHeight
  48. {
  49. get
  50. {
  51. return _originalHeight;
  52. }
  53. set
  54. {
  55. _originalHeight = value;
  56. }
  57. }
  58. /// <summary>
  59. /// 控件是否展开属性
  60. /// </summary>
  61. [Bindable(true)]
  62. [DefaultValue(true)]
  63. [Description("dkGroupBox控件是否是展开状态的设定和取得。")]
  64. public bool IsExpand
  65. {
  66. get
  67. {
  68. return _isExpand;
  69. }
  70. set
  71. {
  72. if (_isExpand != value)
  73. {
  74. _isExpand = value;
  75. if (!_isExpand)
  76. {
  77. _originalExpand = _isExpanding;
  78. if (!_isExpanding)
  79. {
  80. this.Switch(true);
  81. }
  82. else
  83. {
  84. AdjustHeaderText();
  85. }
  86. }
  87. else
  88. {
  89. if (_originalExpand != _isExpanding)
  90. {
  91. this.Switch(_originalExpand);
  92. }
  93. else
  94. {
  95. AdjustHeaderText();
  96. }
  97. }
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 控件可否展开属性
  103. /// </summary>
  104. [DefaultValue(true)]
  105. public bool CanExpand
  106. {
  107. get
  108. {
  109. return _canExpand;
  110. }
  111. set
  112. {
  113. _canExpand = value;
  114. }
  115. }
  116. /// <summary>
  117. /// 控件高度差属性
  118. /// </summary>
  119. [Bindable(false)]
  120. [DefaultValue(0)]
  121. [Description("dkGroupBox控件的高度差的取得。")]
  122. public int DifferenceHeight
  123. {
  124. get
  125. {
  126. return _differenceHeight;
  127. }
  128. }
  129. /// <summary>
  130. /// 控件是否展开属性
  131. /// </summary>
  132. [Browsable(false)]
  133. [DefaultValue(true)]
  134. public bool IsExpanding
  135. {
  136. get
  137. {
  138. return _isExpanding;
  139. }
  140. }
  141. /// <summary>
  142. /// 控件收缩时图标属性
  143. /// </summary>
  144. [Browsable(true)]
  145. [DefaultValue(ControlsConst.CLOSING_MARK)]
  146. [Description("dkGroupBox控件关闭时图标的设定和取得。")]
  147. public string ClosingMark
  148. {
  149. get
  150. {
  151. return _closingMark;
  152. }
  153. set
  154. {
  155. _closingMark = value;
  156. }
  157. }
  158. /// <summary>
  159. /// 控件展开时图标属性
  160. /// </summary>
  161. [Browsable(true)]
  162. [DefaultValue(ControlsConst.OPENING_MARK)]
  163. [Description("dkGroupBox控件展开时图标的设定和取得。")]
  164. public string OpeningMark
  165. {
  166. get
  167. {
  168. return _openingMark;
  169. }
  170. set
  171. {
  172. _openingMark = value;
  173. }
  174. }
  175. /// <summary>
  176. /// 控件所属分组控件属性
  177. /// </summary>
  178. [Browsable(true)]
  179. [DefaultValue(null)]
  180. [Description("dkGroupBox控件所属的RadioButton组的值的设定和取得。")]
  181. public Object RadioBindingValue
  182. {
  183. get
  184. {
  185. foreach (Control control in this.Controls)
  186. {
  187. RadioButton radioButton = control as RadioButton;
  188. if (radioButton != null)
  189. {
  190. if (radioButton.Checked)
  191. {
  192. return radioButton.Tag;
  193. }
  194. }
  195. }
  196. return null;
  197. }
  198. set
  199. {
  200. if (Utility.IsNull(value))
  201. {
  202. return;
  203. }
  204. foreach (Control control in this.Controls)
  205. {
  206. RadioButton radioButton = control as RadioButton;
  207. if (radioButton != null)
  208. {
  209. if (radioButton.Tag != null)
  210. {
  211. if (value.ToString().Equals(radioButton.Tag.ToString()))
  212. {
  213. radioButton.Checked = true;
  214. return;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. public void CloseExpand()
  222. {
  223. this.Switch(false);
  224. }
  225. #endregion
  226. #region 受保护的方法
  227. protected void AdjustHeaderText()
  228. {
  229. if (_isAdjustingHeaderText)
  230. {
  231. return;
  232. }
  233. _isAdjustingHeaderText = true;
  234. this.Text = DeleteRightString(this.Text, ClosingMark);
  235. this.Text = DeleteRightString(this.Text, OpeningMark);
  236. if (_isExpand)
  237. {
  238. // 控件打开的情况
  239. if (_isExpanding)
  240. {
  241. // 控件打开的情况
  242. this.Text = string.Format("{0}{1}{2}"
  243. , this.Text
  244. , ControlsConst.SPACE_MARK
  245. , OpeningMark);
  246. }
  247. else
  248. {
  249. // 控件打开的情况
  250. this.Text = string.Format("{0}{1}{2}"
  251. , this.Text
  252. , ControlsConst.SPACE_MARK
  253. , ClosingMark);
  254. }
  255. }
  256. _isAdjustingHeaderText = false;
  257. }
  258. /// <summary>
  259. /// 删除右侧字符
  260. /// </summary>
  261. /// <param name="source">源字符串</param>
  262. /// <param name="search">目标字符串</param>
  263. /// <returns>结果字符串</returns>
  264. protected string DeleteRightString(string source, string search)
  265. {
  266. if (IsMatchRightString(source, search))
  267. {
  268. source = source.Substring(0,
  269. source.Length - search.Length - ControlsConst.SPACE_MARK.Length);
  270. }
  271. return source;
  272. }
  273. protected bool IsMatchRightString(string source, string search)
  274. {
  275. if (string.IsNullOrEmpty(source))
  276. {
  277. return false;
  278. }
  279. if (string.IsNullOrEmpty(search))
  280. {
  281. return false;
  282. }
  283. if (ControlsConst.SPACE_MARK.Length + search.Length <= source.Length)
  284. {
  285. if (source.LastIndexOf(string.Format("{0}{1}", ControlsConst.SPACE_MARK, search))
  286. == source.Length - search.Length - ControlsConst.SPACE_MARK.Length)
  287. {
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. #endregion 受保护的方法
  294. #region 私有方法
  295. private void dkGroupBox_TextChanged(Object sender, EventArgs e)
  296. {
  297. AdjustHeaderText();
  298. }
  299. private void dkGroupBox_MouseClick(Object sender, MouseEventArgs e)
  300. {
  301. if (this._isExpand
  302. && this.CanExpand)
  303. {
  304. if (e.Button == MouseButtons.Left)
  305. {
  306. if (0 < e.Y && e.Y < this.FontHeight)
  307. {
  308. this.Switch(!this.IsExpanding);
  309. }
  310. }
  311. }
  312. }
  313. /// <summary>
  314. /// 控件展开收缩处理
  315. /// </summary>
  316. /// <param name="isExpand">展开属性</param>
  317. private void Switch(bool isExpand)
  318. {
  319. if (!_isExpand && _isExpanding)
  320. {
  321. return;
  322. }
  323. if (isExpand)
  324. {
  325. // 处理展开情况
  326. if (0 < _originalHeight)
  327. {
  328. // 原始高度被记住的情况,恢复原来的高度
  329. this._differenceHeight = _originalHeight - this.Height;
  330. this.Height = _originalHeight;
  331. this._isExpanding = true;
  332. if (this.Parent != null)
  333. {
  334. int baseY = this.Parent.Controls[this.Name].Top;
  335. foreach (Control control in this.Parent.Controls)
  336. {
  337. if (baseY < control.Top
  338. && !(control.Right < this.Left || this.Right < control.Left)
  339. && !"NotFollow".Equals(control.Tag))
  340. {
  341. if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
  342. {
  343. control.Top += this._differenceHeight;
  344. }
  345. if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
  346. {
  347. if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
  348. {
  349. control.Height -= this._differenceHeight;
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. else
  358. {
  359. _originalHeight = this.Height;
  360. this._differenceHeight = _originalHeight - this.FontHeight - 1;
  361. this.Height = this.FontHeight + 1;
  362. this._isExpanding = false;
  363. if (this.Parent != null)
  364. {
  365. int baseY = this.Parent.Controls[this.Name].Top + _originalHeight;
  366. foreach (Control control in this.Parent.Controls)
  367. {
  368. if (baseY < control.Top
  369. && !(control.Right < this.Left || this.Right < control.Left)
  370. && !"NotFollow".Equals(control.Tag))
  371. {
  372. if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
  373. {
  374. control.Top -= this._differenceHeight;
  375. }
  376. if ((control.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom)
  377. {
  378. if ((control.Anchor & AnchorStyles.Top) == AnchorStyles.Top)
  379. {
  380. control.Height += this._differenceHeight;
  381. }
  382. }
  383. }
  384. }
  385. }
  386. }
  387. AdjustHeaderText();
  388. }
  389. #endregion 私有方法
  390. }
  391. }