using System; using System.Windows.Forms; using System.Drawing; namespace KitchenTimer { public class KitchenTimer : Form { private Timer timer; private Label timerLabel; private Button hourUpButton; private Button hourDownButton; private Button minuteUpButton; private Button minuteDownButton; private Button secondUpButton; private Button secondDownButton; private Button startButton; private Button pauseButton; private Button resetButton; private int remainingTime; // タイマーの残り時間(秒単位) private bool isPaused; // タイマーが一時停止中かどうかを示すフラグ private int pausedTime; // 一時停止した時点での残り時間 public KitchenTimer() { InitializeComponent(); } private void InitializeComponent() { timer = new Timer(); timer.Interval = 1000; // 1秒ごとにタイマーを更新 timer.Tick += new EventHandler(timer_Tick); timerLabel = new Label(); timerLabel.Text = "00:00:00"; timerLabel.TextAlign = ContentAlignment.MiddleCenter; timerLabel.Font = new Font("MS UI Gothic", 25, FontStyle.Bold); timerLabel.Location = new Point(25, 10); timerLabel.Size = new Size(150, 30); hourUpButton = new Button(); hourUpButton.Text = "▲"; hourUpButton.Click += new EventHandler(hourUpButton_Click); hourUpButton.Location = new Point(50, 50); hourDownButton = new Button(); hourDownButton.Text = "▼"; hourDownButton.Click += new EventHandler(hourDownButton_Click); hourDownButton.Location = new Point(50, 80); minuteUpButton = new Button(); minuteUpButton.Text = "▲"; minuteUpButton.Click += new EventHandler(minuteUpButton_Click); minuteUpButton.Location = new Point(100, 50); minuteDownButton = new Button(); minuteDownButton.Text = "▼"; minuteDownButton.Click += new EventHandler(minuteDownButton_Click); minuteDownButton.Location = new Point(100, 80); secondUpButton = new Button(); secondUpButton.Text = "▲"; secondUpButton.Click += new EventHandler(secondUpButton_Click); secondUpButton.Location = new Point(150, 50); secondDownButton = new Button(); secondDownButton.Text = "▼"; secondDownButton.Click += new EventHandler(secondDownButton_Click); secondDownButton.Location = new Point(150, 80); startButton = new Button(); startButton.Text = "Start"; startButton.Click += new EventHandler(startButton_Click); startButton.Location = new Point(50, 110); pauseButton = new Button(); pauseButton.Text = "Pause"; pauseButton.Enabled = false; pauseButton.Click += new EventHandler(pauseButton_Click); pauseButton.Location = new Point(50, 140); resetButton = new Button(); resetButton.Text = "Reset"; resetButton.Enabled = false; resetButton.Click += new EventHandler(resetButton_Click); resetButton.Location = new Point(50, 170); this.Controls.Add(timerLabel); this.Controls.Add(hourUpButton); this.Controls.Add(hourDownButton); this.Controls.Add(minuteUpButton); this.Controls.Add(minuteDownButton); this.Controls.Add(secondUpButton); this.Controls.Add(secondDownButton); this.Controls.Add(startButton); this.Controls.Add(pauseButton); this.Controls.Add(resetButton); this.Name = "MainForm"; this.Text = "Kitchen タイマー"; this.Width = 200; this.Height = 300; } private void hourUpButton_Click(object sender, EventArgs e) { int hours = int.Parse(timerLabel.Text.Substring(0, 2)); hours++; if (hours > 23) { hours = 0; } timerLabel.Text = FormatTime(hours, int.Parse(timerLabel.Text..Substring(3, 2)), int.Parse(timerLabel.Text.Substring(6, 2))); } private void hourDownButton_Click(object sender, EventArgs e) { int hours = int.Parse(timerLabel.Text.Substring(0, 2)); hours--; if (hours < 0) { hours = 23; } timerLabel.Text = FormatTime(hours, int.Parse(timerLabel.Text..Substring(3, 2)), int.Parse(timerLabel.Text.Substring(6, 2))); } private void minuteUpButton_Click(object sender, EventArgs e) { int minutes = int.Parse(timerLabel.Text.Substring(3, 2)); minutes++; if (minutes > 59) { minutes = 0; } timerLabel.Text = FormatTime(int.Parse(timerLabel.Text.Substring(0, 2)), minutes, int.Parse(timerLabel.Text.Substring(6, 2))); } private void minuteDownButton_Click(object sender, EventArgs e) { int minutes = int.Parse(timerLabel.Text.Substring(3, 2)); minutes--; if (minutes < 0) { minutes = 59; } timerLabel.Text = FormatTime(int.Parse(timerLabel.Text.Substring(0, 2)), minutes, int.Parse(timerLabel.Text.Substring(6, 2))); } private void secondUpButton_Click(object sender, EventArgs e) { int seconds = int.Parse(timerLabel.Text.Substring(6, 2)); seconds++; if (seconds > 59) { seconds = 0; } timerLabel.Text = FormatTime(int.Parse(timerLabel.Text.Substring(0, 2)), int.Parse(timerLabel.Text.Substring(3, 2)), seconds); } private void secondDownButton_Click(object sender, EventArgs e) { int seconds = int.Parse(timerLabel.Text.Substring(6, 2)); seconds--; if (seconds < 0) { seconds = 59; } timerLabel.Text = FormatTime(int.Parse(timerLabel.Text.Substring(0, 2)), int.Parse(timerLabel.Text.Substring(3, 2)), seconds); } private void startButton_Click(object sender, EventArgs e) { remainingTime = int.Parse(timerLabel.Text.Substring(0, 2)) * 3600 + int.Parse(timerLabel.Text.Substring(3, 2)) * 60 + int.Parse(timerLabel.Text.Substring(6, 2)); timer.Start(); startButton.Enabled = false; pauseButton.Enabled = true; resetButton.Enabled = true; } private void pauseButton_Click(object sender, EventArgs e) { if (!isPaused) { timer.Stop(); pausedTime = remainingTime; isPaused = true; pauseButton.Text = "Resume"; } else { timer.Start(); isPaused = false; pauseButton.Text = "Pause"; } } private void resetButton_Click(object sender, EventArgs e) { timer.Stop(); timerLabel.Text = "00:00:00"; startButton.Enabled = true; pauseButton.Enabled = false; resetButton.Enabled = false; isPaused = false; pauseButton.Text = "Pause"; } private void timer_Tick(object sender, EventArgs e) { remainingTime--; if (remainingTime <= 0) { timer.Stop(); MessageBox.Show("時間が経過しました!", "タイマー終了", MessageBoxButtons.OK, MessageBoxIcon.Information); resetButton_Click(sender, e); } else { timerLabel.Text = FormatTime(remainingTime / 3600, (remainingTime % 3600) / 60, remainingTime % 60); } } private string FormatTime(int hours, int minutes, int seconds) { return hours.ToString("D2") + ":" + minutes.ToString("D2") + ":" + seconds.ToString("D2"); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new KitchenTimer()); } } }