using PaintDotNet.Base.CommTool; using PaintDotNet.Base.Functionodel; using PaintDotNet.DbOpreate.DbBll; using PaintDotNet.DbOpreate.DbModel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using APIDialogs = Microsoft.WindowsAPICodePack.Dialogs; namespace PaintDotNet.Instrument { /// /// 生成报告 /// internal class CreateReportDialog : PdnBaseForm { private GroupBox groupBox2; private GroupBox groupBox3; private TreeView treeView1; private Panel panel1; private Label label1; private TextBox textBox1; private Button button1; private Button button3; private Button button2; private GroupBox groupBox1; private AppWorkspace appWorkspace; /// /// 当前已打开的图片集合 /// private List bitmapList; /// /// 模板分类数据集合 /// private List infosList; /// /// 图片控件判断鼠标是否按下 /// private bool isMouseDown = false; /// /// 图片控件的location /// private Point picboxLocation; /// /// 鼠标的按下位置 /// private Point mouseOffset; /// /// 图片控件拖动前的location /// private Point oldPictureboxLocation; /// /// 图片控件拖动前的tag值 /// private int oldPictureboxTag; public CreateReportDialog(AppWorkspace appWorkspace) { this.appWorkspace = appWorkspace; this.infosList = mic_template_infos_BLL.FindAll(); this.infosList.Reverse();//查询出来是倒序的 InitializeComponent(); InitializeLanguageText(); InitializePicturesTools(); InitializeTreeView(); } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.button3.Text = PdnResources.GetString("Menu.cancel.text"); this.button2.Text = PdnResources.GetString("Menu.tool.Generatereport.generate.text"); this.label1.Text = PdnResources.GetString("Menu.ReportStoragePath.Text") + ":"; this.groupBox2.Text = PdnResources.GetString("Menu.tool.Generatereport.Templateselection.text"); this.groupBox3.Text = PdnResources.GetString("Menu.tool.Generatereport.imageselect.text"); this.Text = PdnResources.GetString("Menu.Tools.CreateReport.Text"); } /// /// 初始化基础控件 /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button3 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); 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.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.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.button3); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.textBox1); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(13, 13); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(806, 64); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // button3 // this.button3.Location = new System.Drawing.Point(700, 23); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 4; this.button3.Text = "取消"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button2 // this.button2.Location = new System.Drawing.Point(605, 23); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 3; this.button2.Text = "生成"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.BackColor = System.Drawing.SystemColors.Window; this.button1.Location = new System.Drawing.Point(453, 25); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(29, 21); this.button1.TabIndex = 2; this.button1.Text = "..."; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.BackColor = System.Drawing.SystemColors.Control; this.textBox1.Location = new System.Drawing.Point(126, 25); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(330, 21); this.textBox1.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(42, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(89, 12); this.label1.TabIndex = 0; this.label1.Text = "报告存储路径:"; // // 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(13, 84); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(169, 393); this.groupBox2.TabIndex = 1; 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(156, 366); this.treeView1.TabIndex = 0; this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode); // // 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(189, 84); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(630, 393); this.groupBox3.TabIndex = 2; this.groupBox3.TabStop = false; this.groupBox3.Text = "图片选择"; // // panel1 // this.panel1.AutoScroll = true; this.panel1.BackColor = System.Drawing.Color.Transparent; this.panel1.Location = new System.Drawing.Point(7, 21); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(617, 365); this.panel1.TabIndex = 0; // // CreateReportDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.ClientSize = new System.Drawing.Size(831, 486); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "CreateReportDialog"; 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.ResumeLayout(false); } /// /// 获取当前软件已打开的图片集合 /// private void GetBitListFromWorkspace() { if (this.appWorkspace.DocumentWorkspaces != null && this.appWorkspace.DocumentWorkspaces.Count() > 0) { this.bitmapList = new List(); foreach (DocumentWorkspace workspace in this.appWorkspace.DocumentWorkspaces) { if (workspace.CompositionSurface != null) { string extension = Path.GetExtension(workspace.GetFriendlyName());//获得图片后缀 Bitmap oriBit = workspace.CompositionSurface.CreateAliasedBitmap(); using (Bitmap dstBitmap = oriBit.Clone(new Rectangle(0, 0, oriBit.Width, oriBit.Height), oriBit.PixelFormat)) { Graphics graphics = Graphics.FromImage(dstBitmap); if (workspace.GraphicsList != null && workspace.GraphicsList.Count > 0) { workspace.GraphicsList.UnselectAll();//取消标注和测量的选中状态,不然会绘制上外圈矩形 workspace.GraphicsList.Draw(graphics); } if (workspace.GridLineEnabled) DrawRulerHelper.drawGrid(Startup.instance.gridModel, graphics, workspace.CompositionSurface.Width, workspace.CompositionSurface.Height); if (workspace.GridLineFullEnabled) DrawRulerHelper.drawGridFull(Startup.instance.gridModel, graphics, workspace.CompositionSurface.Width, workspace.CompositionSurface.Height); if (workspace.GridRectangleEnabled) DrawRulerHelper.drawGridRectangle(Startup.instance.gridModel, graphics, workspace.CompositionSurface.Width, workspace.CompositionSurface.Height, workspace.MicronRatio); if (workspace.GridRoundEnabled) DrawRulerHelper.drawGridRound(Startup.instance.gridModel, graphics, workspace.CompositionSurface.Width, workspace.CompositionSurface.Height, workspace.MicronRatio); if (workspace.GridCrossCurveEnabled) DrawRulerHelper.drawGridCrossCurve(Startup.instance.gridModel, graphics, workspace.CompositionSurface.Width, workspace.CompositionSurface.Height); //gdi处理后以流的形式存储 MemoryStream ms = new MemoryStream(); switch (extension) { case ".jpg": dstBitmap.Save(ms, ImageFormat.Jpeg); break; case ".jpeg": dstBitmap.Save(ms, ImageFormat.Jpeg); break; case ".png": dstBitmap.Save(ms, ImageFormat.Png); break; case ".bmp": dstBitmap.Save(ms, ImageFormat.Bmp); break; case ".gif": dstBitmap.Save(ms, ImageFormat.Gif); break; default: dstBitmap.Save(ms, ImageFormat.Jpeg); break; } this.bitmapList.Add(new Bitmap(Image.FromStream(ms))); ms.Dispose(); } } } } } /// /// 初始化图片 /// private void InitializePicturesTools() { GetBitListFromWorkspace(); if (this.bitmapList != null && this.bitmapList.Count > 0) { int colNum = 5;//列数固定 int rowNum = this.bitmapList.Count / colNum + 1;//行数 Point startPoint = new Point(0, 4); int marginTB = 7;//固定的上下边距 int pictureNum = 1;//图片顺序 for (int i = 0; i < rowNum; i++) { for (int j = 0; j < colNum; j++) { PictureBox pictureBox = new PictureBox(); pictureBox.Size = new Size(116, 116);//固定尺寸 pictureBox.Location = new Point((120 * j), startPoint.Y + ((116 + marginTB) * i)); pictureBox.BackColor = Color.White; pictureBox.Tag = pictureNum; pictureBox.MouseDown += new MouseEventHandler(picturebox_MouseDown); pictureBox.MouseMove += new MouseEventHandler(picturebox_MouseMove); pictureBox.MouseUp += new MouseEventHandler(picturebox_MouseUp); if (this.bitmapList[pictureNum - 1].Width > pictureBox.Size.Width || this.bitmapList[pictureNum - 1].Height > pictureBox.Size.Height) { pictureBox.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBox.SizeMode = PictureBoxSizeMode.CenterImage; } pictureBox.Image = this.bitmapList[pictureNum - 1]; CheckBox checkBox = new CheckBox(); checkBox.Size = new Size(14, 14); checkBox.Location = new Point(102, 102); checkBox.Checked = true; pictureBox.Controls.Add(checkBox); this.panel1.Controls.Add(pictureBox); pictureNum++; if (pictureNum > this.bitmapList.Count) return; } } } } /// /// 初始化左侧树 /// private void InitializeTreeView() { if (this.infosList != null && this.infosList.Count > 0) { mic_template_infos manageInfo = this.infosList.Find(a => a.language_name == "Template.Manager.item1");//固定查询常规操作 this.treeView1.ImageList = new ImageList(); this.treeView1.ImageList.Images.Add("Catalog", PdnResources.GetImageResource("Icons.ImageFromDiskIcon.png").Reference); this.treeView1.ImageList.Images.Add("Word", PdnResources.GetImageResource("Icons.WordType2Small.png").Reference); //路径不存在就创建一个出来 string managePath = Application.StartupPath + "\\ModuleManage" + manageInfo.template_path; if (!Directory.Exists(managePath)) Directory.CreateDirectory(managePath); TreeNode anime = new TreeNode(); anime.Tag = 1;//1代表文件夹 anime.Text = PdnResources.GetString(manageInfo.language_name); anime.Name = managePath; anime.ImageKey = "Catalog"; this.treeView1.Nodes.Add(anime); addTreeChildNode(anime, manageInfo); anime.Expand();//默认展开主节点 } } /// /// 递归添加叔的子节点 /// /// /// private void addTreeChildNode(TreeNode anime, mic_template_infos manageInfo) { List nodesList = this.infosList.FindAll(a => a.parent_id == manageInfo.id); if(nodesList != null && nodesList.Count > 0) { for(int i = 0; i < nodesList.Count; i++) { string floderPath = Application.StartupPath + "\\ModuleManage" + nodesList[i].template_path; TreeNode floderAnime = new TreeNode(); floderAnime.Tag = 1;//1代表文件夹 if (nodesList[i].template_type == 1) floderAnime.Text = PdnResources.GetString(nodesList[i].language_name);//不可删除的节点为数据库自带,以通用方式读取名称 else floderAnime.Text = nodesList[i].language_name;//可删除的节点数据库中直接存储的显示名称 floderAnime.Name = floderPath; floderAnime.ImageKey = "Catalog"; floderAnime.SelectedImageKey = "Catalog"; anime.Nodes.Add(floderAnime); addTreeChildNode(floderAnime, nodesList[i]); } } string filePath = Application.StartupPath + "\\ModuleManage" + manageInfo.template_path;//获取文档路径 if (Directory.Exists(filePath)) { string[] fileNames = Directory.GetFiles(filePath); for (int i = 0; i < fileNames.Count(); i++) { //编辑时产生的临时文件不显示,尚未找到准确的判断规则 if (Path.GetFileName(fileNames[i]).Contains("~$")) continue; //只显示word文件 if (FileOperationHelper.IsFileWordOrExcel(fileNames[i]) == 1) { TreeNode fileAnime = new TreeNode(); fileAnime.Tag = 2;//2代表模板文件 fileAnime.Text = Path.GetFileNameWithoutExtension(fileNames[i]); fileAnime.Name = fileNames[i]; fileAnime.ImageKey = "Word"; fileAnime.SelectedImageKey = "Word"; anime.Nodes.Add(fileAnime); } else continue; } } } /// /// 树的绘制节点事件 /// /// /// 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 picturebox_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; PictureBox movedPictureBox = (PictureBox)sender; movedPictureBox.Cursor = Cursors.SizeAll; picboxLocation = movedPictureBox.Location; oldPictureboxLocation = movedPictureBox.Location; oldPictureboxTag = Convert.ToInt32(movedPictureBox.Tag); mouseOffset = Control.MousePosition; } } /// /// 图片控件的鼠标抬起事件 /// /// /// private void picturebox_MouseUp(object sender, MouseEventArgs e) { if (!isMouseDown) return; isMouseDown = false; PictureBox movedPictureBox = (PictureBox)sender; List listPicturebox = new List(); foreach (Control C in this.panel1.Controls) { if (C is PictureBox && (PictureBox)C != movedPictureBox) { listPicturebox.Add((PictureBox)C); } } if (listPicturebox != null && listPicturebox.Count > 0) { for (int i = 0; i < listPicturebox.Count; i++) { Point toolScreenLocation = listPicturebox[i].Location; int toolTag = Convert.ToInt32(listPicturebox[i].Tag); Rectangle toolScreenRectangle = new Rectangle(toolScreenLocation.X, toolScreenLocation.Y, 116, 116);//获得图片控件对应的矩形区域 Point nowPoint = this.panel1.PointToClient(Control.MousePosition);//将屏幕鼠标点转换为控件坐标点 if (toolScreenRectangle.Contains(nowPoint) && toolTag != oldPictureboxTag) { //替换两个控件的location和tag movedPictureBox.Location = toolScreenLocation; movedPictureBox.Tag = toolTag; listPicturebox[i].Location = oldPictureboxLocation; listPicturebox[i].Tag = oldPictureboxTag; this.panel1.Refresh(); return; } } } //如果鼠标点不与其他自定义控件重合,则当前拖动控件回到原始点 movedPictureBox.Location = oldPictureboxLocation; } /// /// 图片控件的鼠标拖动事件 /// /// /// private void picturebox_MouseMove(object sender, MouseEventArgs e) { int _x = 0; int _y = 0; if (isMouseDown) { Point pt = Control.MousePosition; _x = mouseOffset.X - pt.X; _y = mouseOffset.Y - pt.Y; PictureBox movedPictureBox = (PictureBox)sender; movedPictureBox.Cursor = Cursors.SizeAll; movedPictureBox.Location = new Point(picboxLocation.X - _x, picboxLocation.Y - _y); } } /// /// 取消按钮 /// /// /// private void button3_Click(object sender, EventArgs e) { this.Close(); } /// /// 生成按钮 /// /// /// private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.textBox1.Text)) { MessageBox.Show(PdnResources.GetString("Menu.leaseselectthestoragepathfortherep.Text")); return; } if (!Directory.Exists(this.textBox1.Text)) { MessageBox.Show(PdnResources.GetString("Menu.eselectedstoragepathdoesnotexi.Text")); return; } if(this.treeView1.SelectedNode == null) { MessageBox.Show(PdnResources.GetString("Menu.seselectareporttemplateontheleft.Text")); return; } int selectedType = this.treeView1.SelectedNode.Tag == null ? 0 : Convert.ToInt32(this.treeView1.SelectedNode.Tag); if(selectedType != 2)//选择的不是模板类型的树节点 { MessageBox.Show(PdnResources.GetString("Menu.aseselecthecorrectreporttemplatenthel.Text")); return; } Dictionary bitmapDic = new Dictionary(); foreach (Control C in this.panel1.Controls) { if(C is PictureBox) { PictureBox pictureBox = (PictureBox)C; foreach (Control childC in pictureBox.Controls) { if(childC is CheckBox)//图片已选中 { CheckBox checkBox = (CheckBox)childC; if (checkBox.Checked) { double pantographRatio = (double)this.appWorkspace.DocumentWorkspaces[(int)pictureBox.Tag - 1].GetRulerMultiple(MeasurementUnit.Micron);//标尺*放大倍数 pictureBox.Image.Tag = pantographRatio; Bitmap bitmap = new Bitmap(pictureBox.Image); bitmap.Tag = pantographRatio; bitmapDic.Add(Convert.ToInt32(pictureBox.Tag), bitmap/*new Bitmap(pictureBox.Image)*/); break; } } } } } List wordBitmapList = new List(); if (bitmapDic != null && bitmapDic.Count > 0) { var dictSort = from objDic in bitmapDic orderby objDic.Key select objDic;//重新按key排序 foreach(KeyValuePair kvp in dictSort) { wordBitmapList.Add(kvp.Value); } } if (wordBitmapList != null && wordBitmapList.Count > 0) { AnalyzeSettingModel analyzeSettingModel = new AnalyzeSettingModel(); analyzeSettingModel.analyzeClassify = PdnResources.GetString("Menu.Normaloperation.text"); analyzeSettingModel.modulePath = this.treeView1.SelectedNode.Name; analyzeSettingModel.savePath = this.textBox1.Text; int dotPitch = Startup.instance.configModel.DotPitchId;//获取系统激活的点距id double pointPitch = 0; if (dotPitch > 0) { mic_screen_rules screenList = mic_screen_rules_BLL.FindDefault(dotPitch);//从数据库查询点距具体信息 if (screenList != null) { try { pointPitch = double.Parse(screenList.point_pitch);//获取点距 } catch (System.Exception) { } } } //获取系统的毫米标尺 double unitLength = 0; this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Millimeter, out unitLength); if (unitLength == 0) unitLength = 1; OfficeFileHandleHelper.CreateNewWordWithContent(analyzeSettingModel, null, wordBitmapList, pointPitch == 0 ? 1 : pointPitch, unitLength, this.appWorkspace.GetGainMultiple()); this.Close(); } else MessageBox.Show(PdnResources.GetString("Menu.ortenerationfaile.Text")); } /// /// 选择存储路径按钮 /// /// /// private void button1_Click(object sender, EventArgs e) { APIDialogs.CommonOpenFileDialog dialog = new APIDialogs.CommonOpenFileDialog(); dialog.IsFolderPicker = true; dialog.EnsurePathExists = true; dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (dialog.ShowDialog() == APIDialogs.CommonFileDialogResult.Ok) { this.textBox1.Text = dialog.FileName; } } } }