using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Update { public partial class CountdownMessageBox : Form { private int _countdownSeconds = 3; private Label _lblMessage; private Label _lblCountdown; public CountdownMessageBox(string message, int seconds = 3) { _countdownSeconds = seconds; // 窗体设置 this.Text = "提示"; this.StartPosition = FormStartPosition.CenterScreen; this.FormBorderStyle = FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.ControlBox = false; this.TopMost = true; this.ShowInTaskbar = false; // 设置窗体大小 this.Size = new Size(400, 200); // 创建消息标签 - 关键设置 _lblMessage = new Label(); _lblMessage.Text = message; // 先设置文本 _lblMessage.Font = new Font("Microsoft YaHei", 10); _lblMessage.TextAlign = ContentAlignment.MiddleCenter; _lblMessage.AutoSize = false; // 设置为false _lblMessage.Size = new Size(360, 120); // 明确设置大小 _lblMessage.Location = new Point(20, 20); // 创建倒计时标签 _lblCountdown = new Label(); _lblCountdown.Text = $"({_countdownSeconds}秒后自动关闭)"; _lblCountdown.Font = new Font("Microsoft YaHei", 9); _lblCountdown.ForeColor = Color.Blue; _lblCountdown.TextAlign = ContentAlignment.MiddleCenter; _lblCountdown.AutoSize = false; _lblCountdown.Size = new Size(360, 25); _lblCountdown.Location = new Point(20, 150); // 添加控件 this.Controls.Add(_lblMessage); this.Controls.Add(_lblCountdown); // 初始化计时器 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer { Interval = 1000 }; timer.Tick += (s, e) => { _countdownSeconds--; _lblCountdown.Text = $"({_countdownSeconds}秒后自动关闭)"; if (_countdownSeconds <= 0) { timer.Stop(); this.Close(); } }; this.Shown += (s, e) => timer.Start(); // 确保控件可见 _lblMessage.Visible = true; _lblCountdown.Visible = true; // 强制刷新显示 this.Refresh(); } } }