| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 | 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<OTSSampleVisualPropertyInfo> m_TreeSampleInfoList = new List<OTSSampleVisualPropertyInfo>();        //保存Treeview所有样品节点信息                                                                                     //当前工作样品名        public String m_WorkSampleName = "";        //工作样品属性参数        public CTreeSampleParam m_WorkSampleParam = new CTreeSampleParam();        //国际化        OTSCommon.Language lan;        Hashtable table;        NLog.Logger log;        public OTSSolutionWindow(OTSIncAMeasureAppForm MeasureAppForm)        {            InitializeComponent();            m_MeasureAppForm = MeasureAppForm;            m_TreeViewBase = new OTSDisplayTreeBase(this);            //国际化            lan = new OTSCommon.Language(this);            table = lan.GetNameTable(this.Name);            m_DefaultSolutionName = table["m_defaultsolutionname"].ToString();            m_DefaultSampleName = table["m_defaultsamplename"].ToString();                }        private void OTSSolutionWindow_Load(object sender, EventArgs e)        {            //TreeView可编辑状态。             treeView1.LabelEdit = true;            this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;           log = NLog.LogManager.GetCurrentClassLogger();        }                  public void Rev_SoluWindow_SampleSortOrder_Event()        {            m_TreeViewBase.SortTreeNodeList();        }          public void DisplayExistSampleFile(List<CTreeSampleParam> TSampleParamList, CTreeSampleParam WorkSampleParam)        {            this.m_TreeViewBase.DisplayTreeViewData(TSampleParamList, WorkSampleParam);        }        public void SwitchSample(string sNewWorkSample)        {            m_TreeViewBase.SwitchWorkSample(sNewWorkSample);        }        public void AddSampleMeasure(CTreeSampleParam TSampleParam)        {            m_WorkSampleParam = TSampleParam;            m_TreeViewBase.DisplayWorkSampleTree(m_WorkSampleParam);        }               public void DeletCurrentSampleNode()        {            treeView1.Nodes.Remove(m_WorkSampleNode);   //移除当前工作样品        }        public void DisplaySample(string sDeletSName)        {            if ("" == sDeletSName)            {                return;            }            //重新获取Treeview上的工作样品节点            this.m_TreeViewBase.GetTreeWorkSampleNode(sDeletSName);            //设置工作样品焦点            this.m_TreeViewBase.ReSetWorkSampleFoucs();        }        public void Rev_MeasureApp_CheckboxStatuChange_Event(string sSampleName, bool bCheckBoxStatu)        {            m_TreeViewBase.SetSampleCheckboxStatu(sSampleName, bCheckBoxStatu);        }               public void Rev_MeasureApp_EditEditSample_Event(string sSampleOldName, string sSampleNewName)        {            if (sSampleNewName == "")   //修改的样品新名称为空,则说明不允许修改            {                return;            }            m_TreeViewBase.EditTreeNodeSampleName(sSampleOldName, sSampleNewName);        }               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))                {                    //删除样品成功                    m_MeasureAppForm.DeleteSample(m_WorkSampleNode.Text);                                       //选择节点                    m_WorkSampleName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName();                    m_TreeViewBase.GetTreeWorkSampleNode(m_WorkSampleName);                    //删除时重新初始化结果窗体结果                    m_MeasureAppForm.m_MeasureResultWindow.SetInit();                                   }            }        }        //sOldSName:  修改样品的原名称        //sNewSName: 修改样品的新名称        private void GetNodeText(TreeNode node)        {                       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_WorkSampleNode = e.Node;                    treeView1.SelectedNode = e.Node;    //当前被选中                                                        // OTSSolutionWindow窗口给MeasureApp窗口发送改变工作样品请求                    treeView1.Refresh();                                       string sWorkName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName();                    if ("" != sWorkName && (sWorkName != e.Node.Text))                    {                                            SwitchSample(e.Node.Text);                    }                }            }          }        //当Checkbox的状态发生变化时,响应事件        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)        {            if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)            {                // 切换工作样品                string sWorkName = m_MeasureAppForm.m_ProjParam.GetWorkSampleName();                if ("" != sWorkName && (sWorkName != e.Node.Text))                {                                  m_MeasureAppForm.SwitchWSample(e.Node.Text);                }                       m_MeasureAppForm.SoluWindowCheckBoxStatuChange(e.Node.Text, e.Node.Checked);            }        }                private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)        {            e.DrawDefault = true;        }               [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;                                 m_MeasureAppForm.SwitchWSample(tn.Text);                //切换工作样品,通知Grid更新工作样品属性值                m_bSetWorkSampleFlag = true;                        m_WorkSampleNode = tn;            if (e.Button == MouseButtons.Right)//判断按下鼠标右键            {                                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)                {                                       ClearMeasureData.Visible = false;                                   }                CurrentNode.ContextMenuStrip = RightClickCheckContextMenuStrip;            }            //通过样品名称获取            m_MeasureAppForm.m_MeasureResultWindow.UpdateResultInfoBySampleName(m_WorkSampleNode.Text);        }        //Treeview增加测量样品节点        public void toolStripMenuItem1_AddNode_Click(object sender, EventArgs e)        {                m_MeasureAppForm.SoluwindowReqAddSample();        }        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)        {            if (null == e.Node.Parent)            {                               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;            }             if (!m_MeasureAppForm.EditWorkSampleName(csOldSampleName, csNSName))            {                e.CancelEdit = true;            }            else            {                if (" " == csNewSampleName.Substring(0, 1) || " " == csNewSampleName.Substring(csNewSampleName.Length - 1, 1))                {                    e.CancelEdit = true;                                       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;            }            //限制鼠标右键显示            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))                    {                      var sam=  m_MeasureAppForm.m_ProjParam.GetResultData().GetSampleByName(m_WorkSampleNode.Text);                        sam.GetMsrStatus().ClearCompletedFieldsInfo();                        sam.ClearFields();                        sam.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.UNMEASURED);                                           //保存测量文件                        m_MeasureAppForm.m_ProjParam.GetResultData().Save();                        //重新打开文件                    }                }            }            MessageBox.Show("The sample data has been cleared!");            ClearMeasureData.Visible = false;        }        /// <summary>        /// 根据路径删除文件        /// </summary>        /// <param name="path"></param>        public bool DeleteFile(string path)        {                       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_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)        {                     // 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);            SwitchSample(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);                SwitchSample(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;        }              /// <summary>        /// 屏蔽TreeView方向键        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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_MeasureResultWindow.UpdateResultInfoBySampleName(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_MeasureResultWindow.UpdateResultInfoBySampleName(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.OTSSamplespaceWindow_KeyDown(sender, e);            }            catch (Exception)            {            }        }        private void treeView1_KeyUp(object sender, KeyEventArgs e)        {            m_MeasureAppForm.m_SamplepaceWindow.KeyUps();        }        private void reClassifyToolStripMenuItem_Click(object sender, EventArgs e)        {        }    }}
 |