using PaintDotNet.Base.CommTool; using PaintDotNet.DbOpreate.DbBll; using PaintDotNet.DbOpreate.DbModel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; namespace PaintDotNet.Instrument { /// /// 模板管理 /// public partial class TemplateManagerDialog : PdnBaseForm { private GroupBox groupBox1; private Button button5; private Label label1; private Button button2; private GroupBox groupBox2; private TreeView treeView1; private GroupBox groupBox3; private Panel panel1; private ListView listView1; private RadioButton radioButton2; private RadioButton radioButton1; private ContextMenuStrip contextMenuStrip1; private IContainer components; private ToolStripMenuItem toolStripMenuItem1; private ToolStripMenuItem toolStripMenuItem2; private ToolStripMenuItem toolStripMenuItem3; private Button button1; private ImageList imageList1; /// /// 模块列表数据集合 /// private List infosList; /// /// 新建窗口 /// private CreateNameDialog createNameDialog; /// /// 判断调用新建窗口时的创建模式 /// 1.新建分类 2.新建word 3.新建excel 0.默认 /// private int createMode = 0; public TemplateManagerDialog() { InitializeComponent(); InitializeLanguageText(); InitializeTreeView(); InitListViewHeader(); } /// /// 初始化右侧表头 /// private void InitListViewHeader() { ColumnHeader header = new ColumnHeader(); header.Text = PdnResources.GetString("Menu.name.text"); header.Width = this.listView1.Width / 3 - 7; this.listView1.Columns.Add(header); header = new ColumnHeader(); header.Text = PdnResources.GetString("Menu.Createtime.text"); header.Width = this.listView1.Width / 3 - 7; this.listView1.Columns.Add(header); header = new ColumnHeader(); header.Text = PdnResources.GetString("Menu.Type.text"); header.Width = this.listView1.Width / 3 - 7; this.listView1.Columns.Add(header); } /// /// 加载右侧数据 /// /// private void InitListViewData(TreeNode node) { this.listView1.Items.Clear(); this.imageList1.Images.Clear(); if (node != null) { mic_template_infos nowInfo = (mic_template_infos)node.Tag; string filePath = Application.StartupPath + "\\ModuleManage" + nowInfo.template_path;//获取文档路径 if (Directory.Exists(filePath)) { string[] fileNames = Directory.GetFiles(filePath); int j = 0;//由于可能存在非允许读取类型的文件,固不能使用循环内的下标 for (int i = 0; i < fileNames.Count(); i++) { //编辑时产生的临时文件不显示,尚未找到准确的判断规则 if (Path.GetFileName(fileNames[i]).Contains("~$")) continue; //只显示word和excel文件 if (FileOperationHelper.IsFileWordOrExcel(fileNames[i]) == 1) this.imageList1.Images.Add("img" + j, PdnResources.GetImageResource("Icons.WordType2.png").Reference); else if (FileOperationHelper.IsFileWordOrExcel(fileNames[i]) == 2) this.imageList1.Images.Add("img" + j, PdnResources.GetImageResource("Icons.ExcelType2.png").Reference); else continue; //大图模式和列表模式所需的参数同时添加 this.listView1.Items.Add("", j); this.listView1.Items[j].Tag = fileNames[i]; this.listView1.Items[j].ImageIndex = j; this.listView1.Items[j].Text = Path.GetFileNameWithoutExtension(fileNames[i]); this.listView1.Items[j].SubItems.Add(FileOperationHelper.GetFileCreationTime(fileNames[i])); if (FileOperationHelper.IsFileWordOrExcel(fileNames[i]) == 1) this.listView1.Items[j].SubItems.Add("word文档"); else this.listView1.Items[j].SubItems.Add("excel文档"); j++;//listview实际的下标 } } } } /// /// 初始化左侧的数据 /// private void InitializeTreeView() { this.infosList = mic_template_infos_BLL.FindAll(); this.infosList.Reverse();//查询出来是倒序的 this.treeView1.ImageList = new ImageList(); this.treeView1.ImageList.Images.Add("Catalog", PdnResources.GetImageResource("Icons.ImageFromDiskIcon.png").Reference); //绑定左侧控件 if (infosList.Count > 0) { for(int i = 0; i < infosList.Count; i++) { //顶级节点 if (infosList[i].parent_id == 0) { TreeNode anime = new TreeNode(); anime.Tag = infosList[i]; anime.Name = infosList[i].id.ToString(); if (infosList[i].template_type == 1) anime.Text = PdnResources.GetString(infosList[i].language_name);//不可删除的节点为自动自带,以通用方式读取名称 else anime.Text = infosList[i].language_name;//可删除的节点数据库中直接存储的显示名称 anime.ImageKey = "Catalog"; this.treeView1.Nodes.Add(anime); RecursiveData(anime); } } } } /// /// 递归处理treeview数据 /// private void RecursiveData(TreeNode anime) { List models = this.infosList.FindAll(a => a.parent_id == Convert.ToInt32(anime.Name)); if (models != null && models.Count > 0) { for (int i = 0; i < models.Count; i++) { TreeNode animeC = new TreeNode(); animeC.Tag = models[i]; if(models[i].template_type == 1) animeC.Text = PdnResources.GetString(models[i].language_name); else animeC.Text = models[i].language_name; animeC.Name = models[i].id.ToString(); anime.ImageKey = "Catalog"; RecursiveData(animeC); anime.Nodes.Add(animeC); } } } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.radioButton2.Text = PdnResources.GetString("Menu.tool.Templategement.Thumbnailmode.text"); this.radioButton1.Text = PdnResources.GetString("Menu.tool.Templatemanent.Listmode.text"); this.button5.Text = PdnResources.GetString("Menu.Edit.Delete.Text"); this.label1.Text = PdnResources.GetString("Menu.imageviewmode.text") + ":"; this.button2.Text = PdnResources.GetString("Menu.tool.Templategement.Newcategory.text"); this.button1.Text = PdnResources.GetString("Menu.Refresh.text"); this.groupBox2.Text = PdnResources.GetString("Menu.Help.HelpTopics.Text"); this.groupBox3.Text = PdnResources.GetString("Menu.tool.Templanagement.Templatedata.text"); this.toolStripMenuItem1.Text = PdnResources.GetString("Menu.Neworddocument.Text"); this.toolStripMenuItem2.Text = PdnResources.GetString("Menu.Edit.Delete.Text"); this.toolStripMenuItem3.Text = PdnResources.GetString("Menu.NewExceldocument.Text"); this.Text = PdnResources.GetString("Menu.Tools.TemplateManager.Text"); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.button5 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.treeView1 = new System.Windows.Forms.TreeView(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.panel1 = new System.Windows.Forms.Panel(); this.listView1 = new System.Windows.Forms.ListView(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.panel1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.radioButton2); this.groupBox1.Controls.Add(this.radioButton1); this.groupBox1.Controls.Add(this.button5); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(1102, 52); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.Checked = true; this.radioButton2.Location = new System.Drawing.Point(178, 23); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(83, 16); this.radioButton2.TabIndex = 11; this.radioButton2.TabStop = true; this.radioButton2.Text = "缩略图模式"; this.radioButton2.UseVisualStyleBackColor = true; this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.Location = new System.Drawing.Point(101, 23); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(71, 16); this.radioButton1.TabIndex = 10; this.radioButton1.TabStop = true; this.radioButton1.Text = "列表模式"; this.radioButton1.UseVisualStyleBackColor = true; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // button5 // this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.button5.Location = new System.Drawing.Point(1018, 18); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(75, 23); this.button5.TabIndex = 9; this.button5.Text = "删除"; this.button5.UseVisualStyleBackColor = true; this.button5.Click += new System.EventHandler(this.button5_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(6, 25); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(89, 12); this.label1.TabIndex = 4; this.label1.Text = "图片查看模式:"; // // button2 // this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.button2.Location = new System.Drawing.Point(856, 19); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "新建分类"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.button1.Location = new System.Drawing.Point(937, 19); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "刷新"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox2 // this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.groupBox2.Controls.Add(this.treeView1); this.groupBox2.Location = new System.Drawing.Point(12, 70); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(248, 523); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "目录"; // // treeView1 // this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText; this.treeView1.HideSelection = false; this.treeView1.Location = new System.Drawing.Point(7, 21); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(235, 496); this.treeView1.TabIndex = 0; this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode); this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick); // // groupBox3 // this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox3.Controls.Add(this.panel1); this.groupBox3.Location = new System.Drawing.Point(266, 70); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(848, 523); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "模板数据"; // // panel1 // this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.panel1.Controls.Add(this.listView1); this.panel1.Location = new System.Drawing.Point(7, 21); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(835, 496); this.panel1.TabIndex = 0; // // listView1 // this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.listView1.ContextMenuStrip = this.contextMenuStrip1; this.listView1.FullRowSelect = true; this.listView1.HideSelection = false; this.listView1.LargeImageList = this.imageList1; this.listView1.Location = new System.Drawing.Point(4, 4); this.listView1.MultiSelect = false; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(828, 489); this.listView1.TabIndex = 0; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.Listview1_MouseDoubleClick); this.listView1.Resize += new System.EventHandler(this.Listview1_Resize); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem1, this.toolStripMenuItem3, this.toolStripMenuItem2}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(69, 48); this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStrip1_Opening); // // toolStripMenuItem1 // this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(68, 22); this.toolStripMenuItem1.Click += new System.EventHandler(this.ToolStripMenuItem1_Click); // // toolStripMenuItem2 // this.toolStripMenuItem2.Name = "toolStripMenuItem2"; this.toolStripMenuItem2.Size = new System.Drawing.Size(68, 22); this.toolStripMenuItem2.Click += new System.EventHandler(this.ToolStripMenuItem2_Click); // // toolStripMenuItem3 // this.toolStripMenuItem3.Name = "toolStripMenuItem3"; this.toolStripMenuItem3.Size = new System.Drawing.Size(68, 22); this.toolStripMenuItem3.Click += new System.EventHandler(this.ToolStripMenuItem3_Click); // // imageList1 // this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imageList1.ImageSize = new System.Drawing.Size(72, 91); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; // // TemplateManagerDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.ClientSize = new System.Drawing.Size(1126, 605); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Name = "TemplateManagerDialog"; this.Text = "模板管理"; this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.Controls.SetChildIndex(this.groupBox3, 0); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); this.panel1.ResumeLayout(false); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); } /// /// 绘制节点事件 /// /// /// private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if ((e.State & TreeNodeStates.Selected) != 0) { //绿底白字 e.Graphics.FillRectangle(Brushes.Green, e.Node.Bounds); Font nodeFont = e.Node.NodeFont; if (nodeFont == null) nodeFont = ((TreeView)sender).Font; e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0)); } else { e.DrawDefault = true; } if ((e.State & TreeNodeStates.Focused) != 0) { using (Pen focusPen = new Pen(Color.Black)) { focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; Rectangle focusBounds = e.Node.Bounds; focusBounds.Size = new System.Drawing.Size(focusBounds.Width - 1, focusBounds.Height - 1); e.Graphics.DrawRectangle(focusPen, focusBounds); } } } /// /// 左侧树的节点点击事件 /// /// /// private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node != null) { if (this.treeView1.SelectedNode != e.Node) { this.treeView1.SelectedNode = e.Node; this.InitListViewData(this.treeView1.SelectedNode); } else { this.InitListViewData(this.treeView1.SelectedNode); } } } /// /// 列表/缩略图模式切换 /// /// /// private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (this.radioButton1.Checked) { this.listView1.View = View.Details; } else if (this.radioButton2.Checked) { this.listView1.View = View.LargeIcon; } } /// /// 新建分类 /// /// /// private void button2_Click(object sender, EventArgs e) { //需要硬编码判断,只能在常规操作下加分类 if (this.treeView1.SelectedNode != null) { mic_template_infos selectedInfo = (mic_template_infos)this.treeView1.SelectedNode.Tag; //只有第一个主节点,及其他之后添加的子节点,可以进行添加分类操作 if (selectedInfo.id == 1 || selectedInfo.template_type == 2|| selectedInfo.template_path ==@"/Automation") { this.createMode = 1; this.createNameDialog = new CreateNameDialog(this); this.createNameDialog.Text = PdnResources.GetString("Menu.tool.Templategement.Newcategory.text"); this.createNameDialog.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); this.createNameDialog.StartPosition = FormStartPosition.CenterParent; this.createNameDialog.ShowDialog(); this.createMode = 0;//创建模式赋值回初始值 } else MessageBox.Show(PdnResources.GetString("Menu.dinthegeneraloperationshomedirectory.Text")); } else MessageBox.Show(PdnResources.GetString("Menu.Pleaseselecadirectoryfirst.Text")); } /// /// 新建分类的名称 /// /// public override void GetCreateName(string name) { //创建分类执行的代码 if (this.createMode == 1) { mic_template_infos selectedInfo = (mic_template_infos)this.treeView1.SelectedNode.Tag; mic_template_infos newInfo = new mic_template_infos(); newInfo.parent_id = selectedInfo.id; newInfo.language_name = name; newInfo.template_type = 2; newInfo.template_path = selectedInfo.template_path + "/" + name; if (mic_template_infos_BLL.Add(newInfo)) { this.infosList.Add(newInfo); TreeNode anime = new TreeNode(); anime.Tag = newInfo; anime.Text = newInfo.language_name; anime.Name = newInfo.id.ToString(); this.treeView1.SelectedNode.Nodes.Add(anime); //选中刚才添加的节点 this.treeView1.SelectedNode = anime; InitListViewData(anime); } else MessageBox.Show(PdnResources.GetString("Menu.ategoryadditionfailed.Text")); } //创建word文件执行的代码 else if (this.createMode == 2) { mic_template_infos selectedInfo = (mic_template_infos)this.treeView1.SelectedNode.Tag; string dictPath = Application.StartupPath + "\\ModuleManage" + selectedInfo.template_path;//文件保存路径 if (FileOperationHelper.IsFileNameExist(name, dictPath)) { MessageBox.Show(PdnResources.GetString("Menu.namealreadyexistsinthisirectoryPleas.Text")); return; } string wordFilePath = dictPath + "\\" + name + ".docx";//文件名称全路径 if (OfficeFileHandleHelper.CreateNewWordFile(dictPath, wordFilePath, false)) { int addIndex = this.listView1.Items.Count;//listview添加新项目的下标 //添加item if (FileOperationHelper.IsFileWordOrExcel(wordFilePath) == 1) this.imageList1.Images.Add("img" + addIndex, PdnResources.GetImageResource("Icons.WordType2.png").Reference); else if (FileOperationHelper.IsFileWordOrExcel(wordFilePath) == 2) this.imageList1.Images.Add("img" + addIndex, PdnResources.GetImageResource("Icons.ExcelType2.png").Reference); else { MessageBox.Show(PdnResources.GetString("Menu.Filecreationfailedinternalerror.Text")); this.createNameDialog.Close(); return; } this.listView1.Items.Add("", addIndex); this.listView1.Items[addIndex].Tag = wordFilePath; this.listView1.Items[addIndex].ImageIndex = addIndex; this.listView1.Items[addIndex].Text = Path.GetFileNameWithoutExtension(wordFilePath); this.listView1.Items[addIndex].SubItems.Add(FileOperationHelper.GetFileCreationTime(wordFilePath)); if (FileOperationHelper.IsFileWordOrExcel(wordFilePath) == 1) this.listView1.Items[addIndex].SubItems.Add(PdnResources.GetString("Menu.Worddocument.Text")); else this.listView1.Items[addIndex].SubItems.Add(PdnResources.GetString("Menu.Exceldocument.Text")); //选中刚才添加的文件 this.listView1.Items[addIndex].Selected = true; this.listView1.Items[addIndex].EnsureVisible(); } else MessageBox.Show(PdnResources.GetString("Menu.Pleasecheckwhetherofficeeriessoftwareis.Text")); } else if (this.createMode == 3) { mic_template_infos selectedInfo = (mic_template_infos)this.treeView1.SelectedNode.Tag; string dictPath = Application.StartupPath + "\\ModuleManage" + selectedInfo.template_path;//文件保存路径 if (FileOperationHelper.IsFileNameExist(name, dictPath)) { MessageBox.Show(PdnResources.GetString("Menu.namealreadyexistsinthisirectoryPleas.Text")); return; } string excelFilePath = dictPath + "\\" + name + ".xlsx";//文件名称全路径 if (OfficeFileHandleHelper.CreateNewExcelFile(dictPath, excelFilePath, false)) { int addIndex = this.listView1.Items.Count;//listview添加新项目的下标 //添加item if (FileOperationHelper.IsFileWordOrExcel(excelFilePath) == 1) this.imageList1.Images.Add("img" + addIndex, PdnResources.GetImageResource("Icons.WordType2.png").Reference); else if (FileOperationHelper.IsFileWordOrExcel(excelFilePath) == 2) this.imageList1.Images.Add("img" + addIndex, PdnResources.GetImageResource("Icons.ExcelType2.png").Reference); else { MessageBox.Show(PdnResources.GetString("Menu.Filecreationfailedinternalerror.Text")); this.createNameDialog.Close(); return; } this.listView1.Items.Add("", addIndex); this.listView1.Items[addIndex].Tag = excelFilePath; this.listView1.Items[addIndex].ImageIndex = addIndex; this.listView1.Items[addIndex].Text = Path.GetFileNameWithoutExtension(excelFilePath); this.listView1.Items[addIndex].SubItems.Add(FileOperationHelper.GetFileCreationTime(excelFilePath)); if (FileOperationHelper.IsFileWordOrExcel(excelFilePath) == 1) this.listView1.Items[addIndex].SubItems.Add(PdnResources.GetString("Menu.Worddocument.Text")); else this.listView1.Items[addIndex].SubItems.Add(PdnResources.GetString("Menu.Exceldocument.Text")); //选中刚才添加的文件 this.listView1.Items[addIndex].Selected = true; this.listView1.Items[addIndex].EnsureVisible(); } else MessageBox.Show(PdnResources.GetString("Menu.Pleasecheckwhetherofficeeriessoftwareis.Text")); } else { } this.createNameDialog.Close(); } /// /// 刷新 /// /// /// private void button1_Click(object sender, EventArgs e) { if (this.treeView1.SelectedNode != null) { this.InitListViewData(this.treeView1.SelectedNode); } else MessageBox.Show(PdnResources.GetString("Menu.Pleaseselecadirectoryfirst.Text")); } /// /// 删除分类 /// /// /// private void button5_Click(object sender, EventArgs e) { if (this.treeView1.SelectedNode != null) { mic_template_infos selectedInfo = (mic_template_infos)this.treeView1.SelectedNode.Tag; TreeNode fatherNode = this.treeView1.SelectedNode.Parent;//父节点 if (selectedInfo.template_type == 2) { if (MessageBox.Show(PdnResources.GetString("Menu.classificatioandfileAllthesubcategorieand.Text")+"?", PdnResources.GetString("Menu.Thisdeletioncannotberecovered.text"), MessageBoxButtons.YesNo) == DialogResult.Yes) { if (FileOperationHelper.IsFileOpened(Application.StartupPath + "\\ModuleManage" + selectedInfo.template_path) == 1) { MessageBox.Show(PdnResources.GetString("Menu.osubcategorinuseandcannotbeand.Text")); return; } List deleteList = new List();//整理所有需删除的节点与子节点集合 deleteList.Add(selectedInfo); foreach (TreeNode node in this.treeView1.SelectedNode.Nodes) { deleteList.Add((mic_template_infos)node.Tag); } FileOperationHelper.DeleteFolder(Application.StartupPath + "\\ModuleManage" + selectedInfo.template_path);//删除本地文件 for (int i = 0; i < deleteList.Count; i++) { mic_template_infos_BLL.Del(deleteList[i].id);//删数据库 this.infosList.Remove(this.infosList.Find(a => a.id == deleteList[i].id));//全局对象中移除 } this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);//树中移除 //当前删除节点有父节点,则选中其父节点并刷新右侧窗口 if(fatherNode != null) { this.treeView1.SelectedNode = fatherNode; InitListViewData(this.treeView1.SelectedNode); } else { this.treeView1.SelectedNode = null; this.listView1.Items.Clear(); } } } else MessageBox.Show(PdnResources.GetString("Menu.Thecategorycannotbedeleted.Text")); } else MessageBox.Show(PdnResources.GetString("Menu.Pleaseselecadirectoryfirst.Text")); } /// /// 双击打开文件 /// /// /// private void Listview1_MouseDoubleClick(object sender, MouseEventArgs e) { ListViewHitTestInfo info = this.listView1.HitTest(e.X, e.Y);//鼠标双击选中的文件 if (info.Item != null) if (!OfficeFileHandleHelper.OpenOfficeFile(info.Item.Tag.ToString())) MessageBox.Show(PdnResources.GetString("Menu.thefilePleasecheckwhethertheofficeseriess.Text")); } /// /// 设置右键菜单的按钮是否可点击 /// /// /// private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e) { if(this.treeView1.SelectedNode != null) { var selectedView = this.listView1.SelectedItems; //删除可点击 if (selectedView.Count > 0) { this.toolStripMenuItem1.Enabled = false; this.toolStripMenuItem2.Enabled = true; } //新建可点击 else { this.toolStripMenuItem1.Enabled = true; this.toolStripMenuItem2.Enabled = false; } if (this.treeView1.SelectedNode.Text.Equals(PdnResources.GetString("Template.Manager.item1.MeasureListAll"))) { this.toolStripMenuItem1.Enabled = false; this.toolStripMenuItem3.Enabled = true; } else { this.toolStripMenuItem1.Enabled = true; this.toolStripMenuItem3.Enabled = false; } } //新建和删除都不可点击 else { this.toolStripMenuItem1.Enabled = false; this.toolStripMenuItem2.Enabled = false; this.toolStripMenuItem3.Enabled = false; } } /// /// 右键菜单的新建word /// /// /// private void ToolStripMenuItem1_Click(object sender, EventArgs e) { this.createMode = 2; this.createNameDialog = new CreateNameDialog(this); this.createNameDialog.Text = PdnResources.GetString("Menu.Neworddocument.Text"); this.createNameDialog.textBox1.MaxLength = 20; this.createNameDialog.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); this.createNameDialog.StartPosition = FormStartPosition.CenterParent; this.createNameDialog.ShowDialog(); this.createMode = 0;//创建模式赋值回初始值 } /// /// 右键菜单的删除 /// /// /// private void ToolStripMenuItem2_Click(object sender, EventArgs e) { var selectedView = this.listView1.SelectedItems; if (selectedView.Count < 1) return; if (MessageBox.Show(PdnResources.GetString("Menu.Confirmtodeletetheselected.Text") + selectedView.Count + PdnResources.GetString("Menu.Documents.Text")+"?", PdnResources.GetString("Menu.Thisdeletioncannotberecovered.text"), MessageBoxButtons.YesNo) == DialogResult.Yes) { foreach(ListViewItem view in selectedView) { string filePath = view.Tag.ToString();//全路径 if (System.IO.File.Exists(filePath)) { try { System.IO.File.Delete(filePath); this.listView1.Items.Remove(view); } catch (Exception) { MessageBox.Show(PdnResources.GetString("Menu.Filedeletionfailed.Text")); } } else { MessageBox.Show(PdnResources.GetString("Menu.File.Text")+"\"" + view.Text + "\""+ PdnResources.GetString("Menu.nonexistent.Text")); } } } } /// /// 右键菜单的新建word /// /// /// private void ToolStripMenuItem3_Click(object sender, EventArgs e) { this.createMode = 3; this.createNameDialog = new CreateNameDialog(this); this.createNameDialog.Text = PdnResources.GetString("Menu.NewExceldocument.Text"); this.createNameDialog.textBox1.MaxLength = 20; this.createNameDialog.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); this.createNameDialog.StartPosition = FormStartPosition.CenterParent; this.createNameDialog.ShowDialog(); this.createMode = 0;//创建模式赋值回初始值 } /// /// 弹窗限制输入新建文件的非法字符与空格 /// /// /// private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char[] illegalCar = new char[] { '\\', '/', '?', '\"', '<', '>', ':', '|', '*'}; if (Array.IndexOf(illegalCar, e.KeyChar) != -1 || e.KeyChar == 32)             { e.Handled = true; } } /// /// 重置listview的列宽 /// /// /// private void Listview1_Resize(object sender, EventArgs e) { foreach(ColumnHeader column in this.listView1.Columns) { column.Width = this.listView1.Width / 3 - 7; } } } }