From 4831af1f69319db74c656c0f222686cd62da7962 Mon Sep 17 00:00:00 2001 From: zsyg <3872006562@qq.com> Date: Sat, 2 Aug 2025 20:52:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BD=91=E7=AB=99=E6=8E=A8?= =?UTF-8?q?=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainForm.cs | 42 ++++++++++- WebSiteCards.cs | 196 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 WebSiteCards.cs diff --git a/MainForm.cs b/MainForm.cs index 4a59787..afc7161 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -147,6 +147,8 @@ namespace AppStore // 软件下载按钮 private Button btnApps = null!; + // 网站推荐按钮 + private Button btnWebsites = null!; // 下载进度按钮 private Button btnDownloads = null!; // 设置按钮 @@ -155,6 +157,8 @@ namespace AppStore private Button btnAbout = null!; // 内容显示面板 private Panel contentPanel = null!; + // 网站卡片流式布局面板 + private FlowLayoutPanel websitesFlowPanel = new FlowLayoutPanel(); // 系统托盘图标 private NotifyIcon trayIcon = null!; // 托盘右键菜单 @@ -372,10 +376,21 @@ namespace AppStore }; buttonPanel.Controls.Add(btnAbout); + // 网站推荐按钮 + btnWebsites = new Button(); + btnWebsites.Text = "网站推荐"; + btnWebsites.Location = new Point(590, 0); + styleButton(btnWebsites); + btnWebsites.Click += (s, e) => { + Logger.Log("用户点击了'网站推荐'按钮"); + ShowWebsitesView(); + }; + buttonPanel.Controls.Add(btnWebsites); + // 内置工具按钮 var btnTools = new Button(); btnTools.Text = "内置工具"; - btnTools.Location = new Point(590, 0); + btnTools.Location = new Point(730, 0); styleButton(btnTools); btnTools.Click += (s, e) => { Logger.Log("用户点击了'内置工具'按钮"); @@ -830,6 +845,31 @@ namespace AppStore } } + private void ShowWebsitesView() + { + contentPanel.Controls.Clear(); + websitesFlowPanel.Dock = DockStyle.Fill; + websitesFlowPanel.AutoScroll = true; + websitesFlowPanel.WrapContents = false; + websitesFlowPanel.Padding = new Padding(20); + contentPanel.Controls.Add(websitesFlowPanel); + + // 添加示例网站卡片 + var card1 = new WebSiteCards(); + card1.WebSiteName = "GitHub"; + card1.WebSiteIconPath = "img/jpg/github.jpg"; + card1.Description = "全球最大的代码托管平台,支持Git版本控制"; + card1.WebSiteUrl = "https://github.com"; + websitesFlowPanel.Controls.Add(card1); + + var card2 = new WebSiteCards(); + card2.WebSiteName = "Stack Overflow"; + card2.WebSiteIconPath = "img/png/StackOverflow.png"; + card2.Description = "程序员问答社区,解决各种编程问题"; + card2.WebSiteUrl = "https://stackoverflow.com"; + websitesFlowPanel.Controls.Add(card2); + } + private void ShowAboutView() { contentPanel.Controls.Clear(); diff --git a/WebSiteCards.cs b/WebSiteCards.cs new file mode 100644 index 0000000..3a486d6 --- /dev/null +++ b/WebSiteCards.cs @@ -0,0 +1,196 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using System.Diagnostics; + +namespace AppStore +{ + public class WebSiteCards : UserControl + { + private PictureBox iconBox; + private Label nameLabel; + private Label descriptionLabel; + private Button visitBtn; + private ToolTip toolTip; + private Color borderColor = SystemColors.ControlDark; + + public string WebSiteName { get; set; } = string.Empty; + private Image _webSiteIcon = SystemIcons.Application.ToBitmap(); + public Image WebSiteIcon + { + get { return _webSiteIcon; } + set + { + try + { + if (value != null) + { + _webSiteIcon = value; + } + else + { + _webSiteIcon = SystemIcons.Application.ToBitmap(); + } + } + catch + { + _webSiteIcon = SystemIcons.Application.ToBitmap(); + } + UpdateDisplay(); + } + } + + public string WebSiteIconPath + { + set + { + try + { + string path = value; + if (!Path.IsPathRooted(path)) + { + path = Path.Combine(Application.StartupPath, path); + } + + if (File.Exists(path)) + { + _webSiteIcon = Image.FromFile(path); + } + else + { + _webSiteIcon = SystemIcons.Application.ToBitmap(); + } + } + catch + { + _webSiteIcon = SystemIcons.Application.ToBitmap(); + } + UpdateDisplay(); + } + } + public string WebSiteUrl { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + + public WebSiteCards() + { + iconBox = new PictureBox() { SizeMode = PictureBoxSizeMode.StretchImage }; + nameLabel = new Label() { Text = string.Empty }; + descriptionLabel = new Label() { Text = string.Empty }; + visitBtn = new Button() { Text = "访问" }; + toolTip = new ToolTip(); + + InitializeComponent(); + } + + private void InitializeComponent() + { + this.Size = new Size(400, 150); // 更宽的卡片以适应横向布局 + this.BackColor = Color.White; + this.Padding = new Padding(10); + + // 网站名称标签 - 顶部 + nameLabel.Font = new Font("Microsoft YaHei", 10, FontStyle.Bold); + nameLabel.TextAlign = ContentAlignment.MiddleLeft; + nameLabel.Location = new Point(10, 10); + nameLabel.Size = new Size(Width - 20, 20); + this.Controls.Add(nameLabel); + + // 网站图标 - 左侧 + iconBox.Size = new Size(80, 80); + iconBox.Location = new Point(10, 40); + this.Controls.Add(iconBox); + + // 网站描述 - 右侧 + descriptionLabel.Font = new Font("Microsoft YaHei", 9); + descriptionLabel.TextAlign = ContentAlignment.TopLeft; + descriptionLabel.AutoSize = false; + descriptionLabel.Size = new Size(Width - 110, 80); + descriptionLabel.Location = new Point(100, 40); + descriptionLabel.MaximumSize = new Size(Width - 110, 0); // 允许自动换行 + this.Controls.Add(descriptionLabel); + + // 访问按钮 - 右下角 + visitBtn.Size = new Size(80, 30); + visitBtn.Location = new Point(Width - 90, Height - 40); + visitBtn.Click += VisitBtn_Click; + this.Controls.Add(visitBtn); + + // 工具提示 + toolTip.AutoPopDelay = 5000; + toolTip.InitialDelay = 500; + toolTip.ReshowDelay = 500; + toolTip.ShowAlways = true; + toolTip.SetToolTip(visitBtn, "在浏览器中打开网站"); + + UpdateDisplay(); + UpdateLabelTheme(); + ThemeManager.ThemeChanged += (theme) => UpdateLabelTheme(); + } + + private void UpdateLabelTheme() + { + if (ThemeManager.CurrentTheme == ThemeManager.ThemeMode.Dark) + { + this.BackColor = Color.FromArgb(45, 45, 48); + nameLabel.ForeColor = Color.White; + descriptionLabel.ForeColor = Color.White; + } + else + { + this.BackColor = Color.White; + nameLabel.ForeColor = Color.Black; + descriptionLabel.ForeColor = Color.Black; + } + } + + public void UpdateDisplay() + { + nameLabel.Text = WebSiteName; + iconBox.Image = WebSiteIcon; + descriptionLabel.Text = Description; + } + + private void VisitBtn_Click(object sender, EventArgs e) + { + try + { + if (!string.IsNullOrEmpty(WebSiteUrl)) + { + Process.Start(new ProcessStartInfo + { + FileName = WebSiteUrl, + UseShellExecute = true + }); + } + } + catch (Exception ex) + { + Logger.LogError($"打开网站失败: {ex.Message}"); + } + } + + protected override void OnPaint(PaintEventArgs e) + { + base.OnPaint(e); + + // 绘制圆角边框 + using (var pen = new Pen(borderColor, 1)) + { + int radius = 10; + var rect = new Rectangle(0, 0, Width - 1, Height - 1); + e.Graphics.DrawPath(pen, GetRoundedRectPath(rect, radius)); + } + } + + private System.Drawing.Drawing2D.GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) + { + var path = new System.Drawing.Drawing2D.GraphicsPath(); + path.AddArc(rect.X, rect.Y, radius, radius, 180, 90); + path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90); + path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90); + path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90); + path.CloseFigure(); + return path; + } + } +}