TreeViewEx.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="TreeViewEx.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.ComponentModel;
  19. using System.Diagnostics;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. using System.Linq;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. using HZH_Controls.Properties;
  27. namespace HZH_Controls.Controls
  28. {
  29. /// <summary>
  30. /// Class TreeViewEx.
  31. /// Implements the <see cref="System.Windows.Forms.TreeView" />
  32. /// </summary>
  33. /// <seealso cref="System.Windows.Forms.TreeView" />
  34. public partial class TreeViewEx : TreeView
  35. {
  36. /// <summary>
  37. /// The ws vscroll
  38. /// </summary>
  39. private const int WS_VSCROLL = 2097152;
  40. /// <summary>
  41. /// The GWL style
  42. /// </summary>
  43. private const int GWL_STYLE = -16;
  44. /// <summary>
  45. /// The LST tips
  46. /// </summary>
  47. private Dictionary<string, string> _lstTips = new Dictionary<string, string>();
  48. /// <summary>
  49. /// The tip font
  50. /// </summary>
  51. private Font _tipFont = new Font("Arial Unicode MS", 12f);
  52. /// <summary>
  53. /// The tip image
  54. /// </summary>
  55. private Image _tipImage = Resources.tips;
  56. /// <summary>
  57. /// The is show tip
  58. /// </summary>
  59. private bool _isShowTip = false;
  60. /// <summary>
  61. /// The is show by custom model
  62. /// </summary>
  63. private bool _isShowByCustomModel = true;
  64. /// <summary>
  65. /// The node height
  66. /// </summary>
  67. private int _nodeHeight = 50;
  68. /// <summary>
  69. /// The node down pic
  70. /// </summary>
  71. private Image _nodeDownPic = Resources.list_add;
  72. /// <summary>
  73. /// The node up pic
  74. /// </summary>
  75. private Image _nodeUpPic = Resources.list_subtract;
  76. /// <summary>
  77. /// The node background color
  78. /// </summary>
  79. private Color _nodeBackgroundColor = Color.White;
  80. /// <summary>
  81. /// The node fore color
  82. /// </summary>
  83. private Color _nodeForeColor = Color.FromArgb(62, 62, 62);
  84. /// <summary>
  85. /// The node is show split line
  86. /// </summary>
  87. private bool _nodeIsShowSplitLine = false;
  88. /// <summary>
  89. /// The node split line color
  90. /// </summary>
  91. private Color _nodeSplitLineColor = Color.FromArgb(232, 232, 232);
  92. /// <summary>
  93. /// The m node selected color
  94. /// </summary>
  95. private Color m_nodeSelectedColor = Color.FromArgb(255, 77, 59);
  96. /// <summary>
  97. /// The m node selected fore color
  98. /// </summary>
  99. private Color m_nodeSelectedForeColor = Color.White;
  100. /// <summary>
  101. /// The parent node can select
  102. /// </summary>
  103. private bool _parentNodeCanSelect = true;
  104. /// <summary>
  105. /// The tree font size
  106. /// </summary>
  107. private SizeF treeFontSize = SizeF.Empty;
  108. /// <summary>
  109. /// The BLN has v bar
  110. /// </summary>
  111. private bool blnHasVBar = false;
  112. /// <summary>
  113. /// Gets or sets the LST tips.
  114. /// </summary>
  115. /// <value>The LST tips.</value>
  116. public Dictionary<string, string> LstTips
  117. {
  118. get
  119. {
  120. return this._lstTips;
  121. }
  122. set
  123. {
  124. this._lstTips = value;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets or sets the tip font.
  129. /// </summary>
  130. /// <value>The tip font.</value>
  131. [Category("自定义属性"), Description("角标文字字体")]
  132. public Font TipFont
  133. {
  134. get
  135. {
  136. return this._tipFont;
  137. }
  138. set
  139. {
  140. this._tipFont = value;
  141. }
  142. }
  143. /// <summary>
  144. /// Gets or sets the tip image.
  145. /// </summary>
  146. /// <value>The tip image.</value>
  147. [Category("自定义属性"), Description("是否显示角标")]
  148. public Image TipImage
  149. {
  150. get
  151. {
  152. return this._tipImage;
  153. }
  154. set
  155. {
  156. this._tipImage = value;
  157. }
  158. }
  159. /// <summary>
  160. /// Gets or sets a value indicating whether this instance is show tip.
  161. /// </summary>
  162. /// <value><c>true</c> if this instance is show tip; otherwise, <c>false</c>.</value>
  163. [Category("自定义属性"), Description("是否显示角标")]
  164. public bool IsShowTip
  165. {
  166. get
  167. {
  168. return this._isShowTip;
  169. }
  170. set
  171. {
  172. this._isShowTip = value;
  173. }
  174. }
  175. /// <summary>
  176. /// Gets or sets a value indicating whether this instance is show by custom model.
  177. /// </summary>
  178. /// <value><c>true</c> if this instance is show by custom model; otherwise, <c>false</c>.</value>
  179. [Category("自定义属性"), Description("使用自定义模式")]
  180. public bool IsShowByCustomModel
  181. {
  182. get
  183. {
  184. return this._isShowByCustomModel;
  185. }
  186. set
  187. {
  188. this._isShowByCustomModel = value;
  189. }
  190. }
  191. /// <summary>
  192. /// Gets or sets the height of the node.
  193. /// </summary>
  194. /// <value>The height of the node.</value>
  195. [Category("自定义属性"), Description("节点高度(IsShowByCustomModel=true时生效)")]
  196. public int NodeHeight
  197. {
  198. get
  199. {
  200. return this._nodeHeight;
  201. }
  202. set
  203. {
  204. this._nodeHeight = value;
  205. base.ItemHeight = value;
  206. }
  207. }
  208. /// <summary>
  209. /// Gets or sets the node down pic.
  210. /// </summary>
  211. /// <value>The node down pic.</value>
  212. [Category("自定义属性"), Description("下翻图标(IsShowByCustomModel=true时生效)")]
  213. public Image NodeDownPic
  214. {
  215. get
  216. {
  217. return this._nodeDownPic;
  218. }
  219. set
  220. {
  221. this._nodeDownPic = value;
  222. }
  223. }
  224. /// <summary>
  225. /// Gets or sets the node up pic.
  226. /// </summary>
  227. /// <value>The node up pic.</value>
  228. [Category("自定义属性"), Description("上翻图标(IsShowByCustomModel=true时生效)")]
  229. public Image NodeUpPic
  230. {
  231. get
  232. {
  233. return this._nodeUpPic;
  234. }
  235. set
  236. {
  237. this._nodeUpPic = value;
  238. }
  239. }
  240. /// <summary>
  241. /// Gets or sets the color of the node background.
  242. /// </summary>
  243. /// <value>The color of the node background.</value>
  244. [Category("自定义属性"), Description("节点背景颜色(IsShowByCustomModel=true时生效)")]
  245. public Color NodeBackgroundColor
  246. {
  247. get
  248. {
  249. return this._nodeBackgroundColor;
  250. }
  251. set
  252. {
  253. this._nodeBackgroundColor = value;
  254. }
  255. }
  256. /// <summary>
  257. /// Gets or sets the color of the node fore.
  258. /// </summary>
  259. /// <value>The color of the node fore.</value>
  260. [Category("自定义属性"), Description("节点字体颜色(IsShowByCustomModel=true时生效)")]
  261. public Color NodeForeColor
  262. {
  263. get
  264. {
  265. return this._nodeForeColor;
  266. }
  267. set
  268. {
  269. this._nodeForeColor = value;
  270. }
  271. }
  272. /// <summary>
  273. /// Gets or sets a value indicating whether [node is show split line].
  274. /// </summary>
  275. /// <value><c>true</c> if [node is show split line]; otherwise, <c>false</c>.</value>
  276. [Category("自定义属性"), Description("节点是否显示分割线(IsShowByCustomModel=true时生效)")]
  277. public bool NodeIsShowSplitLine
  278. {
  279. get
  280. {
  281. return this._nodeIsShowSplitLine;
  282. }
  283. set
  284. {
  285. this._nodeIsShowSplitLine = value;
  286. }
  287. }
  288. /// <summary>
  289. /// Gets or sets the color of the node split line.
  290. /// </summary>
  291. /// <value>The color of the node split line.</value>
  292. [Category("自定义属性"), Description("节点分割线颜色(IsShowByCustomModel=true时生效)")]
  293. public Color NodeSplitLineColor
  294. {
  295. get
  296. {
  297. return this._nodeSplitLineColor;
  298. }
  299. set
  300. {
  301. this._nodeSplitLineColor = value;
  302. }
  303. }
  304. /// <summary>
  305. /// Gets or sets the color of the node selected.
  306. /// </summary>
  307. /// <value>The color of the node selected.</value>
  308. [Category("自定义属性"), Description("选中节点背景颜色(IsShowByCustomModel=true时生效)")]
  309. public Color NodeSelectedColor
  310. {
  311. get
  312. {
  313. return this.m_nodeSelectedColor;
  314. }
  315. set
  316. {
  317. this.m_nodeSelectedColor = value;
  318. }
  319. }
  320. /// <summary>
  321. /// Gets or sets the color of the node selected fore.
  322. /// </summary>
  323. /// <value>The color of the node selected fore.</value>
  324. [Category("自定义属性"), Description("选中节点字体颜色(IsShowByCustomModel=true时生效)")]
  325. public Color NodeSelectedForeColor
  326. {
  327. get
  328. {
  329. return this.m_nodeSelectedForeColor;
  330. }
  331. set
  332. {
  333. this.m_nodeSelectedForeColor = value;
  334. }
  335. }
  336. /// <summary>
  337. /// Gets or sets a value indicating whether [parent node can select].
  338. /// </summary>
  339. /// <value><c>true</c> if [parent node can select]; otherwise, <c>false</c>.</value>
  340. [Category("自定义属性"), Description("父节点是否可选中")]
  341. public bool ParentNodeCanSelect
  342. {
  343. get
  344. {
  345. return this._parentNodeCanSelect;
  346. }
  347. set
  348. {
  349. this._parentNodeCanSelect = value;
  350. }
  351. }
  352. /// <summary>
  353. /// Initializes a new instance of the <see cref="TreeViewEx" /> class.
  354. /// </summary>
  355. public TreeViewEx()
  356. {
  357. base.HideSelection = false;
  358. base.DrawMode = TreeViewDrawMode.OwnerDrawAll;
  359. base.DrawNode += new DrawTreeNodeEventHandler(this.treeview_DrawNode);
  360. base.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.TreeViewEx_NodeMouseClick);
  361. base.SizeChanged += new EventHandler(this.TreeViewEx_SizeChanged);
  362. base.AfterSelect += new TreeViewEventHandler(this.TreeViewEx_AfterSelect);
  363. base.FullRowSelect = true;
  364. base.ShowLines = false;
  365. base.ShowPlusMinus = false;
  366. base.ShowRootLines = false;
  367. this.BackColor = Color.White;
  368. this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  369. DoubleBuffered = true;
  370. }
  371. /// <summary>
  372. /// 重写 <see cref="M:System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message@)" />。
  373. /// </summary>
  374. /// <param name="m">要处理的 Windows<see cref="T:System.Windows.Forms.Message" />。</param>
  375. protected override void WndProc(ref Message m)
  376. {
  377. if (m.Msg == 0x0014) // 禁掉清除背景消息WM_ERASEBKGND
  378. return;
  379. base.WndProc(ref m);
  380. }
  381. /// <summary>
  382. /// Handles the AfterSelect event of the TreeViewEx control.
  383. /// </summary>
  384. /// <param name="sender">The source of the event.</param>
  385. /// <param name="e">The <see cref="TreeViewEventArgs" /> instance containing the event data.</param>
  386. private void TreeViewEx_AfterSelect(object sender, TreeViewEventArgs e)
  387. {
  388. try
  389. {
  390. if (e.Node != null)
  391. {
  392. if (!this._parentNodeCanSelect)
  393. {
  394. if (e.Node.Nodes.Count > 0)
  395. {
  396. e.Node.Expand();
  397. base.SelectedNode = e.Node.Nodes[0];
  398. }
  399. }
  400. }
  401. }
  402. catch (Exception ex)
  403. {
  404. throw ex;
  405. }
  406. }
  407. /// <summary>
  408. /// Handles the SizeChanged event of the TreeViewEx control.
  409. /// </summary>
  410. /// <param name="sender">The source of the event.</param>
  411. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  412. private void TreeViewEx_SizeChanged(object sender, EventArgs e)
  413. {
  414. this.Refresh();
  415. }
  416. /// <summary>
  417. /// Handles the NodeMouseClick event of the TreeViewEx control.
  418. /// </summary>
  419. /// <param name="sender">The source of the event.</param>
  420. /// <param name="e">The <see cref="TreeNodeMouseClickEventArgs" /> instance containing the event data.</param>
  421. private void TreeViewEx_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  422. {
  423. try
  424. {
  425. if (e.Node != null)
  426. {
  427. if (e.Node.Nodes.Count > 0)
  428. {
  429. if (e.Node.IsExpanded)
  430. {
  431. e.Node.Collapse();
  432. }
  433. else
  434. {
  435. e.Node.Expand();
  436. }
  437. }
  438. if (base.SelectedNode != null)
  439. {
  440. if (base.SelectedNode == e.Node && e.Node.IsExpanded)
  441. {
  442. if (!this._parentNodeCanSelect)
  443. {
  444. if (e.Node.Nodes.Count > 0)
  445. {
  446. base.SelectedNode = e.Node.Nodes[0];
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. catch (Exception ex)
  454. {
  455. throw ex;
  456. }
  457. }
  458. /// <summary>
  459. /// Handles the DrawNode event of the treeview control.
  460. /// </summary>
  461. /// <param name="sender">The source of the event.</param>
  462. /// <param name="e">The <see cref="DrawTreeNodeEventArgs" /> instance containing the event data.</param>
  463. private void treeview_DrawNode(object sender, DrawTreeNodeEventArgs e)
  464. {
  465. try
  466. {
  467. if (e.Node == null || !this._isShowByCustomModel || (e.Node.Bounds.Width <= 0 && e.Node.Bounds.Height <= 0 && e.Node.Bounds.X <= 0 && e.Node.Bounds.Y <= 0))
  468. {
  469. e.DrawDefault = true;
  470. }
  471. else
  472. {
  473. e.Graphics.SetGDIHigh();
  474. if (base.Nodes.IndexOf(e.Node) == 0)
  475. {
  476. this.blnHasVBar = this.IsVerticalScrollBarVisible();
  477. }
  478. Font font = e.Node.NodeFont;
  479. if (font == null)
  480. {
  481. font = ((TreeView)sender).Font;
  482. }
  483. if (this.treeFontSize == SizeF.Empty)
  484. {
  485. this.treeFontSize = this.GetFontSize(font, e.Graphics);
  486. }
  487. bool flag = false;
  488. int intLeft = 0;
  489. if (CheckBoxes)
  490. {
  491. intLeft = 20;
  492. }
  493. int num = 0;
  494. if (base.ImageList != null && base.ImageList.Images.Count > 0 && e.Node.ImageIndex >= 0 && e.Node.ImageIndex < base.ImageList.Images.Count)
  495. {
  496. flag = true;
  497. num = (e.Bounds.Height - base.ImageList.ImageSize.Height) / 2;
  498. intLeft += base.ImageList.ImageSize.Width;
  499. }
  500. intLeft += e.Node.Level * Indent;
  501. if ((e.State == TreeNodeStates.Selected || e.State == TreeNodeStates.Focused || e.State == (TreeNodeStates.Focused | TreeNodeStates.Selected)) && (this._parentNodeCanSelect || e.Node.Nodes.Count <= 0))
  502. {
  503. e.Graphics.FillRectangle(new SolidBrush(this.m_nodeSelectedColor), new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(base.Width, e.Node.Bounds.Height)));
  504. e.Graphics.DrawString(e.Node.Text, font, new SolidBrush(this.m_nodeSelectedForeColor), (float)e.Bounds.X + intLeft, (float)e.Bounds.Y + ((float)this._nodeHeight - this.treeFontSize.Height) / 2f);
  505. }
  506. else
  507. {
  508. e.Graphics.FillRectangle(new SolidBrush(this._nodeBackgroundColor), new Rectangle(new Point(0, e.Node.Bounds.Y), new Size(base.Width, e.Node.Bounds.Height)));
  509. e.Graphics.DrawString(e.Node.Text, font, new SolidBrush(this._nodeForeColor), (float)e.Bounds.X + intLeft, (float)e.Bounds.Y + ((float)this._nodeHeight - this.treeFontSize.Height) / 2f);
  510. }
  511. if (CheckBoxes)
  512. {
  513. Rectangle rectCheck = new Rectangle(e.Bounds.X + 3 + e.Node.Level * Indent, e.Bounds.Y + (e.Bounds.Height - 16) / 2, 16, 16);
  514. GraphicsPath pathCheck = rectCheck.CreateRoundedRectanglePath(3);
  515. e.Graphics.FillPath(new SolidBrush(Color.FromArgb(247, 247, 247)), pathCheck);
  516. if (e.Node.Checked)
  517. {
  518. e.Graphics.DrawLines(new Pen(new SolidBrush(m_nodeSelectedColor),2), new Point[]
  519. {
  520. new Point(rectCheck.Left+2,rectCheck.Top+8),
  521. new Point(rectCheck.Left+6,rectCheck.Top+12),
  522. new Point(rectCheck.Right-4,rectCheck.Top+4)
  523. });
  524. }
  525. e.Graphics.DrawPath(new Pen(new SolidBrush(Color.FromArgb(200, 200, 200))), pathCheck);
  526. }
  527. if (flag)
  528. {
  529. int num2 = e.Bounds.X - num - base.ImageList.ImageSize.Width;
  530. if (num2 < 0)
  531. {
  532. num2 = 3;
  533. }
  534. e.Graphics.DrawImage(base.ImageList.Images[e.Node.ImageIndex], new Rectangle(new Point(num2 + intLeft - base.ImageList.ImageSize.Width, e.Bounds.Y + num), base.ImageList.ImageSize));
  535. }
  536. if (this._nodeIsShowSplitLine)
  537. {
  538. e.Graphics.DrawLine(new Pen(this._nodeSplitLineColor, 1f), new Point(0, e.Bounds.Y + this._nodeHeight - 1), new Point(base.Width, e.Bounds.Y + this._nodeHeight - 1));
  539. }
  540. bool flag2 = false;
  541. if (e.Node.Nodes.Count > 0)
  542. {
  543. if (e.Node.IsExpanded && this._nodeUpPic != null)
  544. {
  545. e.Graphics.DrawImage(this._nodeUpPic, new Rectangle(base.Width - (this.blnHasVBar ? 50 : 30), e.Bounds.Y + (this._nodeHeight - 20) / 2, 20, 20));
  546. }
  547. else if (this._nodeDownPic != null)
  548. {
  549. e.Graphics.DrawImage(this._nodeDownPic, new Rectangle(base.Width - (this.blnHasVBar ? 50 : 30), e.Bounds.Y + (this._nodeHeight - 20) / 2, 20, 20));
  550. }
  551. flag2 = true;
  552. }
  553. if (this._isShowTip && this._lstTips.ContainsKey(e.Node.Name) && !string.IsNullOrWhiteSpace(this._lstTips[e.Node.Name]))
  554. {
  555. int num3 = base.Width - (this.blnHasVBar ? 50 : 30) - (flag2 ? 20 : 0);
  556. int num4 = e.Bounds.Y + (this._nodeHeight - 20) / 2;
  557. e.Graphics.DrawImage(this._tipImage, new Rectangle(num3, num4, 20, 20));
  558. SizeF sizeF = e.Graphics.MeasureString(this._lstTips[e.Node.Name], this._tipFont, 100, StringFormat.GenericTypographic);
  559. e.Graphics.DrawString(this._lstTips[e.Node.Name], this._tipFont, new SolidBrush(Color.White), (float)(num3 + 10) - sizeF.Width / 2f - 3f, (float)(num4 + 10) - sizeF.Height / 2f);
  560. }
  561. }
  562. }
  563. catch (Exception ex)
  564. {
  565. throw ex;
  566. }
  567. }
  568. /// <summary>
  569. /// Gets the size of the font.
  570. /// </summary>
  571. /// <param name="font">The font.</param>
  572. /// <param name="g">The g.</param>
  573. /// <returns>SizeF.</returns>
  574. private SizeF GetFontSize(Font font, Graphics g = null)
  575. {
  576. SizeF result;
  577. try
  578. {
  579. bool flag = false;
  580. if (g == null)
  581. {
  582. g = base.CreateGraphics();
  583. flag = true;
  584. }
  585. SizeF sizeF = g.MeasureString("a", font, 100, StringFormat.GenericTypographic);
  586. if (flag)
  587. {
  588. g.Dispose();
  589. }
  590. result = sizeF;
  591. }
  592. catch (Exception ex)
  593. {
  594. throw ex;
  595. }
  596. return result;
  597. }
  598. /// <summary>
  599. /// Gets the window long.
  600. /// </summary>
  601. /// <param name="hwnd">The HWND.</param>
  602. /// <param name="nIndex">Index of the n.</param>
  603. /// <returns>System.Int32.</returns>
  604. [DllImport("user32", CharSet = CharSet.Auto)]
  605. private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
  606. /// <summary>
  607. /// Determines whether [is vertical scroll bar visible].
  608. /// </summary>
  609. /// <returns><c>true</c> if [is vertical scroll bar visible]; otherwise, <c>false</c>.</returns>
  610. private bool IsVerticalScrollBarVisible()
  611. {
  612. return base.IsHandleCreated && (TreeViewEx.GetWindowLong(base.Handle, -16) & 2097152) != 0;
  613. }
  614. }
  615. }