끝나지 않는 프로그래밍 일기


정확히 이 프로그램을 만들게 된 이유는 게임하다가 배터리 알림음이 묻혀서 갑자기 컴퓨터가 종료되는 일이 부쩍 많아져서 이런 프로그램을 만들게 되네요.. <

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace BATTERYALERT
{
    public partial class Form1 : Form
    {
        SoundPlayer sPlayer = null;
        bool isStarting = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            sPlayer = new SoundPlayer();
            sPlayer.Stream = Properties.Resources.complete;
        }

        private String GetBatteryStatus()
        {
            String status = String.Empty, percent = String.Empty;

            if (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Offline) status = "Battery";
            else
                if (SystemInformation.PowerStatus.BatteryLifePercent < 1) status = "Charge";
                else status = "AC";

            return status + "/" + Convert.ToString(SystemInformation.PowerStatus.BatteryLifePercent * 100);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            String getStatus = GetBatteryStatus();
            String status = getStatus.Split('/')[0];
            Int32 percent = Convert.ToInt32(getStatus.Split('/')[1]);

            if (!status.Equals("Charge"))
                if (percent <= Convert.ToInt32(batteryRecommand.Text)) 
                    sPlayer.Play();
        }

        private void alertPlay()
        {
            if (radioButton1.Checked) sPlayer.Play();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!isStarting)
            {
                if (alertTiming.Text == String.Empty) alertTiming.Text = "5"; // Default Value
                if (batteryRecommand.Text == String.Empty) batteryRecommand.Text = "10";
                timer1.Interval = Convert.ToInt32(alertTiming.Text) * 1000;
                MessageBox.Show("알림이 시작되었습니다.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                timer1.Start();
                isStarting = true;
                alertTiming.Enabled = false;
                batteryRecommand.Enabled = false;
                button1.Text = "알림 멈춤";
            }
            else
            {
                MessageBox.Show("알림이 중단되었습니다.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                timer1.Stop();
                button1.Text = "알림 시작";
                isStarting = false;
                alertTiming.Enabled = true;
                batteryRecommand.Enabled = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == false)
            {
                button2.Enabled = true;
            }
            else { button2.Enabled = false; sPlayer.Stream = Properties.Resources.complete; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "wav 파일(*.wav)|*.wav";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                sPlayer.SoundLocation = ofd.FileName;
            }
        }
    }
}