FormUtils.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using CommonLang.Property;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. namespace CommonFroms
  8. {
  9. public static class FormUtils
  10. {
  11. [DllImport("user32.dll", SetLastError = true)]
  12. static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);
  13. /// <summary>
  14. /// 切换到当前窗口
  15. /// </summary>
  16. /// <param name="form"></param>
  17. /// <param name="turnOn"></param>
  18. static public void SwithToThisForm(Form form, bool turnOn)
  19. {
  20. SwitchToThisWindow(form.Handle, turnOn);
  21. }
  22. /// <summary>
  23. /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
  24. /// </summary>
  25. /// <typeparam name="T"></typeparam>
  26. /// <param name="view"></param>
  27. /// <param name="list"></param>
  28. /// <returns></returns>
  29. public static bool ListViewItemTagEquals<T>(ListView view, ICollection<T> list) where T : class
  30. {
  31. if (view.Items.Count != list.Count)
  32. {
  33. return false;
  34. }
  35. foreach (ListViewItem item in view.Items)
  36. {
  37. T it = item.Tag as T;
  38. if (!list.Contains(it))
  39. {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. /// <summary>
  46. /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
  47. /// </summary>
  48. /// <param name="view"></param>
  49. /// <param name="list"></param>
  50. /// <param name="compare">参数1表示list中的单位,参数2表示ListView里的Tag</param>
  51. /// <returns></returns>
  52. public static bool ListViewItemTagEquals(ListView view, IList list, IComparer compare)
  53. {
  54. if (view.Items.Count != list.Count)
  55. {
  56. return false;
  57. }
  58. foreach (ListViewItem item in view.Items)
  59. {
  60. bool finded = false;
  61. foreach (object tag in list)
  62. {
  63. if (compare.Compare(tag, item.Tag) == 0)
  64. {
  65. finded = true;
  66. break;
  67. }
  68. }
  69. if (!finded)
  70. {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public static void GetAllTreeNodes(TreeNodeCollection nodes, List<TreeNode> allnodes)
  77. {
  78. foreach (TreeNode node in nodes)
  79. {
  80. allnodes.Add(node);
  81. GetAllTreeNodes(node.Nodes, allnodes);
  82. }
  83. }
  84. public static TreeNode FindTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
  85. {
  86. List<TreeNode> allnodes = new List<TreeNode>();
  87. GetAllTreeNodes(nodes, allnodes);
  88. int begin = allnodes.IndexOf(start);
  89. if (begin < 0)
  90. {
  91. begin = 0;
  92. }
  93. else if (!include)
  94. {
  95. begin += 1;
  96. }
  97. for (int i = begin; i < allnodes.Count; i++)
  98. {
  99. TreeNode tn = allnodes[i];
  100. if (tn.Text.Contains(text))
  101. {
  102. return tn;
  103. }
  104. }
  105. return null;
  106. }
  107. public static TreeNode FindLastTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
  108. {
  109. List<TreeNode> allnodes = new List<TreeNode>();
  110. GetAllTreeNodes(nodes, allnodes);
  111. int end = allnodes.IndexOf(start);
  112. if (end < 0)
  113. {
  114. end = allnodes.Count - 1;
  115. }
  116. else if (!include)
  117. {
  118. end -= 1;
  119. }
  120. for (int i = end; i >= 0; i--)
  121. {
  122. TreeNode tn = allnodes[i];
  123. if (tn.Text.Contains(text))
  124. {
  125. return tn;
  126. }
  127. }
  128. return null;
  129. }
  130. }
  131. }