事業内容

簡易タイマーその2




以下のリンクから左クリックでダウンロードして使ってみてください。
圧縮形式はzipです。
.NetFrameWork3.5で開発しています。

簡易タイマーその2

30分と2時間に対応させています。

残り時間に応じて下記のメッセージが表示されます。

残り時間半分以上・・・・・まだまだ余裕!
残り時間半分未満・・・・・だんだんせまってきたよ!
残り時間10分未満・・・・・残り10分切ったよ!
残り時間5分未満・・・・・残り5分切ったよ!
残り時間1分未満・・・・・準備して!
残り時間0分・・・・・キターーー!


残り時間が0分になるとシステム音を100ミリ秒単位で鳴らします。
多少の誤差が発生する可能性があるのでまぁ気をつけてください。
バグがあったら教えてください。


プログラムは下記のようになってます。


public partial class Window1 : Window
{
    private const string MIN_START = "30分スタート!";
    private const string MIN_END = "30分ストップ!";
    private const string HOUR_START = "2時間スタート!";
    private const string HOUR_END = "2時間ストップ!";

    private const string NOTYET = "まだはじまってないよ!";
    private const string OVER_HALF = "まだまだ余裕!";
    private const string UNDER_HALF = "だんだんせまってきたよ!";
    private const string UNDER_10MIN = "残り10分切ったよ!";
    private const string UNDER_5MIN = "残り5分切ったよ!";
    private const string UNDER_1MIN = "準備して!";
    private const string FINISH = "キターーー!";

    private const double INTERVAL = 60000d;
    private const double SOUND_INTERVAL = 100d;

    private Timer minTimer = null;
    private Timer hourTimer = null;

    private int minCount = 0;
    private int hourCount = 0;

    public Window1()
    {
        InitializeComponent();

        this.SetInitialContent();

        this.minTimer = new Timer();
        this.minTimer.Interval = INTERVAL;
        this.minTimer.Elapsed += new ElapsedEventHandler(minTimer_Elapsed);
        this.hourTimer = new Timer();
        this.hourTimer.Interval = INTERVAL;
        this.hourTimer.Elapsed += new ElapsedEventHandler(hourTimer_Elapsed);
    }

    void hourTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        this.hourLabel.Dispatcher.Invoke((Action)delegate() {
            this.hourCount++;
            if (this.hourCount == 61) {
                this.hourLabel.Content = UNDER_HALF;
            } else if (this.hourCount == 110) {
                this.hourLabel.Content = UNDER_10MIN;
                this.hourLabel.Background = new SolidColorBrush(Colors.YellowGreen);
            } else if (this.hourCount == 115) {
                this.hourLabel.Content = UNDER_5MIN;
                this.hourLabel.Background = new SolidColorBrush(Colors.Aqua);
            } else if (this.hourCount == 119) {
                this.hourLabel.Content = UNDER_1MIN;
                this.hourLabel.Background = new SolidColorBrush(Colors.Red);
            } else if (this.hourCount >= 120) {
                this.hourLabel.Content = FINISH;
                if (this.hourTimer.Interval == INTERVAL) {
                    this.hourTimer.Interval = SOUND_INTERVAL;
                }
                SystemSounds.Beep.Play();
            }
        }, null);
    }

    void minTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        this.minLabel.Dispatcher.Invoke((Action)delegate() {
            this.minCount++;
            if (this.minCount == 14) {
                this.minLabel.Content = UNDER_HALF;
            } else if (this.minCount == 20) {
                this.minLabel.Content = UNDER_10MIN;
                this.minLabel.Background = new SolidColorBrush(Colors.YellowGreen);
            } else if (this.minCount == 25) {
                this.minLabel.Content = UNDER_5MIN;
                this.minLabel.Background = new SolidColorBrush(Colors.Aqua);
            } else if (this.minCount == 29) {
                this.minLabel.Content = UNDER_1MIN;
                this.minLabel.Background = new SolidColorBrush(Colors.Red);
            } else if (this.minCount >= 30) {
                this.minLabel.Content = FINISH;
                if (this.minTimer.Interval == INTERVAL) {
                    this.minTimer.Interval = SOUND_INTERVAL;
                }
                SystemSounds.Asterisk.Play();
            }
        }, null);
    }

    private void minButton_Click(object sender, RoutedEventArgs e)
    {
        if (this.minButton.Content.ToString() == MIN_START) {
            this.minButton.Content = MIN_END;
            this.minLabel.Content = OVER_HALF;
            this.minTimer.Start();
        } else {
            this.minButton.Content = MIN_START;
            this.minLabel.Content = NOTYET;
            this.minLabel.Background = new SolidColorBrush(Colors.White);
            this.minTimer.Interval = INTERVAL;
            this.minTimer.Stop();
            this.minCount = 0;
        }
    }

    private void hourButton_Click(object sender, RoutedEventArgs e)
    {
        if (this.hourButton.Content.ToString() == HOUR_START) {
            this.hourButton.Content = HOUR_END;
            this.hourLabel.Content = OVER_HALF;
            this.hourTimer.Start();
        } else {
            this.hourButton.Content = HOUR_START;
            this.hourLabel.Content = NOTYET;
            this.hourLabel.Background = new SolidColorBrush(Colors.White);
            this.hourTimer.Interval = INTERVAL;
            this.hourTimer.Stop();
            this.hourCount = 0;
        }
    }

    private void SetInitialContent()
    {
        this.minButton.Content = MIN_START;
        this.hourButton.Content = HOUR_START;

        this.minLabel.Content = NOTYET;
        this.hourLabel.Content = NOTYET;
    }
}



修正履歴



ツール開発へ

テンプレートのpondt