GrpCollapsible.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. 
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace Dongke.WinForm.Controls
  6. {
  7. /// <summary>
  8. /// 可折叠分组控件,该控件显示围绕一组具有可选标题的控件的框架。
  9. /// </summary>
  10. [ToolboxBitmap(typeof(GroupBox))]
  11. public class GrpCollapsible : GrpGroupBox
  12. {
  13. #region 事件声明
  14. #region GroupBoxCollapsing
  15. /// <summary>
  16. /// 扩展或折叠事件
  17. /// </summary>
  18. private static readonly object EventGroupBoxCollapsing = new object();
  19. /// <summary>
  20. /// GroupBoxCollapsing 事件
  21. /// </summary>
  22. [Description("当 Collapsed 属性的值更改时发生。"), Category("CustomerEx")]
  23. public event GroupBoxCollapsingEventHandler GroupBoxCollapsing
  24. {
  25. add
  26. {
  27. base.Events.AddHandler(EventGroupBoxCollapsing, value);
  28. }
  29. remove
  30. {
  31. base.Events.RemoveHandler(EventGroupBoxCollapsing, value);
  32. }
  33. }
  34. #endregion
  35. #region GroupBoxCollapseLinking
  36. /// <summary>
  37. /// 当 Collapsed 属性的值更改时发生。
  38. /// </summary>
  39. private static readonly object EventGroupBoxCollapseLinking = new object();
  40. /// <summary>
  41. /// 当 Collapsed 属性的值更改时发生。
  42. /// </summary>
  43. [Description("当 Collapsed 属性的值更改时发生。"), Category("CustomerEx")]
  44. public event GroupBoxCollapseLinkingEventHandler GroupBoxCollapseLinking
  45. {
  46. add
  47. {
  48. base.Events.AddHandler(EventGroupBoxCollapseLinking, value);
  49. }
  50. remove
  51. {
  52. base.Events.RemoveHandler(EventGroupBoxCollapseLinking, value);
  53. }
  54. }
  55. #endregion
  56. #endregion
  57. #region 成员变量
  58. /// <summary>
  59. /// 允许折叠
  60. /// </summary>
  61. private bool _allowCollapse = true;
  62. /// <summary>
  63. /// 折叠状态
  64. /// </summary>
  65. private bool _collapsed = false;
  66. /// <summary>
  67. /// 折叠时标志
  68. /// </summary>
  69. private string _collapseMark = Constant.GB_CLOSING_MARK;
  70. /// <summary>
  71. /// 展开时标志
  72. /// </summary>
  73. private string _expandingMark = Constant.GB_OPENING_MARK;
  74. /// <summary>
  75. /// 文本
  76. /// </summary>
  77. private string _text = string.Empty;
  78. /// <summary>
  79. /// 控件折叠前高度
  80. /// </summary>
  81. private int _expandinglHeight = 0;
  82. /// <summary>
  83. /// 控件折叠后高度
  84. /// </summary>
  85. private int _collapsedHeight = 0;
  86. /// <summary>
  87. /// 控件折叠调整高度
  88. /// </summary>
  89. private int _adjustHeight = 0;
  90. ///// <summary>
  91. ///// 折叠连锁反应
  92. ///// </summary>
  93. //private CollapseChainReaction _chainReaction = CollapseChainReaction.Below;
  94. private bool _isSizeSetting = false;
  95. #endregion
  96. #region 构造函数
  97. /// <summary>
  98. /// 可折叠分组控件,该控件显示围绕一组具有可选标题的控件的框架。
  99. /// </summary>
  100. public GrpCollapsible()
  101. {
  102. this.CollapseLinked = true;
  103. }
  104. #endregion
  105. #region 属性
  106. /// <summary>
  107. /// 获取或设置控件是否允许折叠。
  108. /// </summary>
  109. [Description("获取或设置控件是否允许折叠。"), Category("CustomerEx")]
  110. [DefaultValue(true)]
  111. public bool AllowCollapse
  112. {
  113. get
  114. {
  115. return this._allowCollapse;
  116. }
  117. set
  118. {
  119. if (this._allowCollapse != value)
  120. {
  121. this._allowCollapse = value;
  122. this.SetText();
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 获取或设置控件折叠时标志。
  128. /// </summary>
  129. [Description("获取或设置控件折叠时标志。"), Category("CustomerEx")]
  130. [DefaultValue("▲")]
  131. public string CollapseMark
  132. {
  133. get
  134. {
  135. return _collapseMark;
  136. }
  137. set
  138. {
  139. if (this._collapseMark != value)
  140. {
  141. this._collapseMark = value;
  142. this.SetText();
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// 获取或设置控件展开时标志。
  148. /// </summary>
  149. [Description("获取或设置控件展开时标志。"), Category("CustomerEx")]
  150. [DefaultValue("▼")]
  151. public string ExpandingMark
  152. {
  153. get
  154. {
  155. return _expandingMark;
  156. }
  157. set
  158. {
  159. if (this._expandingMark != value)
  160. {
  161. this._expandingMark = value;
  162. this.SetText();
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 获取或设置控件折叠状态。
  168. /// </summary>
  169. [Description("获取或设置控件折叠状态。"), Category("CustomerEx")]
  170. [DefaultValue(false)]
  171. public bool Collapsed
  172. {
  173. get
  174. {
  175. return this._collapsed;
  176. }
  177. set
  178. {
  179. if (this._collapsed != value)
  180. {
  181. if (value)
  182. {
  183. this.Collapse();
  184. }
  185. else
  186. {
  187. this.Expand();
  188. }
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// 获取或设置一个值,该值指示控件在折叠/展开时,其他控件是否连动。
  194. /// </summary>
  195. [Description("获取或设置一个值,该值指示控件在折叠/展开时,其他控件是否连动。"), Category("CustomerEx")]
  196. [DefaultValue(true)]
  197. public bool CollapseLinked
  198. {
  199. get;
  200. set;
  201. }
  202. ///// <summary>
  203. ///// 获取或设置控件在折叠/展开时,其他控件的动作。
  204. ///// </summary>
  205. //[Description("获取或设置控件在折叠/展开时,其他控件的动作。"), Category("CustomerEx")]
  206. //[DefaultValue(typeof(CollapseChainReaction), "Below")]
  207. //public CollapseChainReaction ChainReaction
  208. //{
  209. // get
  210. // {
  211. // return this._chainReaction;
  212. // }
  213. // set
  214. // {
  215. // if (this._chainReaction != value)
  216. // {
  217. // this._chainReaction = value;
  218. // }
  219. // }
  220. //}
  221. #endregion
  222. #region 重写属性
  223. /// <summary>
  224. /// 获取和设置文本
  225. /// </summary>
  226. public new string Text
  227. {
  228. get
  229. {
  230. return this._text;
  231. }
  232. set
  233. {
  234. if (this._text != value)
  235. {
  236. this._text = value;
  237. this.SetText();
  238. }
  239. }
  240. }
  241. #endregion
  242. #region 重写事件
  243. #region 属性改变
  244. /// <summary>
  245. /// 改变控件大小
  246. /// </summary>
  247. /// <param name="e"></param>
  248. protected override void OnSizeChanged(System.EventArgs e)
  249. {
  250. if (this._isSizeSetting)
  251. {
  252. return;
  253. }
  254. if (this._collapsed && this.Height != this._collapsedHeight)
  255. {
  256. this._isSizeSetting = true;
  257. this.Height = this._collapsedHeight;
  258. this._isSizeSetting = false;
  259. return;
  260. }
  261. base.OnSizeChanged(e);
  262. }
  263. /// <summary>
  264. /// 鼠标点击,折叠或展开
  265. /// </summary>
  266. /// <param name="e"></param>
  267. protected override void OnMouseClick(MouseEventArgs e)
  268. {
  269. base.OnMouseClick(e);
  270. if (this._allowCollapse && e.Button == MouseButtons.Left)
  271. {
  272. if (0 < e.Y && e.Y < this.FontHeight && 3 < e.X && e.X < this.Width - 3)
  273. {
  274. this.Collapsed = !this._collapsed;
  275. }
  276. }
  277. }
  278. #endregion
  279. #endregion
  280. #region 公有方法
  281. /// <summary>
  282. /// 折叠控件
  283. /// </summary>
  284. public void Collapse()
  285. {
  286. if (!this._allowCollapse || this._collapsed)
  287. {
  288. return;
  289. }
  290. if (this.Dock == DockStyle.Left ||
  291. this.Dock == DockStyle.Right ||
  292. this.Dock == DockStyle.Fill)
  293. {
  294. return;
  295. }
  296. GroupBoxCollapsingEventArgs e = new GroupBoxCollapsingEventArgs(true);
  297. this.OnGroupBoxCollapsing(e);
  298. if (e.Cancel)
  299. {
  300. return;
  301. }
  302. this._collapsed = true;
  303. this.SetText();
  304. int bottom = this.Bottom;
  305. this._expandinglHeight = this.Height;
  306. this._collapsedHeight = this.FontHeight + 3;
  307. this.Height = this._collapsedHeight;
  308. if (!this.CollapseLinked)
  309. {
  310. return;
  311. }
  312. if (this.Parent == null)
  313. {
  314. return;
  315. }
  316. //if (this._chainReaction == CollapseChainReaction.None ||
  317. // this.Parent == null)
  318. //{
  319. // this.Height = this._collapsedHeight;
  320. // return;
  321. //}
  322. this._adjustHeight = this._expandinglHeight - this._collapsedHeight;
  323. //if (this._chainReaction == CollapseChainReaction.Parent)
  324. //{
  325. // GroupBoxCollapseLinkingEventArgs ep = new GroupBoxCollapseLinkingEventArgs(this.Parent, true);
  326. // this.OnGroupBoxCollapseLinking(ep);
  327. // if (ep.Cancel)
  328. // {
  329. // return;
  330. // }
  331. // if (this.Dock == DockStyle.None ||
  332. // this.Dock == DockStyle.Top ||
  333. // this.Dock == DockStyle.Bottom)
  334. // {
  335. // this.Height = this._collapsedHeight;
  336. // }
  337. // this.Parent.Height -= this._adjustHeight;
  338. // return;
  339. //}
  340. foreach (Control control in this.Parent.Controls)
  341. {
  342. if (!control.Visible || control.Top < bottom)
  343. {
  344. continue;
  345. }
  346. //if (this._chainReaction == CollapseChainReaction.JustBelow &&
  347. // control.Right > this.Right || control.Left < this.Left)
  348. //{
  349. // continue;
  350. //}
  351. if (control.Right < this.Left || control.Left > this.Right)
  352. {
  353. continue;
  354. }
  355. GroupBoxCollapseLinkingEventArgs ec = new GroupBoxCollapseLinkingEventArgs(control, true);
  356. this.OnGroupBoxCollapseLinking(ec);
  357. if (ec.Cancel)
  358. {
  359. continue;
  360. }
  361. if (control.Anchor.HasFlag(AnchorStyles.Top))
  362. {
  363. control.Top -= this._adjustHeight;
  364. if (control.Anchor.HasFlag(AnchorStyles.Bottom))
  365. {
  366. control.Height += this._adjustHeight;
  367. }
  368. }
  369. }
  370. }
  371. /// <summary>
  372. /// 展开控件
  373. /// </summary>
  374. public void Expand()
  375. {
  376. if (!this._allowCollapse || !this._collapsed)
  377. {
  378. return;
  379. }
  380. if (this.Dock == DockStyle.Left ||
  381. this.Dock == DockStyle.Right ||
  382. this.Dock == DockStyle.Fill)
  383. {
  384. return;
  385. }
  386. GroupBoxCollapsingEventArgs e = new GroupBoxCollapsingEventArgs(false);
  387. this.OnGroupBoxCollapsing(e);
  388. if (e.Cancel)
  389. {
  390. return;
  391. }
  392. this._collapsed = false;
  393. this.SetText();
  394. int bottom = this.Bottom;
  395. this.Height = this._expandinglHeight;
  396. if (!this.CollapseLinked)
  397. {
  398. return;
  399. }
  400. if (this.Parent == null)
  401. {
  402. return;
  403. }
  404. //if (this._chainReaction == CollapseChainReaction.None ||
  405. // this.Parent == null)
  406. //{
  407. // this.Height = this._expandinglHeight;
  408. // return;
  409. //}
  410. //if (this._chainReaction == CollapseChainReaction.Parent)
  411. //{
  412. // GroupBoxCollapseLinkingEventArgs ep = new GroupBoxCollapseLinkingEventArgs(this.Parent, false);
  413. // this.OnGroupBoxCollapseLinking(ep);
  414. // if (ep.Cancel)
  415. // {
  416. // return;
  417. // }
  418. // if (this.Dock == DockStyle.None ||
  419. // this.Dock == DockStyle.Top ||
  420. // this.Dock == DockStyle.Bottom)
  421. // {
  422. // this.Height = this._expandinglHeight;
  423. // }
  424. // this.Parent.Height += this._adjustHeight;
  425. // return;
  426. //}
  427. //this.Height = this._expandinglHeight;
  428. foreach (Control control in this.Parent.Controls)
  429. {
  430. if (!control.Visible || control.Top < bottom)
  431. {
  432. continue;
  433. }
  434. //if (this._chainReaction == CollapseChainReaction.JustBelow &&
  435. // control.Right > this.Right || control.Left < this.Left)
  436. //{
  437. // continue;
  438. //}
  439. if (control.Right < this.Left || control.Left > this.Right)
  440. {
  441. continue;
  442. }
  443. GroupBoxCollapseLinkingEventArgs ec = new GroupBoxCollapseLinkingEventArgs(control, false);
  444. this.OnGroupBoxCollapseLinking(ec);
  445. if (ec.Cancel)
  446. {
  447. continue;
  448. }
  449. if (control.Anchor.HasFlag(AnchorStyles.Top))
  450. {
  451. if (control.Anchor.HasFlag(AnchorStyles.Bottom))
  452. {
  453. control.Height -= this._adjustHeight;
  454. }
  455. control.Top += this._adjustHeight;
  456. }
  457. }
  458. }
  459. #endregion
  460. #region 保护方法
  461. /// <summary>
  462. /// 引发 GroupBoxCollapsing 事件
  463. /// </summary>
  464. /// <param name="e">包含事件数据的 GroupBoxCollapsingEventArgs</param>
  465. protected virtual void OnGroupBoxCollapsing(GroupBoxCollapsingEventArgs e)
  466. {
  467. GroupBoxCollapsingEventHandler eventHandler =
  468. (GroupBoxCollapsingEventHandler)base.Events[EventGroupBoxCollapsing];
  469. if (eventHandler != null)
  470. {
  471. eventHandler(this, e);
  472. }
  473. }
  474. /// <summary>
  475. /// 引发 GroupBoxCollapseLinking 事件
  476. /// </summary>
  477. /// <param name="e">包含事件数据的 GroupBoxCollapseLinkingEventArgs</param>
  478. protected virtual void OnGroupBoxCollapseLinking(GroupBoxCollapseLinkingEventArgs e)
  479. {
  480. GroupBoxCollapseLinkingEventHandler eventHandler =
  481. (GroupBoxCollapseLinkingEventHandler)base.Events[EventGroupBoxCollapseLinking];
  482. if (eventHandler != null)
  483. {
  484. eventHandler(this, e);
  485. }
  486. }
  487. #endregion
  488. #region 私有方法
  489. /// <summary>
  490. /// 设置显示文本
  491. /// </summary>
  492. private void SetText()
  493. {
  494. if (this._allowCollapse)
  495. {
  496. if (this._collapsed)
  497. {
  498. base.Text = this._text + Constant.S_SPACE + this._collapseMark;
  499. }
  500. else
  501. {
  502. base.Text = this._text + Constant.S_SPACE + this._expandingMark;
  503. }
  504. }
  505. else
  506. {
  507. base.Text = this._text;
  508. }
  509. }
  510. #endregion
  511. }
  512. }