将 cancelButton.CancelButton 改为 form.CancelButton CancelButton是Form的属性,用于设置窗体的取消按钮,不是Button的属性 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
470 lines
18 KiB
C#
470 lines
18 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Drawing;
|
||
using System.Windows.Forms;
|
||
|
||
namespace SlideCombine
|
||
{
|
||
public partial class Form1 : Form
|
||
{
|
||
public Form1()
|
||
{
|
||
InitializeComponent();
|
||
|
||
// 设置窗体图标 - 使用嵌入资源
|
||
try
|
||
{
|
||
var stream = GetType().Assembly.GetManifestResourceStream("SlideCombine.app.ico");
|
||
if (stream != null)
|
||
{
|
||
this.Icon = new Icon(stream);
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
// 如果图标加载失败,使用默认图标
|
||
// this.Icon = SystemIcons.Application;
|
||
}
|
||
}
|
||
|
||
private void btnBrowseSource_Click(object sender, EventArgs e)
|
||
{
|
||
string selectedPath = ShowModernFolderBrowser("请选择包含PDF文件夹的路径(含FreePic2Pdf_bkmk.txt文件)", txtSourcePath.Text);
|
||
if (!string.IsNullOrEmpty(selectedPath))
|
||
{
|
||
txtSourcePath.Text = selectedPath;
|
||
LogInfo($"已选择PDF路径: {selectedPath}");
|
||
}
|
||
}
|
||
|
||
private void btnBrowseText_Click(object sender, EventArgs e)
|
||
{
|
||
string selectedPath = ShowModernFolderBrowser("请选择包含元数据TXT文件的路径", txtTextPath.Text);
|
||
if (!string.IsNullOrEmpty(selectedPath))
|
||
{
|
||
txtTextPath.Text = selectedPath;
|
||
LogInfo($"已选择TXT源路径: {selectedPath}");
|
||
}
|
||
}
|
||
|
||
private void btnBrowseOutput_Click(object sender, EventArgs e)
|
||
{
|
||
string selectedPath = ShowModernFolderBrowser("请选择合并后TXT文件的输出路径", txtOutputPath.Text);
|
||
if (!string.IsNullOrEmpty(selectedPath))
|
||
{
|
||
txtOutputPath.Text = selectedPath;
|
||
LogInfo($"已选择最终输出路径: {selectedPath}");
|
||
}
|
||
}
|
||
|
||
private void btnPasteSource_Click(object sender, EventArgs e)
|
||
{
|
||
if (Clipboard.ContainsText())
|
||
{
|
||
var pastedPath = Clipboard.GetText().Trim();
|
||
if (!string.IsNullOrEmpty(pastedPath))
|
||
{
|
||
txtSourcePath.Text = pastedPath;
|
||
LogInfo($"已粘贴PDF路径: {pastedPath}");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("剪贴板中没有文本内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
|
||
private void btnPasteText_Click(object sender, EventArgs e)
|
||
{
|
||
if (Clipboard.ContainsText())
|
||
{
|
||
var pastedPath = Clipboard.GetText().Trim();
|
||
if (!string.IsNullOrEmpty(pastedPath))
|
||
{
|
||
txtTextPath.Text = pastedPath;
|
||
LogInfo($"已粘贴TXT源路径: {pastedPath}");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("剪贴板中没有文本内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
|
||
private void btnPasteOutput_Click(object sender, EventArgs e)
|
||
{
|
||
if (Clipboard.ContainsText())
|
||
{
|
||
var pastedPath = Clipboard.GetText().Trim();
|
||
if (!string.IsNullOrEmpty(pastedPath))
|
||
{
|
||
txtOutputPath.Text = pastedPath;
|
||
LogInfo($"已粘贴输出路径: {pastedPath}");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("剪贴板中没有文本内容", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
}
|
||
|
||
private void btnClear_Click(object sender, EventArgs e)
|
||
{
|
||
txtSourcePath.Clear();
|
||
txtTextPath.Clear();
|
||
txtOutputPath.Clear();
|
||
txtLog.Clear();
|
||
progressBar.Value = 0;
|
||
LogInfo("界面已清空");
|
||
}
|
||
|
||
private void btnExit_Click(object sender, EventArgs e)
|
||
{
|
||
LogInfo("程序即将退出");
|
||
Application.Exit();
|
||
}
|
||
|
||
private void btnMerge_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
// 验证输入
|
||
if (string.IsNullOrWhiteSpace(txtSourcePath.Text) ||
|
||
string.IsNullOrWhiteSpace(txtTextPath.Text) ||
|
||
string.IsNullOrWhiteSpace(txtOutputPath.Text))
|
||
{
|
||
MessageBox.Show("请选择所有三个路径:PDF路径、TXT源路径和输出路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (!Directory.Exists(txtSourcePath.Text))
|
||
{
|
||
MessageBox.Show("指定的PDF文件夹路径不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
if (!Directory.Exists(txtTextPath.Text))
|
||
{
|
||
MessageBox.Show("指定的TXT源文件路径不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
|
||
// 禁用按钮,防止重复点击
|
||
btnMerge.Enabled = false;
|
||
btnClear.Enabled = false;
|
||
|
||
// 重置进度条和日志
|
||
progressBar.Value = 0;
|
||
txtLog.Clear();
|
||
Log("开始处理PDF书签文件...");
|
||
|
||
// 处理文件
|
||
var results = FileMerger.ProcessAllFolders(txtSourcePath.Text, txtTextPath.Text, txtOutputPath.Text);
|
||
|
||
// 显示进度
|
||
progressBar.Value = 50;
|
||
Log($"找到 {results.Count} 个文件组需要处理");
|
||
|
||
// 保存结果
|
||
FileMerger.SaveResults(results, txtOutputPath.Text);
|
||
|
||
progressBar.Value = 100;
|
||
|
||
// 统计成功和失败的数量
|
||
int successCount = 0;
|
||
int failCount = 0;
|
||
var sb = new StringBuilder();
|
||
|
||
foreach (var result in results)
|
||
{
|
||
if (result.Success)
|
||
{
|
||
successCount++;
|
||
Log($"✓ 成功处理: {result.BaseFileName} (合并了 {result.SourceFiles.Count} 个文件)");
|
||
}
|
||
else
|
||
{
|
||
failCount++;
|
||
Log($"✗ 处理失败: {result.ErrorMessage}");
|
||
}
|
||
}
|
||
|
||
Log($"处理完成! 成功: {successCount}, 失败: {failCount}");
|
||
|
||
if (successCount > 0)
|
||
{
|
||
MessageBox.Show($"书签合并完成!\n成功处理 {successCount} 个文件\n输出路径: {txtOutputPath.Text}",
|
||
"处理完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("没有成功处理任何文件,请检查输入路径和文件格式。",
|
||
"处理失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log($"错误: {ex.Message}");
|
||
MessageBox.Show($"处理过程中发生错误:\n{ex.Message}",
|
||
"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
finally
|
||
{
|
||
// 重新启用按钮
|
||
btnMerge.Enabled = true;
|
||
btnClear.Enabled = true;
|
||
}
|
||
}
|
||
|
||
private void Log(string msg)
|
||
{
|
||
string timestamp = DateTime.Now.ToString("HH:mm:ss");
|
||
txtLog.AppendText($"[{timestamp}] {msg}\r\n");
|
||
txtLog.ScrollToCaret();
|
||
Application.DoEvents();
|
||
}
|
||
|
||
private void LogSuccess(string msg)
|
||
{
|
||
string timestamp = DateTime.Now.ToString("HH:mm:ss");
|
||
txtLog.SelectionStart = txtLog.TextLength;
|
||
txtLog.SelectionColor = Color.Green;
|
||
txtLog.AppendText($"[{timestamp}] ✅ {msg}\r\n");
|
||
txtLog.SelectionColor = txtLog.ForeColor;
|
||
txtLog.ScrollToCaret();
|
||
Application.DoEvents();
|
||
}
|
||
|
||
private void LogError(string msg)
|
||
{
|
||
string timestamp = DateTime.Now.ToString("HH:mm:ss");
|
||
txtLog.SelectionStart = txtLog.TextLength;
|
||
txtLog.SelectionColor = Color.Red;
|
||
txtLog.AppendText($"[{timestamp}] ❌ {msg}\r\n");
|
||
txtLog.SelectionColor = txtLog.ForeColor;
|
||
txtLog.ScrollToCaret();
|
||
Application.DoEvents();
|
||
}
|
||
|
||
private void LogInfo(string msg)
|
||
{
|
||
string timestamp = DateTime.Now.ToString("HH:mm:ss");
|
||
txtLog.SelectionStart = txtLog.TextLength;
|
||
txtLog.SelectionColor = Color.Blue;
|
||
txtLog.AppendText($"[{timestamp}] ℹ️ {msg}\r\n");
|
||
txtLog.SelectionColor = txtLog.ForeColor;
|
||
txtLog.ScrollToCaret();
|
||
Application.DoEvents();
|
||
}
|
||
|
||
private string ShowModernFolderBrowser(string title, string initialPath)
|
||
{
|
||
using (var form = new Form())
|
||
{
|
||
form.Text = title;
|
||
form.Size = new Size(600, 450);
|
||
form.StartPosition = FormStartPosition.CenterParent;
|
||
form.FormBorderStyle = FormBorderStyle.FixedDialog;
|
||
form.MaximizeBox = false;
|
||
form.MinimizeBox = false;
|
||
form.BackColor = Color.White;
|
||
|
||
// 标题标签
|
||
var titleLabel = new Label();
|
||
titleLabel.Text = title;
|
||
titleLabel.Location = new Point(20, 20);
|
||
titleLabel.Size = new Size(540, 30);
|
||
titleLabel.Font = new Font("Microsoft YaHei", 11F, FontStyle.Bold);
|
||
titleLabel.ForeColor = Color.FromArgb(51, 51, 51);
|
||
form.Controls.Add(titleLabel);
|
||
|
||
// 路径输入框
|
||
var pathTextBox = new TextBox();
|
||
pathTextBox.Location = new Point(20, 60);
|
||
pathTextBox.Size = new Size(460, 30);
|
||
pathTextBox.Font = new Font("Microsoft YaHei", 10F);
|
||
pathTextBox.Text = initialPath ?? string.Empty;
|
||
pathTextBox.BorderStyle = BorderStyle.FixedSingle;
|
||
form.Controls.Add(pathTextBox);
|
||
|
||
// 浏览按钮
|
||
var browseButton = new Button();
|
||
browseButton.Text = "浏览";
|
||
browseButton.Location = new Point(490, 60);
|
||
browseButton.Size = new Size(70, 30);
|
||
browseButton.Font = new Font("Microsoft YaHei", 9F);
|
||
browseButton.BackColor = Color.FromArgb(66, 139, 202);
|
||
browseButton.ForeColor = Color.White;
|
||
browseButton.FlatStyle = FlatStyle.Flat;
|
||
browseButton.FlatAppearance.BorderSize = 0;
|
||
form.Controls.Add(browseButton);
|
||
|
||
// 文件列表
|
||
var fileList = new ListView();
|
||
fileList.Location = new Point(20, 100);
|
||
fileList.Size = new Size(540, 250);
|
||
fileList.View = View.Details;
|
||
fileList.FullRowSelect = true;
|
||
fileList.GridLines = true;
|
||
fileList.Font = new Font("Microsoft YaHei", 9F);
|
||
fileList.BorderStyle = BorderStyle.FixedSingle;
|
||
fileList.Columns.Add("名称", 300);
|
||
fileList.Columns.Add("修改日期", 140);
|
||
fileList.Columns.Add("类型", 80);
|
||
form.Controls.Add(fileList);
|
||
|
||
// 状态标签
|
||
var statusLabel = new Label();
|
||
statusLabel.Text = "当前路径: ";
|
||
statusLabel.Location = new Point(20, 360);
|
||
statusLabel.Size = new Size(540, 20);
|
||
statusLabel.Font = new Font("Microsoft YaHei", 9F);
|
||
statusLabel.ForeColor = Color.FromArgb(102, 102, 102);
|
||
form.Controls.Add(statusLabel);
|
||
|
||
// 确定按钮
|
||
var okButton = new Button();
|
||
okButton.Text = "确定";
|
||
okButton.Location = new Point(380, 380);
|
||
okButton.Size = new Size(80, 35);
|
||
okButton.Font = new Font("Microsoft YaHei", 10F, FontStyle.Bold);
|
||
okButton.BackColor = Color.FromArgb(92, 184, 92);
|
||
okButton.ForeColor = Color.White;
|
||
okButton.FlatStyle = FlatStyle.Flat;
|
||
okButton.FlatAppearance.BorderSize = 0;
|
||
form.AcceptButton = okButton;
|
||
form.Controls.Add(okButton);
|
||
|
||
// 取消按钮
|
||
var cancelButton = new Button();
|
||
cancelButton.Text = "取消";
|
||
cancelButton.Location = new Point(480, 380);
|
||
cancelButton.Size = new Size(80, 35);
|
||
cancelButton.Font = new Font("Microsoft YaHei", 10F);
|
||
cancelButton.BackColor = Color.FromArgb(217, 83, 79);
|
||
cancelButton.ForeColor = Color.White;
|
||
cancelButton.FlatStyle = FlatStyle.Flat;
|
||
cancelButton.FlatAppearance.BorderSize = 0;
|
||
form.CancelButton = cancelButton;
|
||
form.Controls.Add(cancelButton);
|
||
|
||
// 当前路径
|
||
string currentPath = string.IsNullOrEmpty(initialPath) ?
|
||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) : initialPath;
|
||
|
||
// 加载文件夹内容的方法
|
||
void LoadFolderContent(string path)
|
||
{
|
||
try
|
||
{
|
||
fileList.Items.Clear();
|
||
statusLabel.Text = $"当前路径: {path}";
|
||
|
||
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
|
||
{
|
||
// 添加返回上级目录
|
||
if (Path.GetDirectoryName(path) != null)
|
||
{
|
||
var parentItem = new ListViewItem(new[] { "..", "上级目录", "文件夹" });
|
||
parentItem.Tag = Path.GetDirectoryName(path);
|
||
parentItem.ForeColor = Color.FromArgb(66, 139, 202);
|
||
fileList.Items.Add(parentItem);
|
||
}
|
||
|
||
// 添加子文件夹
|
||
foreach (var dir in Directory.GetDirectories(path))
|
||
{
|
||
var dirInfo = new DirectoryInfo(dir);
|
||
var item = new ListViewItem(new[]
|
||
{
|
||
dirInfo.Name,
|
||
dirInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm"),
|
||
"文件夹"
|
||
});
|
||
item.Tag = dirInfo.FullName;
|
||
item.ForeColor = Color.FromArgb(51, 51, 51);
|
||
fileList.Items.Add(item);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"无法访问该路径: {ex.Message}", "错误",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
|
||
// 浏览按钮点击事件
|
||
browseButton.Click += (s, e) =>
|
||
{
|
||
using (var dialog = new FolderBrowserDialog())
|
||
{
|
||
dialog.Description = title;
|
||
if (!string.IsNullOrEmpty(currentPath))
|
||
{
|
||
dialog.SelectedPath = currentPath;
|
||
}
|
||
|
||
if (dialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
currentPath = dialog.SelectedPath;
|
||
pathTextBox.Text = currentPath;
|
||
LoadFolderContent(currentPath);
|
||
}
|
||
}
|
||
};
|
||
|
||
// 文件列表双击事件
|
||
fileList.DoubleClick += (s, e) =>
|
||
{
|
||
if (fileList.SelectedItems.Count > 0)
|
||
{
|
||
var selectedItem = fileList.SelectedItems[0];
|
||
if (selectedItem.Tag is string selectedPath && Directory.Exists(selectedPath))
|
||
{
|
||
currentPath = selectedPath;
|
||
pathTextBox.Text = currentPath;
|
||
LoadFolderContent(currentPath);
|
||
}
|
||
}
|
||
};
|
||
|
||
// 确定按钮点击事件
|
||
okButton.Click += (s, e) =>
|
||
{
|
||
if (Directory.Exists(pathTextBox.Text))
|
||
{
|
||
form.DialogResult = DialogResult.OK;
|
||
form.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("请输入有效的文件夹路径", "提示",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
}
|
||
};
|
||
|
||
// 取消按钮点击事件
|
||
cancelButton.Click += (s, e) =>
|
||
{
|
||
form.DialogResult = DialogResult.Cancel;
|
||
form.Close();
|
||
};
|
||
|
||
// 初始加载
|
||
LoadFolderContent(currentPath);
|
||
|
||
// 显示对话框
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
return pathTextBox.Text;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
}
|