From 5e52a24995dbfd0764a5a3bff58a9b0fbe7b9f7a Mon Sep 17 00:00:00 2001 From: btwise Date: Sun, 6 Oct 2024 17:47:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E7=9B=AE=E5=BD=95=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=BA=E5=BC=82=E6=AD=A5=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PDFQFZ/Form1.Designer.cs | 12 +++++ PDFQFZ/Form1.cs | 105 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 111 insertions(+), 6 deletions(-) diff --git a/PDFQFZ/Form1.Designer.cs b/PDFQFZ/Form1.Designer.cs index 1797a4a..d97d9e7 100644 --- a/PDFQFZ/Form1.Designer.cs +++ b/PDFQFZ/Form1.Designer.cs @@ -79,6 +79,7 @@ private void InitializeComponent() this.comboBoxPages = new System.Windows.Forms.ComboBox(); this.textDpi = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); + this.progressBar1 = new System.Windows.Forms.ProgressBar(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); @@ -626,12 +627,21 @@ private void InitializeComponent() this.label5.TabIndex = 54; this.label5.Text = "PDF DPI"; // + // progressBar1 + // + this.progressBar1.Location = new System.Drawing.Point(97, 101); + this.progressBar1.Name = "progressBar1"; + this.progressBar1.Size = new System.Drawing.Size(357, 3); + this.progressBar1.TabIndex = 56; + this.progressBar1.Visible = false; + // // Form1 // this.AllowDrop = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(467, 561); + this.Controls.Add(this.progressBar1); this.Controls.Add(this.textDpi); this.Controls.Add(this.label5); this.Controls.Add(this.comboBoxPages); @@ -683,6 +693,7 @@ private void InitializeComponent() this.Controls.Add(this.bt_gz); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.KeyPreview = true; this.MaximizeBox = false; this.Name = "Form1"; this.Text = "PDF加盖骑缝章(V1.27)"; @@ -746,6 +757,7 @@ private void InitializeComponent() private System.Windows.Forms.ComboBox comboBoxPages; private System.Windows.Forms.TextBox textDpi; private System.Windows.Forms.Label label5; + private System.Windows.Forms.ProgressBar progressBar1; } } diff --git a/PDFQFZ/Form1.cs b/PDFQFZ/Form1.cs index e05bc2f..51e897b 100644 --- a/PDFQFZ/Form1.cs +++ b/PDFQFZ/Form1.cs @@ -13,6 +13,7 @@ using iTextSharp.text.exceptions; using PDFQFZ.Library; using System.Threading.Tasks; +using System.Threading; namespace PDFQFZ { @@ -33,10 +34,12 @@ public partial class Form1 : Form float xzbl = 1f;//旋转图片导致长宽变化的比例 private string strIniFilePath = $@"{Application.StartupPath}\config.ini";//获取INI文件路径 private bool isSelectionCommitted = false; // 文档预览下拉列表框事件标记位 + private CancellationTokenSource cancellationTokenSource;//处理文件进度取消标记 public Form1(string[] args) { InitializeComponent(); + this.KeyDown += Esc_Key_Down;//接受键盘ESC响应 // 在这里处理命令行参数 if (args.Length > 0) { @@ -1283,8 +1286,11 @@ public void PDFToiPDF(string pdfPath) //建一个全局的目录变量 string pathDir = ""; //选择源文件 - private void SelectPath_Click(object sender, EventArgs e) + private async void SelectPath_Click(object sender, EventArgs e) { + cancellationTokenSource = new CancellationTokenSource(); + var token = cancellationTokenSource.Token; // 获取取消标记 + if (comboType.SelectedIndex == 0) //目录模式 { if (Environment.OSVersion.Version.Major >= 6) //操作系统win7及以上才能使用此效果 @@ -1304,13 +1310,35 @@ private void SelectPath_Click(object sender, EventArgs e) dt.Rows.Add(new object[] { "", "" }); DirectoryInfo dir = new DirectoryInfo(pathText.Text); var fileInfos = dir.GetFiles("*.pdf", SearchOption.AllDirectories); - foreach (var fileInfo in fileInfos) + + // 初始化状态栏和进度条 + log.Text = "准备批量处理...\r\n"; + progressBar1.Visible = true; + bt_gz.Enabled = false;//批量加载时防止用户点击按钮 + + //开始异步加载文件 + try { - if (fileInfo.Extension == ".pdf") - { - dt.Rows.Add(new object[] { fileInfo.Name, fileInfo.FullName }); - } + await Task.Run(() => Load_All_PdfFiles_Async(fileInfos, token), token); } + catch (OperationCanceledException) + { + log.Text = "操作已取消!"; + } + finally + { + // 取消后的清理工作 + cancellationTokenSource.Dispose(); + } + bt_gz.Enabled = true;//恢复盖章按钮 + pathText.Text = fsd.FileName; + //foreach (var fileInfo in fileInfos) + //{ + // if (fileInfo.Extension == ".pdf") + // { + // dt.Rows.Add(new object[] { fileInfo.Name, fileInfo.FullName }); + // } + //} } } else @@ -1372,6 +1400,71 @@ private void SelectPath_Click(object sender, EventArgs e) } } } + /// + /// 加载文件夹文件使用异步方式并辅以进度条显示状态,并可以随时按ESC取消 + /// + private async Task Load_All_PdfFiles_Async(FileInfo[] fileInfos, CancellationToken token) + { + int totalFiles = fileInfos.Length; + + // 在 UI 线程上更新进度条的最大值和初始值 + this.Invoke((Action)(() => + { + progressBar1.Maximum = totalFiles; + progressBar1.Value = 0; + log.Text += "开始批量处理...\r\n"; + })); + + + // 遍历文件 + await Task.Run(() => + { + for (int i = 0; i < totalFiles; i++) + { + token.ThrowIfCancellationRequested(); // 检查是否有取消请求,如果有结束遍历动作 + + var fileInfo = fileInfos[i]; + + // 更新UI(此处要使用 Invoke,因为跨线程更新UI) + Invoke(new Action(() => + { + dt.Rows.Add(new object[] { fileInfo.Name, fileInfo.FullName }); + + // 更新进度条和状态栏 + pathText.Text = $"正在加载文件 {i + 1}/{totalFiles}\r\n"; + progressBar1.Value = i + 1; + })); + + // 模拟加载延时(可以去掉或根据需要调整) + Task.Delay(100).Wait(); + } + }); + + // 所有文件加载完成后更新状态 + Invoke(new Action(() => + { + log.Text = "加载完成!"; + progressBar1.Value = totalFiles; + progressBar1.Visible = false; + })); + } + //按下ESC事件 + private void Esc_Key_Down(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Escape) + { + // 检查是否有一个操作在进行中 + if (cancellationTokenSource != null) + { + cancellationTokenSource.Cancel(); // 请求取消当前操作 + MessageBox.Show("操作已停止"); + progressBar1.Visible = false; + bt_gz.Enabled = true; + pathText.Text = ""; + } + } + } + //选择保存目录 private void OutPath_Click(object sender, EventArgs e) {