달나라 노트

C# Sample : 타이머 30분, 20분, 15분 본문

C#/C# Samples

C# Sample : 타이머 30분, 20분, 15분

CosmosProject 2022. 4. 21. 02:39
728x90
반응형

 

 

 

30분 짜리, 20분 짜리, 15분 짜리 타이머

 

using System;
using System.Windows.Forms;
using WMPLib;

class MyProgram
{
    public static void Main()
    {
        // Constants //
        int form_width = 400;
        int form_height = 200;

        int label_width = 100;
        int label_height = 30;

        int btn_width = 100;
        int btn_height = 30;

        String dir_current = System.Environment.CurrentDirectory.ToString();
        String dir_mp3 = "\\mp3\\sample.mp3";
        dir_mp3 = dir_current + dir_mp3;
        dir_mp3 = dir_mp3.Replace("\\", "\\\\");

        int timer_interval = 1000;
        // Constants //

        Form fm = new Form();
        fm.Width = form_width;
        fm.Height = form_height;


        // Timer - 30 minutes //
        Label lbl_30 = new Label();
        lbl_30.Parent = fm;
        lbl_30.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        lbl_30.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        lbl_30.Font = new System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Bold);
        lbl_30.Width = label_width;
        lbl_30.Height = label_height;

        Button btn_30_start = new Button();
        btn_30_start.Parent = fm;
        btn_30_start.Text = "Start (30)";
        btn_30_start.Width = btn_width;
        btn_30_start.Height = btn_height;
        btn_30_start.Left = lbl_30.Right;

        Button btn_30_stop = new Button();
        btn_30_stop.Parent = fm;
        btn_30_stop.Text = "Stop (30)";
        btn_30_stop.Width = btn_width;
        btn_30_stop.Height = btn_height;
        btn_30_stop.Left = btn_30_start.Right;

        MyTimer timer_30 = new MyTimer(30, dir_mp3, timer_interval, lbl_30);

        void event_click_btn_start_30(object sender, EventArgs e)
        {
            timer_30.start_timer();
        }
        btn_30_start.Click += new EventHandler(event_click_btn_start_30);

        void event_click_btn_stop_30(object sender, EventArgs e)
        {
            timer_30.stop_timer();
        }
        btn_30_stop.Click += new EventHandler(event_click_btn_stop_30);


        // Timer - 20 minutes //
        Label lbl_20 = new Label();
        lbl_20.Parent = fm;
        lbl_20.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        lbl_20.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        lbl_20.Font = new System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Bold);
        lbl_20.Width = label_width;
        lbl_20.Height = label_height;
        lbl_20.Top = lbl_30.Bottom;

        Button btn_20_start = new Button();
        btn_20_start.Parent = fm;
        btn_20_start.Text = "Start (20)";
        btn_20_start.Width = btn_width;
        btn_20_start.Height = btn_height;
        btn_20_start.Top = lbl_30.Bottom;
        btn_20_start.Left = lbl_30.Right;

        Button btn_20_stop = new Button();
        btn_20_stop.Parent = fm;
        btn_20_stop.Text = "Stop (20)";
        btn_20_stop.Width = btn_width;
        btn_20_stop.Height = btn_height;
        btn_20_stop.Top = lbl_30.Bottom;
        btn_20_stop.Left = btn_30_start.Right;

        MyTimer timer_20 = new MyTimer(20, dir_mp3, timer_interval, lbl_20);

        void event_click_btn_start_20(object sender, EventArgs e)
        {
            timer_20.start_timer();
        }
        btn_20_start.Click += new EventHandler(event_click_btn_start_20);

        void event_click_btn_stop_20(object sender, EventArgs e)
        {
            timer_20.stop_timer();
        }
        btn_20_stop.Click += new EventHandler(event_click_btn_stop_20);


        // Timer - 15 minutes //
        Label lbl_15 = new Label();
        lbl_15.Parent = fm;
        lbl_15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        lbl_15.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        lbl_15.Font = new System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Bold);
        lbl_15.Width = label_width;
        lbl_15.Height = label_height;
        lbl_15.Top = lbl_20.Bottom;

        Button btn_15_start = new Button();
        btn_15_start.Parent = fm;
        btn_15_start.Text = "Start (15)";
        btn_15_start.Width = btn_width;
        btn_15_start.Height = btn_height;
        btn_15_start.Top = lbl_20.Bottom;
        btn_15_start.Left = lbl_20.Right;

        Button btn_15_stop = new Button();
        btn_15_stop.Parent = fm;
        btn_15_stop.Text = "Stop (15)";
        btn_15_stop.Width = btn_width;
        btn_15_stop.Height = btn_height;
        btn_15_stop.Top = lbl_20.Bottom;
        btn_15_stop.Left = btn_15_start.Right;

        MyTimer timer_15 = new MyTimer(15, dir_mp3, timer_interval, lbl_15);

        void event_click_btn_start_15(object sender, EventArgs e)
        {
            timer_15.start_timer();
        }
        btn_15_start.Click += new EventHandler(event_click_btn_start_15);

        void event_click_btn_stop_15(object sender, EventArgs e)
        {
            timer_15.stop_timer();
        }
        btn_15_stop.Click += new EventHandler(event_click_btn_stop_15);




        Application.Run(fm);
    }

    class MyTimer
    {
        // Constructor //
        public MyTimer(int min, string dir_mp3, int timer_interval, Label label)
        {
            this.minute = min;
            this.second = min * 60;

            this.dir_mp3 = dir_mp3;

            this.tm = new Timer();
            this.timer_interval = timer_interval;
            this.set_timer();

            this.wmp = new WindowsMediaPlayer();
            this.set_wmp();

            calculate_left_time();

            this.lbl = label;
            lbl.Text = $"{this.left_time}";

            set_tick_event();
        }


        // Constants //
        public int minute;
        public int second;

        public string dir_mp3;

        public Timer tm;
        public int timer_interval;
        void set_timer()
        {
            this.tm.Interval = this.timer_interval;
        }

        public WindowsMediaPlayer wmp;
        void set_wmp()
        {
            this.wmp.URL = this.dir_mp3;
            wmp.controls.stop();
        }

        public string left_time;
        void calculate_left_time()
        {
            string left_min = Math.Truncate((double)second / 60).ToString();
            string left_sec = (second % 60).ToString();

            left_min = left_min.PadLeft(2, '0');
            left_sec = left_sec.PadLeft(2, '0');

            this.left_time = $"{left_min}:{left_sec}";
        }

        public Label lbl;


        // Methods //
        public void start_timer()
        {
            this.tm.Start();
        }

        public void stop_timer()
        {
            this.tm.Stop();
            this.second = this.minute * 60;
            this.lbl.Text = $"{this.minute}:00";
            this.wmp.controls.stop();
        }


        // Events //
        public void set_tick_event()
        {
            void event_tick_timer(object sender, EventArgs e)
            {
                this.second = this.second - 1;
                calculate_left_time();
                this.lbl.Text = this.left_time;

                if (this.second <= 5)
                {
                    this.tm.Stop();
                    lbl.Text = "Time up";
                    this.wmp.controls.play();
                }
            }
            this.tm.Tick += new EventHandler(event_tick_timer);
        }
    }
}

 

 

 

 

 

 

 

 

728x90
반응형

'C# > C# Samples' 카테고리의 다른 글

C# Samples : 포물선 운동 (Parabolic Motion)  (0) 2022.05.28
C# Samples : Graphic Timer 3개  (0) 2022.05.24
C# Sample : 주사위 번호 맞추기  (0) 2022.05.15
Comments