UCMindMapping.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-11
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCMindMapping.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCMindMapping.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. internal class UCMindMapping : UserControl
  32. {
  33. /// <summary>
  34. /// The item context menu strip
  35. /// </summary>
  36. private ContextMenuStrip itemContextMenuStrip;
  37. /// <summary>
  38. /// Gets or sets the item context menu strip.
  39. /// </summary>
  40. /// <value>The item context menu strip.</value>
  41. [Description("节点关联的右键菜单"), Category("自定义")]
  42. public ContextMenuStrip ItemContextMenuStrip
  43. {
  44. get { return itemContextMenuStrip; }
  45. set { itemContextMenuStrip = value; }
  46. }
  47. /// <summary>
  48. /// The item backcolor
  49. /// </summary>
  50. private Color itemBackcolor = Color.FromArgb(255, 77, 59);
  51. /// <summary>
  52. /// Gets or sets the item backcolor.
  53. /// </summary>
  54. /// <value>The item backcolor.</value>
  55. [Description("节点背景色,优先级小于数据源中设置的背景色"), Category("自定义")]
  56. public Color ItemBackcolor
  57. {
  58. get { return itemBackcolor; }
  59. set
  60. {
  61. itemBackcolor = value;
  62. Refresh();
  63. }
  64. }
  65. /// <summary>
  66. /// The line color
  67. /// </summary>
  68. private Color lineColor = Color.Black;
  69. /// <summary>
  70. /// Gets or sets the color of the line.
  71. /// </summary>
  72. /// <value>The color of the line.</value>
  73. [Description("线条颜色"), Category("自定义")]
  74. public Color LineColor
  75. {
  76. get { return lineColor; }
  77. set
  78. {
  79. lineColor = value;
  80. Refresh();
  81. }
  82. }
  83. /// <summary>
  84. /// The split width
  85. /// </summary>
  86. private int splitWidth = 50;
  87. // private int itemHeight = 20;
  88. /// <summary>
  89. /// The padding
  90. /// </summary>
  91. private int padding = 20;
  92. /// <summary>
  93. /// The m rect working
  94. /// </summary>
  95. Rectangle m_rectWorking = Rectangle.Empty;
  96. /// <summary>
  97. /// Occurs when [item clicked].
  98. /// </summary>
  99. public event EventHandler ItemClicked;
  100. /// <summary>
  101. /// The data source
  102. /// </summary>
  103. private MindMappingItemEntity dataSource;
  104. /// <summary>
  105. /// The select entity
  106. /// </summary>
  107. private MindMappingItemEntity selectEntity;
  108. /// <summary>
  109. /// Gets the select entity.
  110. /// </summary>
  111. /// <value>The select entity.</value>
  112. [Description("选中的数据源"), Category("自定义")]
  113. public MindMappingItemEntity SelectEntity
  114. {
  115. get { return selectEntity; }
  116. private set { selectEntity = value; }
  117. }
  118. /// <summary>
  119. /// Gets or sets the data source.
  120. /// </summary>
  121. /// <value>The data source.</value>
  122. [Description("数据源"), Category("自定义")]
  123. public MindMappingItemEntity DataSource
  124. {
  125. get { return dataSource; }
  126. set
  127. {
  128. dataSource = value;
  129. ResetSize();
  130. }
  131. }
  132. /// <summary>
  133. /// Initializes a new instance of the <see cref="UCMindMapping" /> class.
  134. /// </summary>
  135. public UCMindMapping()
  136. {
  137. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  138. this.SetStyle(ControlStyles.DoubleBuffer, true);
  139. this.SetStyle(ControlStyles.ResizeRedraw, true);
  140. this.SetStyle(ControlStyles.Selectable, true);
  141. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  142. this.SetStyle(ControlStyles.UserPaint, true);
  143. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  144. this.Click += UCMindMapping_Click;
  145. this.DoubleClick += UCMindMapping_DoubleClick;
  146. this.Load += UCMindMapping_Load;
  147. this.MouseClick += UCMindMapping_MouseClick;
  148. }
  149. /// <summary>
  150. /// Handles the Load event of the UCMindMapping control.
  151. /// </summary>
  152. /// <param name="sender">The source of the event.</param>
  153. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  154. void UCMindMapping_Load(object sender, EventArgs e)
  155. {
  156. if (this.Parent != null)
  157. {
  158. //父控件大小改变时重置大小和位置
  159. this.Parent.SizeChanged += (a, b) =>
  160. {
  161. ResetSize();
  162. };
  163. }
  164. }
  165. /// <summary>
  166. /// 双击处理,主要用于检测节点双击展开折叠
  167. /// </summary>
  168. /// <param name="sender">The source of the event.</param>
  169. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  170. void UCMindMapping_DoubleClick(object sender, EventArgs e)
  171. {
  172. var mouseLocation = this.PointToClient(Control.MousePosition);
  173. bool bln = CheckDrawRectClick(dataSource, mouseLocation);
  174. if (bln)
  175. {
  176. ResetSize();
  177. this.Parent.Refresh();
  178. }
  179. }
  180. /// <summary>
  181. /// 单击处理,主要用于单击节点事件和,展开关闭圆圈处理
  182. /// </summary>
  183. /// <param name="sender">The source of the event.</param>
  184. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  185. void UCMindMapping_Click(object sender, EventArgs e)
  186. {
  187. var mouseLocation = this.PointToClient(Control.MousePosition);
  188. bool bln = CheckExpansionClick(dataSource, mouseLocation);
  189. if (bln)
  190. {
  191. ResetSize();
  192. this.Parent.Refresh();
  193. }
  194. }
  195. /// <summary>
  196. /// Handles the MouseClick event of the UCMindMapping control.
  197. /// </summary>
  198. /// <param name="sender">The source of the event.</param>
  199. /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
  200. void UCMindMapping_MouseClick(object sender, MouseEventArgs e)
  201. {
  202. if (itemContextMenuStrip != null && e.Button == System.Windows.Forms.MouseButtons.Right)
  203. {
  204. bool bln = CheckDrawRectClick(dataSource, e.Location, false);
  205. if (bln)
  206. {
  207. itemContextMenuStrip.Show(this.PointToScreen(e.Location));
  208. }
  209. }
  210. }
  211. /// <summary>
  212. /// 双击检查
  213. /// </summary>
  214. /// <param name="item">The item.</param>
  215. /// <param name="mouseLocation">The mouse location.</param>
  216. /// <param name="blnDoExpansion">if set to <c>true</c> [BLN do expansion].</param>
  217. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  218. private bool CheckDrawRectClick(MindMappingItemEntity item, Point mouseLocation, bool blnDoExpansion = true)
  219. {
  220. if (item == null)
  221. return false;
  222. else
  223. {
  224. if (item.DrawRectangle.Contains(mouseLocation))
  225. {
  226. if (blnDoExpansion)
  227. item.IsExpansion = !item.IsExpansion;
  228. return true;
  229. }
  230. if (item.Childrens != null && item.Childrens.Length > 0)
  231. {
  232. foreach (var child in item.Childrens)
  233. {
  234. var bln = CheckDrawRectClick(child, mouseLocation, blnDoExpansion);
  235. if (bln)
  236. return bln;
  237. }
  238. }
  239. }
  240. return false;
  241. }
  242. /// <summary>
  243. /// 单击检查
  244. /// </summary>
  245. /// <param name="item">The item.</param>
  246. /// <param name="mouseLocation">The mouse location.</param>
  247. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  248. private bool CheckExpansionClick(MindMappingItemEntity item, Point mouseLocation)
  249. {
  250. if (item == null)
  251. return false;
  252. else
  253. {
  254. if (item.DrawRectangle.Contains(mouseLocation))
  255. {
  256. selectEntity = item;
  257. if (ItemClicked != null )
  258. {
  259. ItemClicked(item, null);
  260. }
  261. }
  262. if (item.ExpansionRectangle.Contains(mouseLocation))
  263. {
  264. item.IsExpansion = !item.IsExpansion;
  265. return true;
  266. }
  267. if (item.Childrens != null && item.Childrens.Length > 0)
  268. {
  269. foreach (var child in item.Childrens)
  270. {
  271. var bln = CheckExpansionClick(child, mouseLocation);
  272. if (bln)
  273. return bln;
  274. }
  275. }
  276. }
  277. return false;
  278. }
  279. /// <summary>
  280. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  281. /// </summary>
  282. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  283. protected override void OnPaint(PaintEventArgs e)
  284. {
  285. try
  286. {
  287. base.OnPaint(e);
  288. if (m_rectWorking == Rectangle.Empty || m_rectWorking == null)
  289. return;
  290. var g = e.Graphics;
  291. g.SetGDIHigh();
  292. int intHeight = dataSource.AllChildrensMaxShowHeight;
  293. dataSource.WorkingRectangle = new RectangleF(m_rectWorking.Left, m_rectWorking.Top + (m_rectWorking.Height - intHeight) / 2, m_rectWorking.Width, intHeight);
  294. DrawItem(dataSource, g);
  295. }
  296. catch (Exception exc)
  297. {
  298. MessageBox.Show(exc.ToString(), "错误");
  299. }
  300. }
  301. /// <summary>
  302. /// 画节点
  303. /// </summary>
  304. /// <param name="item">The item.</param>
  305. /// <param name="g">The g.</param>
  306. private void DrawItem(MindMappingItemEntity item, Graphics g)
  307. {
  308. //节点
  309. var size = g.MeasureString(item.Text, item.Font);
  310. item.DrawRectangle = new RectangleF(item.WorkingRectangle.Left + 2, item.WorkingRectangle.Top + (item.WorkingRectangle.Height - size.Height) / 2 + 2, size.Width + 4, size.Height + 4);
  311. GraphicsPath drawPath = item.DrawRectangle.CreateRoundedRectanglePath(5);
  312. if (item.BackColor.HasValue)
  313. g.FillPath(new SolidBrush(item.BackColor.Value), drawPath);
  314. else
  315. g.FillPath(new SolidBrush(itemBackcolor), drawPath);
  316. g.DrawString(item.Text, item.Font, new SolidBrush(item.ForeColor), item.DrawRectangle.Location.X + 2, item.DrawRectangle.Location.Y + 2);
  317. //子节点
  318. if (item.Childrens != null && item.IsExpansion)
  319. {
  320. for (int i = 0; i < item.Childrens.Length; i++)
  321. {
  322. var child = item.Childrens[i];
  323. if (i == 0)
  324. {
  325. child.WorkingRectangle = new RectangleF(item.DrawRectangle.Right + splitWidth, item.WorkingRectangle.Top, item.WorkingRectangle.Width - (item.DrawRectangle.Width + splitWidth), child.AllChildrensMaxShowHeight);
  326. }
  327. else
  328. {
  329. child.WorkingRectangle = new RectangleF(item.DrawRectangle.Right + splitWidth, item.Childrens[i - 1].WorkingRectangle.Bottom, item.WorkingRectangle.Width - (item.DrawRectangle.Width + splitWidth), child.AllChildrensMaxShowHeight);
  330. }
  331. DrawItem(child, g);
  332. }
  333. }
  334. //连线
  335. if (item.ParentItem != null)
  336. {
  337. g.DrawLines(new Pen(new SolidBrush(lineColor), 1), new PointF[]
  338. {
  339. new PointF(item.ParentItem.DrawRectangle.Right,item.ParentItem.DrawRectangle.Top+item.ParentItem.DrawRectangle.Height/2),
  340. new PointF(item.ParentItem.DrawRectangle.Right+12,item.ParentItem.DrawRectangle.Top+item.ParentItem.DrawRectangle.Height/2),
  341. //new PointF(item.ParentItem.DrawRectangle.Right+12,item.DrawRectangle.Top+item.DrawRectangle.Height/2),
  342. new PointF(item.DrawRectangle.Left-12,item.DrawRectangle.Top+item.DrawRectangle.Height/2),
  343. new PointF(item.DrawRectangle.Left,item.DrawRectangle.Top+item.DrawRectangle.Height/2),
  344. });
  345. }
  346. //展开折叠按钮
  347. if (item.Childrens != null && item.Childrens.Length > 0)
  348. {
  349. RectangleF _rect = new RectangleF(item.DrawRectangle.Right + 1, item.DrawRectangle.Top + (item.DrawRectangle.Height - 10) / 2, 10, 10);
  350. item.ExpansionRectangle = _rect;
  351. g.FillEllipse(new SolidBrush(this.BackColor), _rect);
  352. g.DrawEllipse(new Pen(new SolidBrush(Color.Black)), _rect);
  353. g.DrawLine(new Pen(new SolidBrush(lineColor)), _rect.Left + 2, _rect.Y + _rect.Height / 2, _rect.Right - 2, _rect.Y + _rect.Height / 2);
  354. if (!item.IsExpansion)
  355. {
  356. g.DrawLine(new Pen(new SolidBrush(lineColor)), _rect.Left + _rect.Width / 2, _rect.Top + 2, _rect.Left + _rect.Width / 2, _rect.Bottom - 2);
  357. }
  358. }
  359. }
  360. /// <summary>
  361. /// 重置大小
  362. /// </summary>
  363. private void ResetSize()
  364. {
  365. if (this.Parent == null)
  366. return;
  367. try
  368. {
  369. ControlHelper.FreezeControl(this, true);
  370. if (dataSource == null)
  371. {
  372. m_rectWorking = Rectangle.Empty;
  373. this.Size = this.Parent.Size;
  374. }
  375. else
  376. {
  377. int intWidth = dataSource.AllChildrensMaxShowWidth;
  378. int intHeight = dataSource.AllChildrensMaxShowHeight;
  379. this.Width = intWidth + padding * 2;
  380. this.Height = intHeight + padding * 2;
  381. if (this.Width < this.Parent.Width)
  382. this.Width = this.Parent.Width;
  383. m_rectWorking = new Rectangle(padding, padding, intWidth, intHeight);
  384. if (this.Height > this.Parent.Height)
  385. {
  386. //this.Location = new Point(0, 0);
  387. }
  388. else
  389. this.Location = new Point(0, (this.Parent.Height - this.Height) / 2);
  390. }
  391. }
  392. finally
  393. {
  394. ControlHelper.FreezeControl(this, false);
  395. }
  396. }
  397. }
  398. }