using CommonLang.Property;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CommonFroms
{
public static class FormUtils
{
[DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);
///
/// 切换到当前窗口
///
///
///
static public void SwithToThisForm(Form form, bool turnOn)
{
SwitchToThisWindow(form.Handle, turnOn);
}
///
/// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
///
///
///
///
///
public static bool ListViewItemTagEquals(ListView view, ICollection list) where T : class
{
if (view.Items.Count != list.Count)
{
return false;
}
foreach (ListViewItem item in view.Items)
{
T it = item.Tag as T;
if (!list.Contains(it))
{
return false;
}
}
return true;
}
///
/// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
///
///
///
/// 参数1表示list中的单位,参数2表示ListView里的Tag
///
public static bool ListViewItemTagEquals(ListView view, IList list, IComparer compare)
{
if (view.Items.Count != list.Count)
{
return false;
}
foreach (ListViewItem item in view.Items)
{
bool finded = false;
foreach (object tag in list)
{
if (compare.Compare(tag, item.Tag) == 0)
{
finded = true;
break;
}
}
if (!finded)
{
return false;
}
}
return true;
}
public static void GetAllTreeNodes(TreeNodeCollection nodes, List allnodes)
{
foreach (TreeNode node in nodes)
{
allnodes.Add(node);
GetAllTreeNodes(node.Nodes, allnodes);
}
}
public static TreeNode FindTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
{
List allnodes = new List();
GetAllTreeNodes(nodes, allnodes);
int begin = allnodes.IndexOf(start);
if (begin < 0)
{
begin = 0;
}
else if (!include)
{
begin += 1;
}
for (int i = begin; i < allnodes.Count; i++)
{
TreeNode tn = allnodes[i];
if (tn.Text.Contains(text))
{
return tn;
}
}
return null;
}
public static TreeNode FindLastTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
{
List allnodes = new List();
GetAllTreeNodes(nodes, allnodes);
int end = allnodes.IndexOf(start);
if (end < 0)
{
end = allnodes.Count - 1;
}
else if (!include)
{
end -= 1;
}
for (int i = end; i >= 0; i--)
{
TreeNode tn = allnodes[i];
if (tn.Text.Contains(text))
{
return tn;
}
}
return null;
}
}
}