#nullable enable using System; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Diagnostics; using AppStore; namespace AppStore { /// /// 主窗体类,负责应用程序的主界面显示和交互 /// public class MainForm : Form { private static readonly string CacheDir = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "zsyg", "kortapp-z", ".cache"); private class AppPositionCache { public string AppName { get; set; } = string.Empty; public int X { get; set; } public int Y { get; set; } public DateTime LastUpdated { get; set; } = DateTime.Now; } private static string GetPositionCacheFilePath(string appName) { using var md5 = MD5.Create(); var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(appName)); var fileName = BitConverter.ToString(hash).Replace("-", "").ToLower() + ".json"; return Path.Combine(CacheDir, fileName); } private static void EnsureCacheDirectory() { try { if (!Directory.Exists(CacheDir)) { Directory.CreateDirectory(CacheDir); Logger.Log($"已创建缓存目录: {CacheDir}"); } } catch (Exception ex) { Logger.LogError($"创建缓存目录失败: {CacheDir}", ex); throw; } } private static bool TryReadPositionCache(string appName, out AppPositionCache? cacheData) { cacheData = null; var cacheFile = GetPositionCacheFilePath(appName); if (!File.Exists(cacheFile)) return false; try { var json = File.ReadAllText(cacheFile); cacheData = JsonSerializer.Deserialize(json); return cacheData != null; } catch { return false; } } private static void WritePositionCache(string appName, Point position) { try { EnsureCacheDirectory(); var cacheFile = GetPositionCacheFilePath(appName); var cacheData = new AppPositionCache { AppName = appName, X = position.X, Y = position.Y, LastUpdated = DateTime.Now }; var json = JsonSerializer.Serialize(cacheData); File.WriteAllText(cacheFile, json); Logger.Log($"已保存位置缓存: {appName} ({position.X}, {position.Y})"); } catch (Exception ex) { Logger.LogError($"保存位置缓存失败: {appName}", ex); throw; } } private static bool IsPositionCacheValid(AppPositionCache cacheData) { // 缓存有效期设为7天 return (DateTime.Now - cacheData.LastUpdated).TotalDays <= 7; } private static void SaveAllCardPositions(FlowLayoutPanel flowPanel) { foreach (Control control in flowPanel.Controls) { if (control is AppCard card) { WritePositionCache(card.AppName, control.Location); } } } // 软件下载按钮 private Button btnApps = null!; // 下载进度按钮 private Button btnDownloads = null!; // 设置按钮 private Button btnSettings = null!; // 关于按钮 private Button btnAbout = null!; // 内容显示面板 private Panel contentPanel = null!; /// /// 初始化窗体组件 /// private void InitializeComponent() { // 设置窗体基本属性 // 窗体基本设置 this.Text = "kortapp-z"; this.Size = new Size(1430, 1050); // 增加窗体高度 this.MinimumSize = new Size(600, 600); // 设置最小尺寸 this.StartPosition = FormStartPosition.CenterScreen; this.Icon = new Icon("img/ico/icon.ico"); // 设置窗体图标 // 现代化顶部导航栏 Panel buttonPanel = new Panel(); buttonPanel.Dock = DockStyle.Top; buttonPanel.Height = 70; buttonPanel.BackColor = Color.FromArgb(240, 240, 240); buttonPanel.Padding = new Padding(10, 15, 10, 0); buttonPanel.AutoScroll = true; buttonPanel.AutoSize = true; buttonPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 导航按钮样式 Action