using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Speech.Synthesis; using System.Threading; namespace ClassNotifyClient { public class NotifyForm : Form { private string _title = "🔔 老师呼叫"; private string _message = "请到办公室一趟。"; private string _type = "call"; // call, notice, urgent private bool _enableVoice = true; private bool _enableShake = true; private int _remainingSeconds = 15; private System.Windows.Forms.Timer _countdownTimer; private Label _lblCountdown; private Button _btnConfirm; private Color _themeColor = Color.FromArgb(37, 99, 235); // Blue by default public NotifyForm(string title, string message, string type, bool voice, bool shake, int duration) { if (!string.IsNullOrEmpty(title)) _title = title; if (!string.IsNullOrEmpty(message)) _message = message; if (!string.IsNullOrEmpty(type)) _type = type.ToLower(); _enableVoice = voice; _enableShake = shake; _remainingSeconds = duration > 0 ? duration : 15; // Theme colors based on type switch (_type) { case "urgent": _themeColor = Color.FromArgb(220, 38, 38); // Red if (string.IsNullOrEmpty(title)) _title = "⚠️ 紧急提醒"; break; case "notice": _themeColor = Color.FromArgb(5, 150, 105); // Green if (string.IsNullOrEmpty(title)) _title = "📢 班级通知"; break; case "call": default: _themeColor = Color.FromArgb(37, 99, 235); // Blue if (string.IsNullOrEmpty(title)) _title = "🔔 老师呼叫"; break; } InitializeComponents(); } private void InitializeComponents() { this.FormBorderStyle = FormBorderStyle.None; this.StartPosition = FormStartPosition.CenterScreen; this.Size = new Size(680, 420); this.TopMost = true; this.ShowInTaskbar = false; this.BackColor = Color.White; this.DoubleBuffered = true; // Header Panel Panel pnlHeader = new Panel { Dock = DockStyle.Top, Height = 80, BackColor = _themeColor }; Label lblTitle = new Label { Text = _title, Font = new Font("Microsoft YaHei", 20, FontStyle.Bold), ForeColor = Color.White, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleCenter }; pnlHeader.Controls.Add(lblTitle); this.Controls.Add(pnlHeader); // Body Panel for Message Panel pnlBody = new Panel { Dock = DockStyle.Fill, Padding = new Padding(30, 20, 30, 10), BackColor = Color.FromArgb(248, 250, 252) }; Label lblMessage = new Label { Text = _message, Font = new Font("Microsoft YaHei", 26, FontStyle.Bold), ForeColor = Color.FromArgb(30, 41, 59), Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleCenter, AutoEllipsis = true }; pnlBody.Controls.Add(lblMessage); this.Controls.Add(pnlBody); // Footer Panel for Buttons & Countdown Panel pnlFooter = new Panel { Dock = DockStyle.Bottom, Height = 110, BackColor = Color.FromArgb(248, 250, 252) }; _btnConfirm = new Button { Text = "我知道了", Font = new Font("Microsoft YaHei", 16, FontStyle.Bold), Size = new Size(220, 52), Location = new Point((this.ClientSize.Width - 220) / 2, 10), BackColor = _themeColor, ForeColor = Color.White, FlatStyle = FlatStyle.Flat, Cursor = Cursors.Hand }; _btnConfirm.FlatAppearance.BorderSize = 0; _btnConfirm.Click += BtnConfirm_Click; _lblCountdown = new Label { Text = string.Format("{0} 秒后自动关闭", _remainingSeconds), Font = new Font("Microsoft YaHei", 10), ForeColor = Color.FromArgb(100, 116, 139), Size = new Size(300, 25), Location = new Point((this.ClientSize.Width - 300) / 2, 70), TextAlign = ContentAlignment.MiddleCenter }; pnlFooter.Controls.Add(_btnConfirm); pnlFooter.Controls.Add(_lblCountdown); this.Controls.Add(pnlFooter); // Countdown Timer _countdownTimer = new System.Windows.Forms.Timer(); _countdownTimer.Interval = 1000; _countdownTimer.Tick += CountdownTimer_Tick; this.Load += NotifyForm_Load; this.Paint += NotifyForm_Paint; } private void NotifyForm_Paint(object sender, PaintEventArgs e) { // Draw a subtle border around the frameless window using (Pen borderPen = new Pen(_themeColor, 3)) { e.Graphics.DrawRectangle(borderPen, 1, 1, this.Width - 3, this.Height - 3); } } private void NotifyForm_Load(object sender, EventArgs e) { _countdownTimer.Start(); // Shake window if enabled if (_enableShake) { ThreadPool.QueueUserWorkItem((state) => { Thread.Sleep(100); ShakeWindow(); }); } // Speak message if voice enabled if (_enableVoice) { ThreadPool.QueueUserWorkItem((state) => { PlayTTS(); }); } } private void ShakeWindow() { try { this.Invoke((MethodInvoker)delegate { int origX = this.Location.X; int origY = this.Location.Y; int[] offsets = new int[] { -18, 18, -14, 14, -10, 10, -7, 7, -4, 4, 0 }; foreach (int dx in offsets) { this.Location = new Point(origX + dx, origY); Application.DoEvents(); Thread.Sleep(30); } this.Location = new Point(origX, origY); }); } catch { } } private void PlayTTS() { try { using (SpeechSynthesizer synth = new SpeechSynthesizer()) { synth.SetOutputToDefaultAudioDevice(); synth.Rate = 0; // Normal speed synth.Volume = 100; synth.Speak(_message); } } catch { } } private void BtnConfirm_Click(object sender, EventArgs e) { _countdownTimer.Stop(); Environment.ExitCode = 0; // 0 = Confirmed by student/classroom this.Close(); } private void CountdownTimer_Tick(object sender, EventArgs e) { _remainingSeconds--; if (_remainingSeconds <= 0) { _countdownTimer.Stop(); Environment.ExitCode = 1; // 1 = Timeout auto-close this.Close(); } else { _lblCountdown.Text = string.Format("{0} 秒后自动关闭", _remainingSeconds); } } [STAThread] public static int Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string title = ""; string message = "张三,请到办公室一趟。"; string type = "call"; bool voice = true; bool shake = true; int duration = 15; // Parse command line arguments for (int i = 0; i < args.Length; i++) { string arg = args[i]; if (arg.Equals("--title", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { title = args[++i]; } else if (arg.Equals("--msg", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { message = args[++i]; } else if (arg.Equals("--type", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { type = args[++i]; } else if (arg.Equals("--voice", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { bool.TryParse(args[++i], out voice); } else if (arg.Equals("--shake", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { bool.TryParse(args[++i], out shake); } else if (arg.Equals("--duration", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length) { int.TryParse(args[++i], out duration); } } NotifyForm form = new NotifyForm(title, message, type, voice, shake, duration); Application.Run(form); return Environment.ExitCode; } } }