From cedfe32b1521a92c7ad60bd8641344dab96088f3 Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Fri, 1 Aug 2025 12:06:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=85=E7=BD=AE=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainForm.cs | 3913 ++++++++++++++++++++++++++------------------------- 1 file changed, 1975 insertions(+), 1938 deletions(-) diff --git a/MainForm.cs b/MainForm.cs index 978d65c..4a59787 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -1,1938 +1,1975 @@ -#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; -using Sunny.UI; -using System.Runtime.InteropServices; - -namespace AppStore -{ - public class MainForm : UIForm - { - [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] - private static extern IntPtr CreateRoundRectRgn( - int nLeftRect, - int nTopRect, - int nRightRect, - int nBottomRect, - int nWidthEllipse, - int nHeightEllipse - ); - - [DllImport("dwmapi.dll")] - private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); - - [DllImport("dwmapi.dll")] - private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize); - - [DllImport("dwmapi.dll")] - private static extern int DwmIsCompositionEnabled(ref int pfEnabled); - - private struct MARGINS - { - public int leftWidth; - public int rightWidth; - public int topHeight; - public int bottomHeight; - } - - private const int DWMWA_WINDOW_CORNER_PREFERENCE = 33; - private const int DWMWCP_ROUND = 2; - - 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 NotifyIcon trayIcon = null!; - // 托盘右键菜单 - private ContextMenuStrip trayMenu = 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"); - this.Style = UIStyle.Custom; - this.FormBorderStyle = FormBorderStyle.None; - - // 应用现代化圆角 - this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, - ThemeManager.FormRadius, ThemeManager.FormRadius)); - - // 启用窗口阴影 - if (Environment.OSVersion.Version.Major >= 6) - { - int val = DWMWCP_ROUND; - DwmSetWindowAttribute(this.Handle, DWMWA_WINDOW_CORNER_PREFERENCE, ref val, sizeof(int)); - - MARGINS margins = new MARGINS() - { - leftWidth = 1, - rightWidth = 1, - topHeight = 1, - bottomHeight = 1 - }; - DwmExtendFrameIntoClientArea(this.Handle, ref margins); - } - - // 初始化系统托盘 - trayMenu = new ContextMenuStrip(); - trayMenu.Items.Add("打开", null, (s, e) => { - this.Show(); - this.WindowState = FormWindowState.Normal; - }); - trayMenu.Items.Add("退出", null, (s, e) => Application.Exit()); - - trayIcon = new NotifyIcon(); - trayIcon.Text = "kortapp-z"; - trayIcon.Icon = new Icon("img/ico/icon.ico"); - trayIcon.ContextMenuStrip = trayMenu; - trayIcon.Visible = true; - trayIcon.DoubleClick += (s, e) => { - this.Show(); - this.WindowState = FormWindowState.Normal; - }; - - // 窗体最小化到托盘处理 - this.Resize += (s, e) => { - if (this.WindowState == FormWindowState.Minimized) - { - this.Hide(); - } - }; - - // 窗体关闭按钮处理 - 隐藏到托盘而不是退出 - this.FormClosing += (s, e) => { - if (e.CloseReason == CloseReason.UserClosing) - { - e.Cancel = true; - this.Hide(); - } - }; - - // 注册主题变更事件 - ThemeManager.ThemeChanged += (theme) => - { - this.Invoke((MethodInvoker)delegate { - AnimateThemeChange(); - }); - }; - - // 现代化顶部导航栏 - Panel buttonPanel = new Panel(); - buttonPanel.Dock = DockStyle.Top; - buttonPanel.Height = 80; - buttonPanel.BackColor = ThemeManager.ControlBackgroundColor; - buttonPanel.Padding = new Padding(15, 20, 15, 5); - buttonPanel.AutoScroll = true; - buttonPanel.AutoSize = true; - buttonPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; - - // 添加支持作者按钮 - UIButton btnSupport = new UIButton(); - btnSupport.Text = "支持作者"; - btnSupport.Font = new Font("微软雅黑", 10F, FontStyle.Bold); - btnSupport.Size = new Size(120, 45); - btnSupport.Location = new Point(buttonPanel.Width - 140, 15); - btnSupport.Anchor = AnchorStyles.Top | AnchorStyles.Right; - btnSupport.Style = UIStyle.Custom; - btnSupport.FillColor = ThemeManager.AccentColor; - btnSupport.ForeColor = Color.White; - btnSupport.Click += (s, e) => { - // 使用SunnyUI的现代化MessageBox - var form = new UIForm(); - form.Style = UIStyle.Custom; - form.Text = "支持作者"; - form.Size = new Size(400, 200); - form.StartPosition = FormStartPosition.CenterParent; - - var label = new Label(); - label.Text = "您确定要前往GitHub支持作者吗?"; - label.Font = new Font("Microsoft YaHei", 10); - label.AutoSize = false; - label.Size = new Size(300, 40); - label.TextAlign = ContentAlignment.MiddleCenter; - label.Location = new Point(50, 40); - form.Controls.Add(label); - - var btnOK = new UIButton(); - btnOK.Text = "确定"; - btnOK.Style = UIStyle.Custom; - btnOK.Size = new Size(80, 30); - btnOK.Location = new Point(120, 100); - btnOK.Click += (s, e) => { - form.DialogResult = DialogResult.OK; - form.Close(); - }; - form.Controls.Add(btnOK); - - var btnCancel = new UIButton(); - btnCancel.Text = "取消"; - btnCancel.Style = UIStyle.Custom; - btnCancel.Size = new Size(80, 30); - btnCancel.Location = new Point(220, 100); - btnCancel.Click += (s, e) => { - form.DialogResult = DialogResult.Cancel; - form.Close(); - }; - form.Controls.Add(btnCancel); - - if (form.ShowDialog(this) == DialogResult.OK) - { - Process.Start(new ProcessStartInfo("https://github.com/zs-yg/kortapp-z") { UseShellExecute = true }); - } - }; - buttonPanel.Controls.Add(btnSupport); - - // 导航按钮样式 - Action