using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; using System.IO; namespace ResourceManage { #region 设置双缓存,是屏幕不会闪烁 class ListViewNF : System.Windows.Forms.ListView { public ListViewNF() { //开启双缓存 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.EnableNotifyMessage, true); } protected override void OnNotifyMessage(Message m) { if (m.Msg != 0x14) { base.OnNotifyMessage(m); } } }; #endregion public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 定义的变量 //自定义的文件操作类 ManageFile clManageFile = new ManageFile(); //ManageThread clManageThread = new ManageThread(); //记录listview中items的集合 private List<ListViewItem> ItemsSource = new List<ListViewItem>(); ListViewItem[] CurrentCacheItemSource; //记录选中的TreeView节点 TreeNode tNode; //记录Thread中sleep前后的list的count值 int iLoopx = 0; int iLoopy = 0; List<int> list = new List<int>(); //记录线程二是否开始 bool bSecondThread = true; //记录线程一是否开始 bool bFirstThread = true; //定义线程二 Thread threadSecond; //定义线程一 Thread threadFirst; //记录选中的文件夹的路径集合 List<string> listStr = new List<string>(); private delegate void FlushClient();//代理 //记录TreeView节点的路径 string strTreeViewFilePath; #endregion #region 页面事件 //页面加载 private void Form1_Load(object sender, EventArgs e) { // clManageFile.GetImageList(ref imageList1); //非递归绑定节点 clManageFile.BandTreeViewNodeNo(tvTreeView); //对线程使用的设置 Control.CheckForIllegalCrossThreadCalls = false; //自定义两个线程 threadSecond = new Thread(new ThreadStart(threadFunSecond)); threadSecond.IsBackground = false; threadFirst = new Thread(new ThreadStart(CrossThreadFlush)); threadFirst.IsBackground = false; // clManageFile.IFileNum = 0; } //重写关闭事件 protected override void OnClosed(EventArgs e) { threadFirst.Abort(); threadSecond.Abort(); base.OnClosed(e); } #endregion #region TreeView点击事件 //右键选择中Item项 private void tvTreeView_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { tvTreeView.SelectedNode = tNode; } if(e.Button == System.Windows.Forms.MouseButtons.Left) { if (CurrentCacheItemSource != null) { strPath = (CurrentCacheItemSource[iIndex].Name); } } } //单击TreeView节点 private void tvTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { tNode = e.Node; } //TreeView节点获得焦点 private void tvTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e) { //必须设置,否则影响过滤任务 lvListView.SelectedIndices.Clear(); lvListView.VirtualMode = false; //用于过滤无效任务 try { Monitor.Enter(list); list.Add(1); } finally { Monitor.Exit(list); } //开启两个线程 if (bSecondThread) { threadSecond.Start(); threadFirst.Start(); bSecondThread = false; } //获得点击的treenode tNode = e.Node; e.Node.Checked = true; } //点击节点前的加号 private void tvTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) { clManageFile.FirstBandNodeNo(e.Node); } #endregion #region TreeView修改文件名 //进入修改之前 private void tvTreeView_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Node.Parent == null) { e.Node.EndEdit(true); } } //修改完成 private void tvTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { strTreeViewFilePath = e.Node.Name; if (null != e.Label) { //进行文件夹名字的修改 clManageFile.UpdataFileName(strTreeViewFilePath, e.Label, ref tNode); clManageFile.FirstBandNodeNo(tNode); //tvTreeView.Focus(); threadFunFirst(); tvTreeView.SelectedNode = tNode; } } #endregion #region TreeView右键功能 //新增文件夹 private void AddtoolStripMenuItemTreeView_Click(object sender, EventArgs e) { } //修改文件夹 private void UpdatatoolStripMenuItemTreeView_Click(object sender, EventArgs e) { tvTreeView.SelectedNode.BeginEdit(); } //删除文件夹 private void DeltoolStripMenuItemTreeView_Click(object sender, EventArgs e) { } //复制文件夹 private void CopytoolStripMenuItemTreeView_Click(object sender, EventArgs e) { } //粘贴文件夹 private void PlasttoolStripMenuItemTreeView_Click(object sender, EventArgs e) { } #endregion #region ListView点击事件 ListViewItem item = new ListViewItem(); //双击ListView中item打开此文件目录的内容 private void lvListView_DoubleClick(object sender, EventArgs e) { } //获得多个有焦点的文件的路径 private void lvListView_MouseClick(object sender, MouseEventArgs e) { //iIndex = lvListView.SelectedIndices[0]; item = lvListView.GetItemAt(e.X, e.Y); strPath = item.Name; } #endregion #region ListView修改文件名 string strPath = ""; int iIndex = 0; private void lvListView_BeforeLabelEdit(object sender, LabelEditEventArgs e) { } //记录文件修改之后的名称 string strLable = ""; //对文件名进行修改之后 private void lvListView_AfterLabelEdit(object sender, LabelEditEventArgs e) { strLable = e.Label; Thread thread = new Thread(new ThreadStart(UpFileNameThread)); thread.IsBackground = false; thread.Start(); clManageFile.FirstBandNodeNo(tNode); tNode.Expand(); } private void UpFileNameThread() { firstItem = 0; clManageFile.UpdataFileName(strPath, strLable); if (this.InvokeRequired) { this.BeginInvoke(new EventHandler(DelRefresh)); } else { DelRefresh(null, null); } } private void DelRefresh(object sender, EventArgs e) { this.lvListView.VirtualListSize = ItemsSource.Count; this.lvListView.Refresh(); } #endregion #region ListView右键功能 //新增文件夹 private void AddToolStripMenuItem_Click(object sender, EventArgs e) { lvListView.VirtualMode = false; string strNewFilePath = ""; clManageFile.AddFileName(tNode.Name, "新建文件夹", ref strNewFilePath); ///AgainBand(); //foreach(ListViewItem items in lvListView.Items) //{ // if (items.Name == strNewFilePath) // { // items.Selected = true; // break; // } //} } //删除文件夹 private void DelToolStripMenuItem_Click(object sender, EventArgs e) { foreach (string strDelFileName in listStr) { clManageFile.DelFileName(strDelFileName); } AgainBand(); } //修改文件夹 private void UpdataToolStripMenuItem_Click(object sender, EventArgs e) { lvListView.SelectedItems[0].BeginEdit(); } //打开文件夹 private void OpenToolStripMenuItem_Click(object sender, EventArgs e) { tvTreeView.Focus(); TreeNode tNodeOne = new TreeNode(); clManageFile.FindTreeNode(tNode, listStr[0], ref tNodeOne); clManageFile.FirstBandNodeNo(tNodeOne); tNodeOne.Expand(); tvTreeView.SelectedNode = tNodeOne; } //复制文件夹 private void CopyToolStripMenuItem_Click(object sender, EventArgs e) { } //粘贴文件夹 private void PlastToolStripMenuItem_Click(object sender, EventArgs e) { foreach (string strPath in listStr) { clManageFile.CopyFileName(strPath); } clManageFile.BandListViewItem(tNode, lvListView); } //右键的目录显示情况 private void contextMenuStripFirst_Opening(object sender, CancelEventArgs e) { if (0 == listStr.Count) { AddToolStripMenuItem.Enabled = false; DelToolStripMenuItem.Enabled = false; UpdataToolStripMenuItem.Enabled = false; OpenToolStripMenuItem.Enabled = false; CopyToolStripMenuItem.Enabled = false; PlastToolStripMenuItem.Enabled = false; } else { AddToolStripMenuItem.Enabled = false; DelToolStripMenuItem.Enabled = false; UpdataToolStripMenuItem.Enabled = false; OpenToolStripMenuItem.Enabled = false; CopyToolStripMenuItem.Enabled = false; PlastToolStripMenuItem.Enabled = false; } } //增删改查文件后重新绑定 private void AgainBand() { clManageFile.BandListViewItem(tNode, lvListView); clManageFile.FirstBandNodeNo(tNode); tNode.Expand(); } #endregion #region ListView的虚拟模式 //用来记录ListView的总数据的个数 List<string> strList = new List<string>(); //用来记录标志 private int firstItem; #region 虚拟模式的三个主要函数 private void lvListView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { System.Diagnostics.Debug.WriteLine(e.ItemIndex); if (CurrentCacheItemSource != null && e.ItemIndex >= firstItem && e.ItemIndex < firstItem + CurrentCacheItemSource.Length) { e.Item = CurrentCacheItemSource[e.ItemIndex - firstItem]; } else { if (e.ItemIndex < strList.Count) { int i = 0; while(i<1) { string strPath = strList[e.ItemIndex];//得到路径 e.Item = clManageFile.GetItem(strPath); ++i; } } else { e.Item = null; } } } private void lvListView_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e) { if (CurrentCacheItemSource != null && e.StartIndex >= firstItem && e.EndIndex <= firstItem + CurrentCacheItemSource.Length) { return; } firstItem = e.StartIndex; int Length = e.EndIndex - e.StartIndex + 1; CurrentCacheItemSource = new ListViewItem[Length]; int x = 0; imageList1.Images.Clear(); for (int i = 0; i < Length; ++i) { if (i < strList.Count) { string strPath = strList[i + firstItem];//得到路径 CurrentCacheItemSource[i] = clManageFile.GetItem(strPath); } } } private void lvListView_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e) { double x = 0; if (Double.TryParse(e.Text, out x)) //check if this is a valid search { x = Math.Sqrt(x); x = Math.Round(x); e.Index = (int)x; } } #endregion private void lvListView_VirtualItemsSelectionRangeChanged(object sender, ListViewVirtualItemsSelectionRangeChangedEventArgs e) { //strPath = (CurrentCacheItemSource[iIndex].Name); //MessageBox.Show(strPath); //foreach (ListViewItem item in lvListView.SelectedItems) //{ // string str = item.Name; // listStr.Add(str); //} } private bool isMouseDown = false; private void lvListView_MouseDown(object sender, MouseEventArgs e) { isMouseDown = true; } private void lvListView_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; } private void lvListView_MouseMove(object sender, MouseEventArgs e) { ListViewItem item = lvListView.GetItemAt(e.X, e.Y); if (item != null) { System.Diagnostics.Debug.WriteLine(item.Text); } if (isMouseDown && e.Button == MouseButtons.Left) { lvListView.AllowDrop = true; List<string> strListTemp = new List<string>(); foreach (int iLoop in lvListView.SelectedIndices) { strListTemp.Add(strList[iLoop]); } lvListView.DoDragDrop(strListTemp, DragDropEffects.All); } } private string DragOverObject = null; private void lvListView_DragOver(object sender, DragEventArgs e) { Point p = lvListView.PointToClient(new Point(e.X, e.Y)); ListViewItem item = lvListView.GetItemAt(p.X, p.Y); if (item != null) { DragOverObject = strList[item.Index]; e.Effect = DragDropEffects.All; } else { e.Effect = DragDropEffects.None; } } private void lvListView_DragDrop(object sender, DragEventArgs e) { string[] formats = e.Data.GetFormats(); if (formats.Length > 0) { List<string> temList = (List<string>)e.Data.GetData(formats[0]); if (temList != null) { foreach (string p in temList) { if(DragOverObject == p) { DelRefresh(null, null); return; } else if (Directory.Exists(DragOverObject)) { clManageFile.MoveFileName(DragOverObject, p); } else { this.lvListView.VirtualListSize = ItemsSource.Count; this.lvListView.Refresh(); return; } } } } DelRefresh(null, null); DragOverObject = null; this.lvListView.Refresh(); } #endregion #region 线程 //线程一的方法 private void CrossThreadFlush() { while (true) { if (bFirstThread) { continue; } else { //将代理绑定到方法 FlushClient fc = new FlushClient(threadFunFirst); //this.BeginInvoke(fc);//调用代理 lvListView.Invoke(fc); bFirstThread = true; } } } //线程一的方法:开启ListView的虚拟模式 private void threadFunFirst() { //虚拟模式绑定 strList.Clear(); //imageList1.Images.Clear(); if(CurrentCacheItemSource != null) { CurrentCacheItemSource = null; } string[] astrPath = Directory.GetFileSystemEntries(tNode.Name); foreach (string strPath in astrPath) { if (clManageFile.IsHidden(strPath)) { continue; } strList.Add(strPath); } lvListView.VirtualListSize = strList.Count; lvListView.VirtualMode = true; } //线程二的方法:控制线程一 private void threadFunSecond() { while (true) { try { Monitor.Enter(list); iLoopx = list.Count; } finally { Monitor.Exit(list); } Thread.Sleep(100); try { Monitor.Enter(list); iLoopy = list.Count; } finally { Monitor.Exit(list); } if (iLoopx != iLoopy) { continue; } if (tNode != null) { if (tNode.Checked) { bFirstThread = false; list.Clear(); } else { continue; } } } } #endregion } }