Add files via upload

This commit is contained in:
zsyg
2025-07-02 16:05:28 +08:00
committed by GitHub
parent 7c78a118a9
commit 459c0bc9d7
10 changed files with 728 additions and 728 deletions

View File

@@ -1,34 +1,34 @@
using System; using System;
namespace AppStore namespace AppStore
{ {
/// <summary> /// <summary>
/// 提供基本数学运算功能的工具类 /// 提供基本数学运算功能的工具类
/// </summary> /// </summary>
public static class Calculator public static class Calculator
{ {
/// <summary> /// <summary>
/// 加法运算 /// 加法运算
/// </summary> /// </summary>
public static double Add(double a, double b) => a + b; public static double Add(double a, double b) => a + b;
/// <summary> /// <summary>
/// 减法运算 /// 减法运算
/// </summary> /// </summary>
public static double Subtract(double a, double b) => a - b; public static double Subtract(double a, double b) => a - b;
/// <summary> /// <summary>
/// 乘法运算 /// 乘法运算
/// </summary> /// </summary>
public static double Multiply(double a, double b) => a * b; public static double Multiply(double a, double b) => a * b;
/// <summary> /// <summary>
/// 除法运算 /// 除法运算
/// </summary> /// </summary>
public static double Divide(double a, double b) public static double Divide(double a, double b)
{ {
if (b == 0) throw new DivideByZeroException("除数不能为零"); if (b == 0) throw new DivideByZeroException("除数不能为零");
return a / b; return a / b;
} }
} }
} }

View File

