TKK_E32230371/UPSNet/ShutdownCountdownForm.cs

163 lines
5.6 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace UPSNet
{
/// <summary>
/// A countdown dialog that appears before Windows shutdown is executed.
/// Allows the user to cancel the shutdown during the countdown.
/// </summary>
public class ShutdownCountdownForm : Form
{
private Label labelTitle;
private Label labelCountdown;
private Label labelMessage;
private Button buttonCancel;
private Timer countdownTimer;
private int remainingSeconds;
/// <summary>
/// True if user cancelled the shutdown
/// </summary>
public bool WasCancelled { get; private set; }
/// <summary>
/// True if countdown reached zero (proceed with shutdown)
/// </summary>
public bool CountdownFinished { get; private set; }
public ShutdownCountdownForm(int seconds)
{
remainingSeconds = seconds;
WasCancelled = false;
CountdownFinished = false;
InitializeUI();
countdownTimer = new Timer();
countdownTimer.Interval = 1000;
countdownTimer.Tick += CountdownTimer_Tick;
}
private void InitializeUI()
{
this.SuspendLayout();
// Form settings
this.Text = "UPS Shutdown Warning";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.TopMost = true;
this.ClientSize = new Size(340, 180);
this.ShowInTaskbar = true;
this.BackColor = Color.FromArgb(30, 30, 30);
// Title label
labelTitle = new Label();
labelTitle.Text = "⚠ UPS Shutdown Warning";
labelTitle.Font = new Font("Segoe UI", 11F, FontStyle.Bold);
labelTitle.ForeColor = Color.OrangeRed;
labelTitle.Location = new Point(12, 15);
labelTitle.Size = new Size(316, 25);
labelTitle.TextAlign = ContentAlignment.MiddleCenter;
// Message label
labelMessage = new Label();
labelMessage.Text = "Sistem akan dimatikan dalam:";
labelMessage.Font = new Font("Segoe UI", 9.5F);
labelMessage.ForeColor = Color.White;
labelMessage.Location = new Point(12, 50);
labelMessage.Size = new Size(316, 20);
labelMessage.TextAlign = ContentAlignment.MiddleCenter;
// Countdown label
labelCountdown = new Label();
labelCountdown.Text = remainingSeconds.ToString() + " detik";
labelCountdown.Font = new Font("Segoe UI", 22F, FontStyle.Bold);
labelCountdown.ForeColor = Color.Red;
labelCountdown.Location = new Point(12, 72);
labelCountdown.Size = new Size(316, 45);
labelCountdown.TextAlign = ContentAlignment.MiddleCenter;
// Cancel button
buttonCancel = new Button();
buttonCancel.Text = "Cancel Shutdown";
buttonCancel.Font = new Font("Segoe UI", 9.5F, FontStyle.Bold);
buttonCancel.Location = new Point(70, 130);
buttonCancel.Size = new Size(200, 35);
buttonCancel.BackColor = Color.FromArgb(0, 150, 200);
buttonCancel.ForeColor = Color.White;
buttonCancel.FlatStyle = FlatStyle.Flat;
buttonCancel.FlatAppearance.BorderSize = 0;
buttonCancel.Cursor = Cursors.Hand;
buttonCancel.Click += ButtonCancel_Click;
this.Controls.Add(labelTitle);
this.Controls.Add(labelMessage);
this.Controls.Add(labelCountdown);
this.Controls.Add(buttonCancel);
this.ResumeLayout(false);
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
countdownTimer.Start();
// Play a warning sound
System.Media.SystemSounds.Exclamation.Play();
}
private void CountdownTimer_Tick(object sender, EventArgs e)
{
remainingSeconds--;
if (remainingSeconds <= 0)
{
countdownTimer.Stop();
CountdownFinished = true;
labelCountdown.Text = "0 detik";
this.Close();
}
else
{
labelCountdown.Text = remainingSeconds.ToString() + " detik";
// Flash color when critical
if (remainingSeconds <= 5)
{
labelCountdown.ForeColor = (remainingSeconds % 2 == 0) ? Color.Red : Color.Yellow;
}
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
countdownTimer.Stop();
WasCancelled = true;
CountdownFinished = false;
this.Close();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
countdownTimer.Stop();
base.OnFormClosing(e);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (countdownTimer != null)
{
countdownTimer.Dispose();
}
}
base.Dispose(disposing);
}
}
}