using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using OTS.WinFormsUI.Docking; using System.Runtime.InteropServices; using System.IO; using System.Collections; using OTSDataType; namespace OTSMeasureApp { public partial class OTSSolutionWindow : DockContent { //如果没有样品标题信息,就使用默认 "未加载样品名"; public String m_DefaultSolutionName = "未加载样品名"; public String m_DefaultSampleName = "样品"; public OTSIncAMeasureAppForm m_MeasureAppForm = null; public OTSDisplayTreeBase m_TreeViewBase = null; //工作样品节点 public TreeNode m_WorkSampleNode = null; // 设置工作样品是否成功 public bool m_bSetWorkSampleFlag = false; public List m_TreeSampleInfoList = new List(); //保存Treeview所有样品节点信息 //当前工作样品名 public String m_WorkSampleName = ""; //工作样品属性参数 public CTreeSampleParam m_WorkSampleParam = new CTreeSampleParam(); //国际化 OTSSysMgrTools.Language lan; Hashtable table; NLog.Logger log; public OTSSolutionWindow(OTSIncAMeasureAppForm MeasureAppForm) { InitializeComponent(); m_MeasureAppForm = MeasureAppForm; m_TreeViewBase = new OTSDisplayTreeBase(this); //国际化 lan = new OTSSysMgrTools.Language(this); table = lan.GetNameTable(this.Name); m_DefaultSolutionName = table["m_defaultsolutionname"].ToString(); m_DefaultSampleName = table["m_defaultsamplename"].ToString(); // 初始化事件对象 //this.InitSoluDelegateEvent(); } private void OTSSolutionWindow_Load(object sender, EventArgs e) { //TreeView可编辑状态。 treeView1.LabelEdit = true; this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll; log = NLog.LogManager.GetCurrentClassLogger(); } // 当TREEVIEW 样品数的样品顺序发生变化后,执行TREEVIEW树上的样品排序 public void Rev_SoluWindow_SampleSortOrder_Event() { m_TreeViewBase.SortTreeNodeList(); } // MeasureApp 和 OTSSolutionWindow 交互样品信息 //List TSampleParamList : //MeasureApp 给 SoluWindow 发送打开已存在的样品LIST (之前可能保存了多个样品) //CTreeSampleParam WorkSampleParam : 工作样品参数 (保存了多个样品,其中的一个是工作样品) public void Rev_MeasureApp_OpenExistSampleFile_Event(List TSampleParamList, CTreeSampleParam WorkSampleParam) { this.m_TreeViewBase.DisplayTreeViewData(TSampleParamList, WorkSampleParam); } //切换当前工作样品 //string sNewWorkSample : 新的工作样品名 public void Rev_MeasureApp_SwitchSample_Event(string sNewWorkSample) { m_TreeViewBase.SwitchWorkSample(sNewWorkSample); } // MeasureApp 和 OTSSolutionWindow 交互样品信息 //CWorkSampleParam WSampleParam : //MeasureApp 给 SoluWindow 发送添加样品的样品信息 public void AddSampleMeasure(CTreeSampleParam TSampleParam) { m_WorkSampleParam = TSampleParam; m_TreeViewBase.DisplayWorkSampleTree(m_WorkSampleParam); } //MeasureApp窗口给 OTSSolutionWindow 发送窗口删除样品回复 public void Rev_MeasureApp_DeletSample_Event(string sDeletSName) { treeView1.Nodes.Remove(m_WorkSampleNode); //移除当前工作样品 if ("" == sDeletSName) { return; } //重新获取Treeview上的工作样品节点 this.m_TreeViewBase.GetTreeWorkSampleNode(sDeletSName); //设置工作样品焦点 this.m_TreeViewBase.ReSetWorkSampleFoucs(); } // 新 //MeasureApp 给 OTSSolutionWindow 发送样品checkbox发生变化事件 public void Rev_MeasureApp_CheckboxStatuChange_Event(string sSampleName, bool bCheckBoxStatu) { m_TreeViewBase.SetSampleCheckboxStatu(sSampleName, bCheckBoxStatu); } // MeasureApp 窗口给 OTSSolutionWindow 窗口编辑样品名回复 public void Rev_MeasureApp_EditEditSample_Event(string sSampleOldName, string sSampleNewName) { if (sSampleNewName == "") //修改的样品新名称为空,则说明不允许修改 { return; } // 将Treeview里的修改的样品名修改为新样品名 // this.EditTreeSampleName(sSampleOldName, sSampleNewName); m_TreeViewBase.EditTreeNodeSampleName(sSampleOldName, sSampleNewName); } //修改样品名请求返回后,将样品名设置为新样品名 // 返回 True: 说明修改成功 bool bChangeNodeTextFlag = false; private void ChangeNodeText(TreeNode node, string sOldSName, string sNewSName) { // result.Add(node.Text); if (node.IsSelected) { if (node.Text == sOldSName) { node.Text = sNewSName; node.Text = " "; bChangeNodeTextFlag = true; } return; } if (node.Nodes.Count != 0) { for (int i = 0; i < node.Nodes.Count; i++) { if (!bChangeNodeTextFlag) { ChangeNodeText(node.Nodes[i], sOldSName, sNewSName); } } } } //删除测量样品 string csCheckNodeName = ""; public void DeleteNode_Click(object sender, EventArgs e) { if (m_bSetWorkSampleFlag) //设置工作样品成功,并可以删除当前工作样品 { string sDeleteSampleName = table["message1"].ToString(); sDeleteSampleName += m_WorkSampleNode.Text; if (DialogResult.OK == MessageBox.Show(sDeleteSampleName, "Tip", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { //删除样品成功 if (m_MeasureAppForm.DeleteSample(m_WorkSampleNode.Text)) { //选择节点 m_WorkSampleName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName(); m_TreeViewBase.GetTreeWorkSampleNode(m_WorkSampleName); //删除时重新初始化结果窗体结果 m_MeasureAppForm.m_MeasureRetWindow.SetInit(); } } } } //sOldSName: 修改样品的原名称 //sNewSName: 修改样品的新名称 private void GetNodeText(TreeNode node) { // result.Add(node.Text); if (node.Checked) { csCheckNodeName = node.Text; return; } if (node.Nodes.Count != 0) { for (int i = 0; i < node.Nodes.Count; i++) { if (csCheckNodeName == "") { GetNodeText(node.Nodes[i]); } } } } //左键 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { //鼠标选中 if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard) { if (e.Node.IsSelected) { //判断的选中的CHECKBOX和焦点都在当前被选择的节点上,切换当前工作样品 // m_DeleteOrLockTreeNode = e.Node; m_WorkSampleNode = e.Node; treeView1.SelectedNode = e.Node; //当前被选中 // OTSSolutionWindow窗口给MeasureApp窗口发送改变工作样品请求 treeView1.Refresh(); // OTSSolutionWindow窗口给MeasureApp窗口发送改变工作样品请求 //当选择的样品节点不是工作样品再切换 string sWorkName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName(); if ("" != sWorkName && (sWorkName != e.Node.Text)) { //m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(e.Node.Text); Rev_MeasureApp_SwitchSample_Event(e.Node.Text); } } } } //当Checkbox的状态发生变化时,响应事件 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { //OTSSolutionWindow 给 MeasureApp 发送样品checkbox状态由鼠标/键盘 触发状态变化事件 if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard) { // 切换工作样品 string sWorkName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName(); if ("" != sWorkName && (sWorkName != e.Node.Text)) { //m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(e.Node.Text); m_MeasureAppForm.SwitchWSample(e.Node.Text); } //OTSSolutionWindow 给 MeasureApp 发送样品checkbox发生变化事件 //m_MeasureAppForm.m_EventFun.Send_CheckBoxStatu_SoluWindowToMeasureAPP_Event_Fun(e.Node.Text, e.Node.Checked); m_MeasureAppForm.Rev_SoluWindowReqCheckBosStatuChange_Event(e.Node.Text, e.Node.Checked); } } [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TVITEM lParam); private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { e.DrawDefault = true; } private const int TVIF_STATE = 0x8; private const int TVIS_STATEIMAGEMASK = 0xF000; private const int TV_FIRST = 0x1100; private const int TVM_SETITEM = TV_FIRST + 63; private void HideCheckBox(TreeView tvw, TreeNode node) { TVITEM tvi = new TVITEM(); tvi.hItem = node.Handle; tvi.mask = TVIF_STATE; tvi.stateMask = TVIS_STATEIMAGEMASK; tvi.state = 0; SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi); } [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)] private struct TVITEM { public int mask; public IntPtr hItem; public int state; public int stateMask; [MarshalAs(UnmanagedType.LPTStr)] public string lpszText; public int cchTextMax; public int iImage; public int iSelectedImage; public int cChildren; public IntPtr lParam; } //只选择子类的复选框 //需要将样品测量参数编辑锁的属性 CheckOnClick 设置为True private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { NodeMouseClick(e); } protected void NodeMouseClick(TreeNodeMouseClickEventArgs e) { TreeNode tn = (TreeNode)e.Node; string sWorkName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName(); //m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(tn.Text); m_MeasureAppForm.SwitchWSample(tn.Text); //切换工作样品,通知Grid更新工作样品属性值 m_bSetWorkSampleFlag = true; m_WorkSampleNode = tn; if (e.Button == MouseButtons.Right)//判断按下鼠标右键 { if (!IsShowSysContextMenu) { } Point ClickPoint = new Point(e.X, e.Y); TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint); bool bLockStatu = false; //获取样品节点的测量参数锁状态 if (!m_TreeViewBase.GetSampleNodeLockStatu(ref bLockStatu)) { log.Error("(OTSSolutionWindow.treeView1_NodeMouseClick ) m_TreeViewBase.GetSampleNodeLockStatu(m_WorkSampleNode, ref bLockStatu) = false Failed !"); } SampleParaLock.Checked = bLockStatu; //显示清除测量数据 COTSSample cSample = m_MeasureAppForm.m_ProjParam.GetWorkSample(); if (cSample.GetName() == tn.Text) { //当前工作样品的测量区域 获取帧图信息 int CompletedFieldsCount = cSample.GetMsrStatus().GetCompletedFields(); if (CompletedFieldsCount > 0) { ClearMeasureData.Visible = true; } else { ClearMeasureData.Visible = false; } } CurrentNode.ContextMenuStrip = RightClickCheckContextMenuStrip; } //通过样品名称获取 m_MeasureAppForm.m_MeasureRetWindow.GetResultFileInfoBySampleName(m_WorkSampleNode.Text); } //Treeview增加测量样品节点 public void toolStripMenuItem1_AddNode_Click(object sender, EventArgs e) { // m_TreeViewBase.AddTreeViewNode(); //m_MeasureAppForm.m_EventFun.Send_AddSample_SoluWindowReqMeasureApp_Event_Fun(); m_MeasureAppForm.SoluwindowReqAddSample(); } private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (null == e.Node.Parent) { // e.Node.EndEdit(false); e.CancelEdit = true; return; } //修改样品名称 String csNewSampleName = e.Label; //获取新文本 string csOldSampleName = e.Node.Text;//获取原来的文本 if (null == csNewSampleName) //没有修改样品名 或者 样品名为空 { e.CancelEdit = true; return; } //样品名为空 if ("" == csNewSampleName) { e.CancelEdit = true; return; } string csNSName = csNewSampleName; //去除字符串首尾空格 csNSName = csNSName.Trim(); if (0 == csNSName.Length) { e.CancelEdit = true; return; } // m_MeasureAppForm.m_EventFun.Send_EditSample_SoluWindowReqMeasureApp_Event_Fun(csOldSampleName, csNewSampleName); if (!m_MeasureAppForm.EditWorkSampleName(csOldSampleName, csNSName)) { e.CancelEdit = true; } else { if (" " == csNewSampleName.Substring(0, 1) || " " == csNewSampleName.Substring(csNewSampleName.Length - 1, 1)) { e.CancelEdit = true; //m_MeasureAppForm.m_EventFun.Send_EditSample_SoluWindowRevMeasureApp_Event_Fun(csOldSampleName, csNSName); Rev_MeasureApp_EditEditSample_Event(csOldSampleName, csNSName); } else { e.Node.EndEdit(false); } } } //控制显示右键系统菜单 bool IsShowSysContextMenu = true; //修改选定的样品名的名字 private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e) { if (null == e.Node.Parent) { e.Node.EndEdit(false); e.CancelEdit = true; return; } string csSameNodeName = e.Node.Text; string csNewName = e.Label; //限制鼠标右键显示 IsShowSysContextMenu = false; } private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (null != e.Node.Parent) { e.Node.BeginEdit(); //进入修改状态 return; } //禁止修改UTILITYED e.Node.EndEdit(false); } // 清除样品测量数据 private void ClearSampleMeasureData_Click(object sender, EventArgs e) { if (m_bSetWorkSampleFlag) //设置工作样品成功,并可以删除当前工作样品 { string sDeleteSampleName = table["message1"].ToString(); sDeleteSampleName += m_WorkSampleNode.Text; if (DialogResult.OK == MessageBox.Show(sDeleteSampleName, "Tip", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)) { //1.删除样品测量信息 string mPathName = m_MeasureAppForm.m_ProjParam.GetResultData().GetPathName(); string smPathName = mPathName.Substring(0, mPathName.LastIndexOf("\\"))+"\\" + m_WorkSampleNode.Text; if (DeleteFile(smPathName)) { //2.删除样品节点信息 if (m_MeasureAppForm.DeleteSample(m_WorkSampleNode.Text)) { //选择节点 m_WorkSampleName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName(); m_TreeViewBase.GetTreeWorkSampleNode(m_WorkSampleName); } //保存测量文件 m_MeasureAppForm.m_ProjParam.GetResultData().Save(); //重新打开文件 } } } ClearMeasureData.Visible = false; } /// /// 根据路径删除文件 /// /// public bool DeleteFile(string path) { //try //{ // FileAttributes attr = File.GetAttributes(path); // if (attr == FileAttributes.Directory) // { // Directory.Delete(path, true); // } // else // { // File.Delete(path); // } // return true; //} //catch (Exception ex) //{ // return false; //} try { //去除文件夹和子文件的只读属性 //去除文件夹的只读属性 System.IO.DirectoryInfo fileInfo = new DirectoryInfo(path); fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory; //去除文件的只读属性 System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal); //判断文件夹是否还存在 if (Directory.Exists(path)) { foreach (string f in Directory.GetFileSystemEntries(path)) { if (File.Exists(f)) { using (FileStream fileStream = new FileStream(f, FileMode.Open, FileAccess.ReadWrite,FileShare.Delete)) { int byteLength = (int)fileStream.Length; byte[] fileBytes = new byte[byteLength]; fileStream.Read(fileBytes, 0, byteLength); //文件流关闭,文件解除锁定 fileStream.Close(); //如果有子文件删除文件 File.SetAttributes(f, FileAttributes.Normal); File.Delete(f); } } else { //循环递归删除子文件夹 DeleteFile(f); } } } return true; } catch (Exception ex) // 异常处理 { return false; } } //设置样品参数锁 private void SampleParaLock_Click(object sender, EventArgs e) { //m_MeasureAppForm.m_LogFunExport.TraceLog("(OTSSolutionWindow.SampleParaLock_Click) Set Sample Para Lock"); m_TreeViewBase.SetSampleParaLockStatu(); } private void treeView1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (!IsShowSysContextMenu) { return; } } } // treeView1_ItemDrag treeView1_ItemDrag treeView1_DragDrop ,treeView1_DragOver 四个事件函数是实现节点鼠标拖动移动的。另外需要将 Treeview控件属性 AllowDrop 设置为True private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) { // DoDragDrop(e.Item, DragDropEffects.Move); // Move the dragged node when the left mouse button is used. if (e.Button == MouseButtons.Left) { DoDragDrop(e.Item, DragDropEffects.Move); } // Copy the dragged node when the right mouse button is used. else if (e.Button == MouseButtons.Right) { DoDragDrop(e.Item, DragDropEffects.Copy); } } private void treeView1_DragEnter(object sender, DragEventArgs e) { e.Effect = e.AllowedEffect; } private void treeView1_DragDrop(object sender, DragEventArgs e) { //获得拖放中的节点 TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); //设置拖动节点为工作样品节点 string sWSampleName = moveNode.Text; if ("" == sWSampleName) { log.Error("(OTSSolutionWindow.treeView1_DragDrop ) moveNode.Text = null Failed !"); return; } //将拖动样品设置为工作样品 if (!m_MeasureAppForm.m_ProjParam.SetWorkSample(moveNode.Text)) { log.Error("(OTSSolutionWindow.treeView1_DragDrop ) m_MeasureAppForm.SetWorkSample(moveNode.Text) = false Failed !"); return; } //m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(moveNode.Text); Rev_MeasureApp_SwitchSample_Event(moveNode.Text); //根据鼠标坐标确定要移动到的目标节点 Point pt; TreeNode targeNode; pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y)); targeNode = this.treeView1.GetNodeAt(pt); //如果未获取到节点信息 则return if (targeNode == null) { treeView1.SelectedNode = moveNode; //m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(moveNode.Text); Rev_MeasureApp_SwitchSample_Event(moveNode.Text); return; } //如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端 TreeNode NewMoveNode = (TreeNode)moveNode.Clone(); if (targeNode != null) { if (targeNode.Nodes.Count == 0) { if (moveNode.Index > targeNode.Index) { treeView1.Nodes.Insert(targeNode.Index, NewMoveNode); } else { if (targeNode.Index + 1 == treeView1.Nodes.Count) { treeView1.Nodes.Add(NewMoveNode); } else { treeView1.Nodes.Insert(targeNode.Index+1, NewMoveNode); } } } else { // targeNode.Nodes.Insert(targeNode.Nodes.Count, NewMoveNode); return; } //更新当前拖动的节点选择 treeView1.SelectedNode = NewMoveNode; //展开目标节点,便于显示拖放效果 targeNode.Expand(); //移除拖放的节点 moveNode.Remove(); } else { if (moveNode.Parent != null) { moveNode.Parent.Nodes.Add(NewMoveNode); //移除拖放的节点 moveNode.Remove(); } } //节点位置移动后,TreeNodeList里的节点位置也应该发生变动 // m_TreeViewBase.SortTreeNodeList(); Rev_SoluWindow_SampleSortOrder_Event(); } // Select the node under the mouse pointer to indicate the // expected drop location. private void treeView1_DragOver(object sender, DragEventArgs e) { //获得拖放中的节点 //TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); // Retrieve the client coordinates of the mouse position. Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); // Select the node at the mouse position. treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint); // m_MeasureAppForm.m_EventFun.Send_SwithcWSample_SoluWindowReqMeasureApp_Event_Fun(moveNode.Text); } //拖动窗口移动响应事件 private void OTSSolutionWindow_Move(object sender, EventArgs e) { } private void OTSSolutionWindow_Paint(object sender, PaintEventArgs e) { m_TreeViewBase.ReSetWorkSampleFoucs(); } protected override void WndProc(ref Message m) { if (m.Msg != 0x007B) { base.WndProc(ref m); } } private void OTSSolutionWindow_SizeChanged(object sender, EventArgs e) { treeView1.Width = this.Width; } /// /// 屏蔽TreeView方向键 /// /// /// private void treeView1_KeyDown(object sender, KeyEventArgs e) { try { TreeView tv = (TreeView)sender; //获取子节点的总数 int nodeAllCount = treeView1.Nodes[0].Nodes.Count; //当前选择节点的索引位置 int currentNodeIndex = 0; currentNodeIndex = tv.SelectedNode.Index; switch (e.KeyCode) { case Keys.Up: if (currentNodeIndex == 0) { e.Handled = true; } else { for (int i = 0; i < treeView1.Nodes[0].Nodes.Count; i++) { if (i == currentNodeIndex) { treeView1.SelectedNode = treeView1.Nodes[0].Nodes[currentNodeIndex]; //通过样品名称获取 m_MeasureAppForm.m_MeasureRetWindow.GetResultFileInfoBySampleName(treeView1.Nodes[0].Nodes[currentNodeIndex - 1].Text); break; } } } break; case Keys.Down: if (nodeAllCount == currentNodeIndex + 1) { e.Handled = true; } else { for (int i = 0; i < treeView1.Nodes[0].Nodes.Count; i++) { if (i == currentNodeIndex) { treeView1.SelectedNode = treeView1.Nodes[0].Nodes[currentNodeIndex]; //通过样品名称获取 m_MeasureAppForm.m_MeasureRetWindow.GetResultFileInfoBySampleName(treeView1.Nodes[0].Nodes[currentNodeIndex + 1].Text); break; } } } break; case Keys.Left: e.Handled = true; break; case Keys.Right: e.Handled = true; break; } m_MeasureAppForm.m_SamplepaceWindow.KeyDowns(); } catch (Exception) { } } private void treeView1_KeyUp(object sender, KeyEventArgs e) { m_MeasureAppForm.m_SamplepaceWindow.KeyUps(); } private void reClassifyToolStripMenuItem_Click(object sender, EventArgs e) { } } }