@@ -1,118 +1,118 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using AppStore; using AppStore;
namespace AppStore namespace AppStore
{ {
public class CalculatorForm : Form public class CalculatorForm : Form
{ {
private TextBox display = new TextBox(); private TextBox display = new TextBox();
private double firstNumber = 0; private double firstNumber = 0;
private string operation = ""; private string operation = "";
public CalculatorForm() public CalculatorForm()
{ {
this.Text = "计算器"; this.Text = "计算器";
this.Size = new Size(300, 400); this.Size = new Size(300, 400);
this.StartPosition = FormStartPosition.CenterScreen; this.StartPosition = FormStartPosition.CenterScreen;
CreateControls(); CreateControls();
} }
private void CreateControls() private void CreateControls()
{ {
// 显示框 // 显示框
display = new TextBox(); display = new TextBox();
display.ReadOnly = true; display.ReadOnly = true;
display.TextAlign = HorizontalAlignment.Right; display.TextAlign = HorizontalAlignment.Right;
display.Font = new Font("Microsoft YaHei", 14); display.Font = new Font("Microsoft YaHei", 14);
display.Size = new Size(260, 40); display.Size = new Size(260, 40);
display.Location = new Point(20, 20); display.Location = new Point(20, 20);
this.Controls.Add(display); this.Controls.Add(display);
// 按钮布局 // 按钮布局
string[] buttonLabels = { string[] buttonLabels = {
"7", "8", "9", "/", "7", "8", "9", "/",
"4", "5", "6", "*", "4", "5", "6", "*",
"1", "2", "3", "-", "1", "2", "3", "-",
"0", ".", "=", "+" "0", ".", "=", "+"
}; };
for (int i = 0; i < buttonLabels.Length; i++) for (int i = 0; i < buttonLabels.Length; i++)
{ {
var btn = new Button(); var btn = new Button();
btn.Text = buttonLabels[i]; btn.Text = buttonLabels[i];
btn.Size = new Size(60, 50); btn.Size = new Size(60, 50);
btn.Location = new Point(20 + (i % 4) * 65, 70 + (i / 4) * 55); btn.Location = new Point(20 + (i % 4) * 65, 70 + (i / 4) * 55);
btn.Font = new Font("Microsoft YaHei", 12); btn.Font = new Font("Microsoft YaHei", 12);
btn.Click += ButtonClickHandler; btn.Click += ButtonClickHandler;
this.Controls.Add(btn); this.Controls.Add(btn);
} }
// 清除按钮 // 清除按钮
var clearBtn = new Button(); var clearBtn = new Button();
clearBtn.Text = "C"; clearBtn.Text = "C";
clearBtn.Size = new Size(260, 40); clearBtn.Size = new Size(260, 40);
clearBtn.Location = new Point(20, 300); clearBtn.Location = new Point(20, 300);
clearBtn.Font = new Font("Microsoft YaHei", 12); clearBtn.Font = new Font("Microsoft YaHei", 12);
clearBtn.Click += (s, e) => { clearBtn.Click += (s, e) => {
display.Text = ""; display.Text = "";
firstNumber = 0; firstNumber = 0;
operation = ""; operation = "";
}; };
this.Controls.Add(clearBtn); this.Controls.Add(clearBtn);
} }
private void ButtonClickHandler(object sender, EventArgs e) private void ButtonClickHandler(object sender, EventArgs e)
{ {
var button = sender as Button; var button = sender as Button;
if (button == null) return; if (button == null) return;
switch (button.Text) switch (button.Text)
{ {
case "+": case "+":
case "-": case "-":
case "*": case "*":
case "/": case "/":
if (!string.IsNullOrEmpty(display.Text)) if (!string.IsNullOrEmpty(display.Text))
{ {
firstNumber = double.Parse(display.Text); firstNumber = double.Parse(display.Text);
operation = button.Text; operation = button.Text;
display.Text = ""; display.Text = "";
} }
break; break;
case "=": case "=":
if (!string.IsNullOrEmpty(operation) && !string.IsNullOrEmpty(display.Text)) if (!string.IsNullOrEmpty(operation) && !string.IsNullOrEmpty(display.Text))
{ {
double secondNumber = double.Parse(display.Text); double secondNumber = double.Parse(display.Text);
double result = 0; double result = 0;
try try
{ {
result = operation switch result = operation switch
{ {
"+" => AppStore.Calculator.Add(firstNumber, secondNumber), "+" => AppStore.Calculator.Add(firstNumber, secondNumber),
"-" => AppStore.Calculator.Subtract(firstNumber, secondNumber), "-" => AppStore.Calculator.Subtract(firstNumber, secondNumber),
"*" => AppStore.Calculator.Multiply(firstNumber, secondNumber), "*" => AppStore.Calculator.Multiply(firstNumber, secondNumber),
"/" => AppStore.Calculator.Divide(firstNumber, secondNumber), "/" => AppStore.Calculator.Divide(firstNumber, secondNumber),
_ => 0 _ => 0
}; };
display.Text = result.ToString(); display.Text = result.ToString();
} }
catch (DivideByZeroException) catch (DivideByZeroException)
{ {
MessageBox.Show("除数不能为零", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("除数不能为零", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
operation = ""; operation = "";
} }
break; break;
default: default:
display.Text += button.Text; display.Text += button.Text;
break; break;
} }
} }
} }
} }

View File

@@ -1,38 +1,38 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
namespace AppStore namespace AppStore
{ {
public class CalculatorToolCard : ToolCard public class CalculatorToolCard : ToolCard
{ {
public CalculatorToolCard() public CalculatorToolCard()
{ {
ToolName = "计算器"; ToolName = "计算器";
try try
{ {
string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "Calculator.png"); string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "Calculator.png");
if (File.Exists(iconPath)) if (File.Exists(iconPath))
{ {
ToolIcon = Image.FromFile(iconPath); ToolIcon = Image.FromFile(iconPath);
} }
else else
{ {
ToolIcon = SystemIcons.Application.ToBitmap(); ToolIcon = SystemIcons.Application.ToBitmap();
} }
} }
catch catch
{ {
ToolIcon = SystemIcons.Application.ToBitmap(); ToolIcon = SystemIcons.Application.ToBitmap();
} }
this.ToolCardClicked += OnCalculatorCardClicked; this.ToolCardClicked += OnCalculatorCardClicked;
UpdateDisplay(); UpdateDisplay();
} }
private void OnCalculatorCardClicked(object sender, EventArgs e) private void OnCalculatorCardClicked(object sender, EventArgs e)
{ {
var calculatorForm = new CalculatorForm(); var calculatorForm = new CalculatorForm();
calculatorForm.ShowDialog(); calculatorForm.ShowDialog();
} }
} }
} }

View File

@@ -1,136 +1,136 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using ZXing; using ZXing;
using ZXing.Common; using ZXing.Common;
using ZXing.QrCode; using ZXing.QrCode;
namespace AppStore namespace AppStore
{ {
public class QrCodeGeneratorForm : Form public class QrCodeGeneratorForm : Form
{ {
private TextBox? inputTextBox; private TextBox? inputTextBox;
private PictureBox? qrCodePictureBox; private PictureBox? qrCodePictureBox;
private Button? generateButton; private Button? generateButton;
private Button? saveButton; private Button? saveButton;
public QrCodeGeneratorForm() public QrCodeGeneratorForm()
{ {
InitializeComponent(); InitializeComponent();
this.Text = "二维码生成器"; this.Text = "二维码生成器";
this.Size = new Size(500, 600); this.Size = new Size(500, 600);
this.StartPosition = FormStartPosition.CenterScreen; this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedDialog; this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false; this.MaximizeBox = false;
} }
private void InitializeComponent() private void InitializeComponent()
{ {
// 输入文本框 // 输入文本框
inputTextBox = new TextBox(); inputTextBox = new TextBox();
inputTextBox.Multiline = true; inputTextBox.Multiline = true;
inputTextBox.ScrollBars = ScrollBars.Vertical; inputTextBox.ScrollBars = ScrollBars.Vertical;
inputTextBox.Size = new Size(400, 100); inputTextBox.Size = new Size(400, 100);
inputTextBox.Location = new Point(50, 30); inputTextBox.Location = new Point(50, 30);
inputTextBox.PlaceholderText = "请输入要生成二维码的文本内容..."; inputTextBox.PlaceholderText = "请输入要生成二维码的文本内容...";
this.Controls.Add(inputTextBox); this.Controls.Add(inputTextBox);
// 生成按钮 // 生成按钮
generateButton = new Button(); generateButton = new Button();
generateButton.Text = "生成二维码"; generateButton.Text = "生成二维码";
generateButton.Size = new Size(150, 40); generateButton.Size = new Size(150, 40);
generateButton.Location = new Point(50, 150); generateButton.Location = new Point(50, 150);
generateButton.Click += GenerateButton_Click; generateButton.Click += GenerateButton_Click;
this.Controls.Add(generateButton); this.Controls.Add(generateButton);
// 二维码显示区域 // 二维码显示区域
qrCodePictureBox = new PictureBox(); qrCodePictureBox = new PictureBox();
qrCodePictureBox.Size = new Size(300, 300); qrCodePictureBox.Size = new Size(300, 300);
qrCodePictureBox.Location = new Point(100, 220); qrCodePictureBox.Location = new Point(100, 220);
qrCodePictureBox.SizeMode = PictureBoxSizeMode.StretchImage; qrCodePictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
qrCodePictureBox.BorderStyle = BorderStyle.FixedSingle; qrCodePictureBox.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(qrCodePictureBox); this.Controls.Add(qrCodePictureBox);
// 保存按钮 // 保存按钮
saveButton = new Button(); saveButton = new Button();
saveButton.Text = "保存二维码"; saveButton.Text = "保存二维码";
saveButton.Size = new Size(150, 40); saveButton.Size = new Size(150, 40);
saveButton.Location = new Point(300, 150); saveButton.Location = new Point(300, 150);
saveButton.Click += SaveButton_Click; saveButton.Click += SaveButton_Click;
this.Controls.Add(saveButton); this.Controls.Add(saveButton);
} }
private void GenerateButton_Click(object sender, EventArgs e) private void GenerateButton_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrWhiteSpace(inputTextBox?.Text)) if (string.IsNullOrWhiteSpace(inputTextBox?.Text))
{ {
MessageBox.Show("请输入要生成二维码的文本内容", "提示", MessageBox.Show("请输入要生成二维码的文本内容", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
return; return;
} }
try try
{ {
var writer = new BarcodeWriterPixelData var writer = new BarcodeWriterPixelData
{ {
Format = BarcodeFormat.QR_CODE, Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions Options = new QrCodeEncodingOptions
{ {
Width = 300, Width = 300,
Height = 300, Height = 300,
Margin = 1 Margin = 1
} }
}; };
var pixelData = writer.Write(inputTextBox.Text); var pixelData = writer.Write(inputTextBox.Text);
var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); var bitmap = new Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height), var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try try
{ {
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length); System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
} }
finally finally
{ {
bitmap.UnlockBits(bitmapData); bitmap.UnlockBits(bitmapData);
} }
qrCodePictureBox!.Image = bitmap; qrCodePictureBox!.Image = bitmap;
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"生成二维码失败: {ex.Message}", "错误", MessageBox.Show($"生成二维码失败: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void SaveButton_Click(object sender, EventArgs e) private void SaveButton_Click(object sender, EventArgs e)
{ {
if (qrCodePictureBox?.Image == null) if (qrCodePictureBox?.Image == null)
{ {
MessageBox.Show("请先生成二维码", "提示", MessageBox.Show("请先生成二维码", "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
return; return;
} }
using (var saveDialog = new SaveFileDialog()) using (var saveDialog = new SaveFileDialog())
{ {
saveDialog.Filter = "PNG 图片|*.png|JPEG 图片|*.jpg|BMP 图片|*.bmp"; saveDialog.Filter = "PNG 图片|*.png|JPEG 图片|*.jpg|BMP 图片|*.bmp";
saveDialog.Title = "保存二维码图片"; saveDialog.Title = "保存二维码图片";
if (saveDialog.ShowDialog() == DialogResult.OK) if (saveDialog.ShowDialog() == DialogResult.OK)
{ {
try try
{ {
qrCodePictureBox!.Image.Save(saveDialog.FileName); qrCodePictureBox!.Image.Save(saveDialog.FileName);
MessageBox.Show("二维码保存成功", "成功", MessageBox.Show("二维码保存成功", "成功",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"保存二维码失败: {ex.Message}", "错误", MessageBox.Show($"保存二维码失败: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
} }
} }
} }

View File

@@ -1,103 +1,103 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include <windows.h> #include <windows.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h> #include <stb/stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h> #include <stb/stb_image_write.h>
namespace fs = std::filesystem; namespace fs = std::filesystem;
void ShowHelp() { void ShowHelp() {
std::cout << "图片压缩工具 v1.0\n"; std::cout << "图片压缩工具 v1.0\n";
std::cout << "用法: image_compressor [选项] 输入文件 输出文件\n"; std::cout << "用法: image_compressor [选项] 输入文件 输出文件\n";
std::cout << "选项:\n"; std::cout << "选项:\n";
std::cout << " -t <type> 压缩类型: lossy(有损)/lossless(无损) (默认: lossy)\n"; std::cout << " -t <type> 压缩类型: lossy(有损)/lossless(无损) (默认: lossy)\n";
std::cout << " -q <quality> 压缩质量 (1-1000) (默认: 80)\n"; std::cout << " -q <quality> 压缩质量 (1-1000) (默认: 80)\n";
std::cout << " -e 保留EXIF信息 (默认: 不保留)\n"; std::cout << " -e 保留EXIF信息 (默认: 不保留)\n";
std::cout << " -h 显示帮助信息\n"; std::cout << " -h 显示帮助信息\n";
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
// 默认参数 // 默认参数
std::string compressType = "lossy"; std::string compressType = "lossy";
int quality = 80; int quality = 80;
bool keepExif = false; bool keepExif = false;
std::string inputFile; std::string inputFile;
std::string outputFile; std::string outputFile;
// 解析命令行参数 // 解析命令行参数
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
std::string arg = argv[i]; std::string arg = argv[i];
if (arg == "-h") { if (arg == "-h") {
ShowHelp(); ShowHelp();
return 0; return 0;
} else if (arg == "-t" && i + 1 < argc) { } else if (arg == "-t" && i + 1 < argc) {
compressType = argv[++i]; compressType = argv[++i];
} else if (arg == "-q" && i + 1 < argc) { } else if (arg == "-q" && i + 1 < argc) {
quality = std::stoi(argv[++i]); quality = std::stoi(argv[++i]);
} else if (arg == "-e") { } else if (arg == "-e") {
keepExif = true; keepExif = true;
} else if (inputFile.empty()) { } else if (inputFile.empty()) {
inputFile = arg; inputFile = arg;
} else { } else {
outputFile = arg; outputFile = arg;
} }
} }
// 验证参数 // 验证参数
if (inputFile.empty() || outputFile.empty()) { if (inputFile.empty() || outputFile.empty()) {
std::cerr << "错误: 必须指定输入文件和输出文件\n"; std::cerr << "错误: 必须指定输入文件和输出文件\n";
ShowHelp(); ShowHelp();
return 1; return 1;
} }
if (quality < 1 || quality > 1000) { if (quality < 1 || quality > 1000) {
std::cerr << "错误: 压缩质量必须在1-1000之间\n"; std::cerr << "错误: 压缩质量必须在1-1000之间\n";
return 1; return 1;
} }
try { try {
std::cout << "正在压缩: " << inputFile << " -> " << outputFile << std::endl; std::cout << "正在压缩: " << inputFile << " -> " << outputFile << std::endl;
std::cout << "类型: " << compressType << std::endl; std::cout << "类型: " << compressType << std::endl;
std::cout << "质量: " << quality << std::endl; std::cout << "质量: " << quality << std::endl;
std::cout << "保留EXIF: " << (keepExif ? "" : "") << std::endl; std::cout << "保留EXIF: " << (keepExif ? "" : "") << std::endl;
// 加载图片 // 加载图片
int width, height, channels; int width, height, channels;
unsigned char* image = stbi_load(inputFile.c_str(), &width, &height, &channels, 0); unsigned char* image = stbi_load(inputFile.c_str(), &width, &height, &channels, 0);
if (!image) { if (!image) {
std::cerr << "错误: 无法加载图片 " << inputFile << " - " << stbi_failure_reason() << std::endl; std::cerr << "错误: 无法加载图片 " << inputFile << " - " << stbi_failure_reason() << std::endl;
return 1; return 1;
} }
// 压缩图片 // 压缩图片
int result = 0; int result = 0;
if (compressType == "lossy") { if (compressType == "lossy") {
// 有损压缩 (JPEG) // 有损压缩 (JPEG)
int jpeg_quality = quality / 10; // 将1-1000映射到1-100 int jpeg_quality = quality / 10; // 将1-1000映射到1-100
jpeg_quality = std::max(1, std::min(jpeg_quality, 100)); jpeg_quality = std::max(1, std::min(jpeg_quality, 100));
result = stbi_write_jpg(outputFile.c_str(), width, height, channels, image, jpeg_quality); result = stbi_write_jpg(outputFile.c_str(), width, height, channels, image, jpeg_quality);
} else { } else {
// 无损压缩 (PNG) // 无损压缩 (PNG)
result = stbi_write_png(outputFile.c_str(), width, height, channels, image, width * channels); result = stbi_write_png(outputFile.c_str(), width, height, channels, image, width * channels);
} }
// 释放内存 // 释放内存
stbi_image_free(image); stbi_image_free(image);
if (!result) { if (!result) {
std::cerr << "错误: 无法保存图片 " << outputFile << std::endl; std::cerr << "错误: 无法保存图片 " << outputFile << std::endl;
return 1; return 1;
} }
std::cout << "压缩完成\n"; std::cout << "压缩完成\n";
return 0; return 0;
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl; std::cerr << "发生错误: " << e.what() << std::endl;
return 1; return 1;
} }
} }

View File

@@ -1,96 +1,96 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <chrono> #include <chrono>
#include <vector> #include <vector>
#include <windows.h> #include <windows.h>
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool IsAdmin() { bool IsAdmin() {
BOOL isAdmin = FALSE; BOOL isAdmin = FALSE;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdminGroup; PSID AdminGroup;
if (AllocateAndInitializeSid(&NtAuthority, 2, if (AllocateAndInitializeSid(&NtAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &AdminGroup)) { 0, 0, 0, 0, 0, 0, &AdminGroup)) {
if (!CheckTokenMembership(NULL, AdminGroup, &isAdmin)) { if (!CheckTokenMembership(NULL, AdminGroup, &isAdmin)) {
isAdmin = FALSE; isAdmin = FALSE;
} }
FreeSid(AdminGroup); FreeSid(AdminGroup);
} }
return isAdmin == TRUE; return isAdmin == TRUE;
} }
void CleanDirectory(const fs::path& dir, size_t& deletedCount, size_t& errorCount) { void CleanDirectory(const fs::path& dir, size_t& deletedCount, size_t& errorCount) {
if (!fs::exists(dir)) { if (!fs::exists(dir)) {
std::cout << "目录不存在: " << dir << std::endl; std::cout << "目录不存在: " << dir << std::endl;
return; return;
} }
for (const auto& entry : fs::directory_iterator(dir)) { for (const auto& entry : fs::directory_iterator(dir)) {
try { try {
if (fs::is_regular_file(entry)) { if (fs::is_regular_file(entry)) {
fs::remove(entry); fs::remove(entry);
deletedCount++; deletedCount++;
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "删除文件失败: " << entry.path() << " - " << e.what() << std::endl; std::cerr << "删除文件失败: " << entry.path() << " - " << e.what() << std::endl;
errorCount++; errorCount++;
} }
} }
} }
int main() { int main() {
try { try {
if (!IsAdmin()) { if (!IsAdmin()) {
std::cerr << "请以管理员身份运行此程序以清理所有目录" << std::endl; std::cerr << "请以管理员身份运行此程序以清理所有目录" << std::endl;
} }
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
size_t totalDeleted = 0; size_t totalDeleted = 0;
size_t totalErrors = 0; size_t totalErrors = 0;
// 定义要清理的目录列表 // 定义要清理的目录列表
std::vector<fs::path> directories = { std::vector<fs::path> directories = {
"C:\\Windows\\Temp", "C:\\Windows\\Temp",
fs::path(std::getenv("USERPROFILE")) / "AppData" / "Local" / "Temp", fs::path(std::getenv("USERPROFILE")) / "AppData" / "Local" / "Temp",
"C:\\Windows\\SoftwareDistribution\\Download", "C:\\Windows\\SoftwareDistribution\\Download",
"C:\\Windows\\Logs", "C:\\Windows\\Logs",
"C:\\Windows\\Minidump" "C:\\Windows\\Minidump"
}; };
// 只有管理员才能清理Prefetch目录 // 只有管理员才能清理Prefetch目录
if (IsAdmin()) { if (IsAdmin()) {
directories.push_back("C:\\Windows\\Prefetch"); directories.push_back("C:\\Windows\\Prefetch");
} }
// 清理每个目录 // 清理每个目录
for (const auto& dir : directories) { for (const auto& dir : directories) {
size_t dirDeleted = 0; size_t dirDeleted = 0;
size_t dirErrors = 0; size_t dirErrors = 0;
std::cout << "正在清理: " << dir << std::endl; std::cout << "正在清理: " << dir << std::endl;
CleanDirectory(dir, dirDeleted, dirErrors); CleanDirectory(dir, dirDeleted, dirErrors);
totalDeleted += dirDeleted; totalDeleted += dirDeleted;
totalErrors += dirErrors; totalErrors += dirErrors;
std::cout << "已删除 " << dirDeleted << " 个文件" << std::endl; std::cout << "已删除 " << dirDeleted << " 个文件" << std::endl;
} }
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "\n清理完成:" << std::endl; std::cout << "\n清理完成:" << std::endl;
std::cout << "总共删除文件数: " << totalDeleted << std::endl; std::cout << "总共删除文件数: " << totalDeleted << std::endl;
std::cout << "总共错误数: " << totalErrors << std::endl; std::cout << "总共错误数: " << totalErrors << std::endl;
std::cout << "耗时: " << duration.count() << " 毫秒" << std::endl; std::cout << "耗时: " << duration.count() << " 毫秒" << std::endl;
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl; std::cerr << "发生错误: " << e.what() << std::endl;
return 1; return 1;
} }
return 0; return 0;
} }

View File

@@ -1,21 +1,21 @@
namespace KortAppZ.Tools.Viewer namespace KortAppZ.Tools.Viewer
{ {
public interface IImageViewer public interface IImageViewer
{ {
/// <summary> /// <summary>
/// 加载图片文件 /// 加载图片文件
/// </summary> /// </summary>
/// <param name="filePath">图片文件路径</param> /// <param name="filePath">图片文件路径</param>
void LoadImage(string filePath); void LoadImage(string filePath);
/// <summary> /// <summary>
/// 显示图片 /// 显示图片
/// </summary> /// </summary>
void ShowImage(); void ShowImage();
/// <summary> /// <summary>
/// 关闭图片查看器 /// 关闭图片查看器
/// </summary> /// </summary>
void CloseViewer(); void CloseViewer();
} }
} }

View File

@@ -1,41 +1,41 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
namespace KortAppZ.Tools.Viewer namespace KortAppZ.Tools.Viewer
{ {
public static class ImageFileHandler public static class ImageFileHandler
{ {
private static readonly string[] SupportedExtensions = private static readonly string[] SupportedExtensions =
{ {
".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff", ".tif" ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff", ".tif"
}; };
public static bool IsImageFile(string filePath) public static bool IsImageFile(string filePath)
{ {
if (string.IsNullOrEmpty(filePath)) if (string.IsNullOrEmpty(filePath))
return false; return false;
string extension = Path.GetExtension(filePath).ToLower(); string extension = Path.GetExtension(filePath).ToLower();
return Array.Exists(SupportedExtensions, ext => ext == extension); return Array.Exists(SupportedExtensions, ext => ext == extension);
} }
public static Image LoadImage(string filePath) public static Image LoadImage(string filePath)
{ {
if (!IsImageFile(filePath)) if (!IsImageFile(filePath))
throw new ArgumentException("不支持的图片格式"); throw new ArgumentException("不支持的图片格式");
if (!File.Exists(filePath)) if (!File.Exists(filePath))
throw new FileNotFoundException("图片文件不存在", filePath); throw new FileNotFoundException("图片文件不存在", filePath);
try try
{ {
return Image.FromFile(filePath); return Image.FromFile(filePath);
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new Exception($"无法加载图片: {ex.Message}", ex); throw new Exception($"无法加载图片: {ex.Message}", ex);
} }
} }
} }
} }

View File

@@ -1,93 +1,93 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using KortAppZ.Tools.Viewer; using KortAppZ.Tools.Viewer;
namespace KortAppZ.Tools.Viewer namespace KortAppZ.Tools.Viewer
{ {
public class ImageViewerForm : Form public class ImageViewerForm : Form
{ {
private Button? openButton; private Button? openButton;
public ImageViewerForm() public ImageViewerForm()
{ {
InitializeComponent(); InitializeComponent();
} }
private void InitializeComponent() private void InitializeComponent()
{ {
this.openButton = new Button(); this.openButton = new Button();
// //
// openButton // openButton
// //
this.openButton.Text = "打开图片"; this.openButton.Text = "打开图片";
this.openButton.Size = new Size(120, 40); this.openButton.Size = new Size(120, 40);
this.openButton.Font = new Font("Microsoft YaHei", 10); this.openButton.Font = new Font("Microsoft YaHei", 10);
this.openButton.Anchor = AnchorStyles.None; this.openButton.Anchor = AnchorStyles.None;
this.openButton.Click += new EventHandler(OpenButton_Click); this.openButton.Click += new EventHandler(OpenButton_Click);
// 初始位置 // 初始位置
CenterOpenButton(); CenterOpenButton();
// 窗体大小变化时重新居中按钮 // 窗体大小变化时重新居中按钮
this.Resize += (s, e) => CenterOpenButton(); this.Resize += (s, e) => CenterOpenButton();
// //
// ImageViewerForm // ImageViewerForm
// //
this.Text = "图片查看器"; this.Text = "图片查看器";
this.ClientSize = new Size(400, 300); this.ClientSize = new Size(400, 300);
this.StartPosition = FormStartPosition.CenterScreen; this.StartPosition = FormStartPosition.CenterScreen;
this.MinimumSize = new Size(300, 200); this.MinimumSize = new Size(300, 200);
this.MaximizeBox = false; this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog; this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.Controls.Add(this.openButton); this.Controls.Add(this.openButton);
} }
private void CenterOpenButton() private void CenterOpenButton()
{ {
if (openButton != null) if (openButton != null)
{ {
openButton.Location = new Point( openButton.Location = new Point(
(this.ClientSize.Width - openButton.Width) / 2, (this.ClientSize.Width - openButton.Width) / 2,
(this.ClientSize.Height - openButton.Height) / 2); (this.ClientSize.Height - openButton.Height) / 2);
} }
} }
private void OpenButton_Click(object sender, EventArgs e) private void OpenButton_Click(object sender, EventArgs e)
{ {
OpenFileDialog openFileDialog = new OpenFileDialog(); OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.tif|所有文件|*.*"; openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.tif|所有文件|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
string filePath = openFileDialog.FileName; string filePath = openFileDialog.FileName;
ShowImage(filePath); ShowImage(filePath);
} }
} }
private void ShowImage(string filePath) private void ShowImage(string filePath)
{ {
try try
{ {
if (!ImageFileHandler.IsImageFile(filePath)) if (!ImageFileHandler.IsImageFile(filePath))
{ {
MessageBox.Show("不支持的图片格式", "错误", MessageBox.Show("不支持的图片格式", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
ImageDisplayForm displayForm = new ImageDisplayForm(); ImageDisplayForm displayForm = new ImageDisplayForm();
displayForm.LoadImage(filePath); displayForm.LoadImage(filePath);
displayForm.Show(); displayForm.Show();
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"无法加载图片: {ex.Message}", "错误", MessageBox.Show($"无法加载图片: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
} }

View File

@@ -1,48 +1,48 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using AppStore; using AppStore;
namespace KortAppZ.Tools.Viewer namespace KortAppZ.Tools.Viewer
{ {
public class ImageViewerToolCard : ToolCard public class ImageViewerToolCard : ToolCard
{ {
public ImageViewerToolCard() public ImageViewerToolCard()
{ {
this.ToolName = "图片查看"; this.ToolName = "图片查看";
try try
{ {
string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "ImageCompressor.png"); string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "ImageCompressor.png");
if (File.Exists(iconPath)) if (File.Exists(iconPath))
{ {
this.ToolIcon = Image.FromFile(iconPath); this.ToolIcon = Image.FromFile(iconPath);
} }
else else
{ {
this.ToolIcon = SystemIcons.Application.ToBitmap(); this.ToolIcon = SystemIcons.Application.ToBitmap();
} }
} }
catch catch
{ {
this.ToolIcon = SystemIcons.Application.ToBitmap(); this.ToolIcon = SystemIcons.Application.ToBitmap();
} }
this.ToolCardClicked += (s, e) => { this.ToolCardClicked += (s, e) => {
try try
{ {
ImageViewerForm viewerForm = new ImageViewerForm(); ImageViewerForm viewerForm = new ImageViewerForm();
viewerForm.Show(); viewerForm.Show();
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"打开图片查看器失败: {ex.Message}", "错误", MessageBox.Show($"打开图片查看器失败: {ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
}; };
this.UpdateDisplay(); this.UpdateDisplay();
} }
} }
} }