From 08a11f025a71d819157d251edd0807267a09e09b Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Sat, 5 Jul 2025 19:08:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=E5=92=8C=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AppCard.cs | 106 ++++++++++++++++++++++++++++++------------------ ThemeManager.cs | 7 ++++ logger.cs | 20 +++++++++ 3 files changed, 93 insertions(+), 40 deletions(-) diff --git a/AppCard.cs b/AppCard.cs index 4fc6789..17c5c55 100644 --- a/AppCard.cs +++ b/AppCard.cs @@ -54,9 +54,23 @@ namespace AppStore this.Padding = new Padding(10); // 异步初始化卡片路径和边框 + // 预加载边框路径 Task.Run(() => { InitializeCardPath(); InitializeBorder(); + + // 确保在主线程注册事件 + this.Invoke((MethodInvoker)(() => { + this.Paint += (sender, e) => { + if (BorderCache.IsEmpty) + { + Task.Run(() => { + InitializeBorder(); + this.Invoke((MethodInvoker)(() => this.Invalidate())); + }); + } + }; + })); }); // 应用图标 - 添加null检查 @@ -187,52 +201,58 @@ namespace AppStore // 使用卡片尺寸作为缓存键 string cacheKey = $"{Width}_{Height}_10"; - // 检查缓存中是否已有路径 + // 双重检查锁模式确保线程安全 if (!BorderCache.TryGetValue(cacheKey, out var borderPath)) { - // 创建临时文件存储路径数据 - string tempFile = Path.GetTempFileName(); - try + lock (BorderCache) { - // 配置C++程序启动参数 - ProcessStartInfo startInfo = new ProcessStartInfo + if (!BorderCache.TryGetValue(cacheKey, out borderPath)) { - FileName = Path.Combine(Application.StartupPath, "resource", "border_renderer.exe"), - Arguments = $"{Width} {Height} 10 \"{tempFile}\"", // 传递宽高和圆角半径 - UseShellExecute = false, // 不显示命令行窗口 - CreateNoWindow = true // 静默运行 - }; - - // 启动C++程序计算路径 - using (var process = Process.Start(startInfo)) - { - process.WaitForExit(); - - // 检查计算结果 - if (process.ExitCode == 0 && File.Exists(tempFile)) + // 创建临时文件存储路径数据 + string tempFile = Path.GetTempFileName(); + try { - // 读取C++程序生成的路径点 - var lines = File.ReadAllLines(tempFile); - PointF[] points = lines.Select(line => { - var parts = line.Split(','); // 解析坐标点 - return new PointF(float.Parse(parts[0]), float.Parse(parts[1])); - }).ToArray(); - - // 创建GraphicsPath对象 - borderPath = new System.Drawing.Drawing2D.GraphicsPath(); - borderPath.AddLines(points); // 添加路径点 - - // 缓存路径对象 - BorderCache.TryAdd(cacheKey, borderPath); + // 配置C++程序启动参数 + ProcessStartInfo startInfo = new ProcessStartInfo + { + FileName = Path.Combine(Application.StartupPath, "resource", "border_renderer.exe"), + Arguments = $"{Width} {Height} 10 \"{tempFile}\"", // 传递宽高和圆角半径 + UseShellExecute = false, // 不显示命令行窗口 + CreateNoWindow = true // 静默运行 + }; + + // 启动C++程序计算路径 + using (var process = Process.Start(startInfo)) + { + process.WaitForExit(); + + // 检查计算结果 + if (process.ExitCode == 0 && File.Exists(tempFile)) + { + // 读取C++程序生成的路径点 + var lines = File.ReadAllLines(tempFile); + PointF[] points = lines.Select(line => { + var parts = line.Split(','); // 解析坐标点 + return new PointF(float.Parse(parts[0]), float.Parse(parts[1])); + }).ToArray(); + + // 创建GraphicsPath对象 + borderPath = new System.Drawing.Drawing2D.GraphicsPath(); + borderPath.AddLines(points); // 添加路径点 + + // 缓存路径对象 + BorderCache.TryAdd(cacheKey, borderPath); + } + } + } + finally + { + // 确保临时文件被删除 + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } } - } - } - finally - { - // 确保临时文件被删除 - if (File.Exists(tempFile)) - { - File.Delete(tempFile); } } } @@ -243,6 +263,12 @@ namespace AppStore { base.OnPaint(e); + // 确保边框已初始化 + if (BorderCache.IsEmpty) + { + InitializeBorder(); + } + // 绘制背景 using (var brush = new SolidBrush(this.BackColor)) { e.Graphics.FillRectangle(brush, this.ClientRectangle); diff --git a/ThemeManager.cs b/ThemeManager.cs index d1b6d2b..cc322fd 100644 --- a/ThemeManager.cs +++ b/ThemeManager.cs @@ -76,6 +76,10 @@ namespace AppStore private static readonly Color DarkText = Color.FromArgb(240, 240, 240); private static readonly Color DarkButtonHover = Color.FromArgb(60, 60, 60); private static readonly Color DarkButtonActive = Color.FromArgb(70, 70, 70); + private static readonly Color DarkBorder = Color.FromArgb(80, 80, 80); + + // 浅色主题边框颜色 + private static readonly Color LightBorder = Color.FromArgb(180, 180, 180); public static event Action ThemeChanged = delegate {}; @@ -108,6 +112,9 @@ namespace AppStore public static Color ButtonActiveColor => _currentTheme == ThemeMode.Light ? LightButtonActive : DarkButtonActive; + public static Color BorderColor => + _currentTheme == ThemeMode.Light ? LightBorder : DarkBorder; + public static void ApplyTheme(Control control) { ApplyThemeToControl(control); diff --git a/logger.cs b/logger.cs index 9ac2492..177dce2 100644 --- a/logger.cs +++ b/logger.cs @@ -75,5 +75,25 @@ namespace AppStore } Log(warningMessage); } + + public static void LogDebug(string message, Exception? ex = null) + { + string debugMessage = $"DEBUG: {message}"; + if (ex != null) + { + debugMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}"; + } + Log(debugMessage); + } + + public static void LogTip(string message, Exception? ex = null) + { + string tipMessage = $"TIP: {message}"; + if (ex != null) + { + tipMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}"; + } + Log(tipMessage); + } } }