寫一個定時提醒小軟體,到點有音樂,或語音朗讀提醒

c979170768發表於2011-11-12
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace @lock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int formheight = 310;                      //這個是窗體原來的樣式

        int formwidth = 400;

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                this.Height += 30;
            }
            else
            {

                this.Height = formheight;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Text == "寫個")
            {
                btn.Text = "寫完";
                this.Width += 400;
                sign = false;
            }
            else
            {
                string s = "";
                if (radioButton1.Checked)
                {
                    s = radioButton1.Text;
                }
                else
                {
                    s = radioButton2.Text;
                }
                btn.Text = "寫個";
                this.Width = formwidth;
                string str = textBox2.Text + ";" + label7.Text + "," + dateTimePicker1.Text + " " + comboBox1.Text + ":" + comboBox2.Text + "," +textBox1.Text+","+ s;
                inputtxt(str);                                                                        //寫入txt

                ListViewItem lvi = listView1.Items.Add(textBox2.Text);                                 //名稱 
                lvi.SubItems.Add(label7.Text);                                                         //開始時間       
                lvi.SubItems.Add(dateTimePicker1.Text + " " + comboBox1.Text + ":" + comboBox2.Text);  //結束時間
                lvi.SubItems.Add(s);                                                                    //詳情
                lvi.SubItems.Add(textBox1.Text); 
                sign = true;

            }

        }

        private static void inputtxt(string str)    //寫文件
        {
            FileStream fs = new FileStream(@"d:/123.txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(str);
            sw.Close();
            fs.Close();
        }

        private static void allinput(string str)
        {
            FileStream fs = new FileStream(@"d:/123.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(str);
            sw.Close();
            fs.Close();

        }   //修改檔案

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            for (int i = 10; i < 60; i++)
            {
                comboBox1.Items.Add(i.ToString());
                comboBox2.Items.Add(i.ToString());
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Selected)
                {
                    MessageBox.Show(item.SubItems[4].Text);
                }    

            }


        }

        bool sign = false;

        ArrayList list = new ArrayList();   //列表

        int add = 0;  //累加器 
        private void timer1_Tick(object sender, EventArgs e)
        {
            label7.Text = System.DateTime.Now.ToString();
            add++;
            if (add > 59)
            {
                sign = true;
                add = 0;
            }
            if (sign)
            {
           //   if (label7.Text.Substring(0, label7.Text.LastIndexOf(':')) == dateTimePicker1.Text + " " + comboBox1.Text + ":" + comboBox2.Text)
                  if ("yes" == deleterecord(label7.Text.Substring(0, label7.Text.LastIndexOf(':'))))
                  {
                    sign = false;
                    // MessageBox.Show(dateTimePicker1.Text + comboBox1.Text + comboBox2.Text);  
                    if (music.Text != "" && radioButton1.Checked)
                    {
                        playmusic(); //播放音樂
                        //MessageBox.Show(msg);
                    }
                    else
                    {
                        //this.axWindowsMediaPlayer1.URL = "D:\\21.wav";
                        //this.axWindowsMediaPlayer1.Ctlcontrols.play();
                        //axWindowsMediaPlayer1.settings.setMode("loop", true);  //迴圈播放

                    }
                    //這裡是寫呼叫鈴聲
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string input = "";                         //串加器
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Selected)
                {
                    listView1.Items.Remove(item);
                    using (StreamReader sr = File.OpenText(@"d:/123.txt"))
                    {
                        string s = "";
                        while ((s = sr.ReadLine()) != null)
                        {
                            if (s != "")
                            {
                                if (s.Substring(0, s.LastIndexOf(';')) != item.Text)
                                {
                                    input += "\r\n" + s;
                                }
                            }
                        }
                    }
                    allinput(input);
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            string str = "";
            using (StreamReader sr = File.OpenText(@"d:/123.txt"))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    if (s!="")
                    {
                        ListViewItem lvi = listView1.Items.Add(s.Substring(0,s.LastIndexOf(';')));     //取名稱  
                        str=s.Substring(s.LastIndexOf(';')+1);
                        string[] m=str.Split(',');                  //陣列
                        lvi.SubItems.Add(m[0]);     //                                                      //開始時間       
                        lvi.SubItems.Add(m[1]);  //結束時間
                        lvi.SubItems.Add(m[3]);
                        lvi.SubItems.Add(m[2]); 
 
                    }
                }
            }
           // button1.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.Selected)
                {
                   read(item.SubItems[4].Text);     
                }
              
            }
          
        }

        private static void read(string str)
        {
            string readstring = "Const SSFMCreateForWrite = 3\n";
            //readstring+="strText =" +"\""姓名:郭佔慧,簽到時間:8點10分12秒,狀態為:遲到;姓名:吳超,簽到時間:8點10分12秒,狀態為:遲到;這兩個人是兩口子"\""+"\n";
            readstring += "strText=" + "\"" + str + "\"" + "\n";
            readstring += "Set objVoice = CreateObject(" + "\"SAPI.SpVoice\"" + ")\n";
            readstring += "objvoice.Speak strText\n";
            //readstring += "Set objFile = CreateObject(" + "\"SAPI.SpFileStream.1\"" + ")\n";
            //readstring += "objFile.Open " + "\"D:\\Test.wav\"" + "," + "SSFMCreateForWrite\n";
            //readstring += "Set objVoice.AudioOutputStream = objFile\n";
            //readstring += "objVoice.Speak strText\n";

            string path = @"D:";
            StreamWriter sw = new StreamWriter(path + "\\" + "1.vbs", false, Encoding.Unicode);//GetEncoding("gb2312 "));
            sw.WriteLine(readstring);
            sw.Close();
            sw.Dispose();
            System.Diagnostics.Process.Start(@"d:\1.vbs");
        }
        bool mn = true;
        private void music_SelectedIndexChanged(object sender, EventArgs e)
        {
            playmusic();   //呼叫播放音樂
                                                                                                                                                                                                                                                                                 
        }

        private void playmusic()   //這裡是播放音樂
        {
            string path = Application.StartupPath;
            path = path.Replace("bin\\Debug", "Resources\\midi\\");
            if (music.Text != "")
                path = path + music.Text + ".mid";
            this.axWindowsMediaPlayer1.URL = path;
            this.axWindowsMediaPlayer1.Ctlcontrols.play();
            axWindowsMediaPlayer1.settings.setMode("loop", true);  //迴圈播放

        }

        private void button6_Click(object sender, EventArgs e)
        {
            mn = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string str = "";
            deleterecord(str);                //清除當前啟用的這條記錄
            
        }
        string msg = "";
        private string deleterecord(string str)
        {
            string b = "no";
            ListViewItem lv = new ListViewItem(str);
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.SubItems[2].Text == str)
                {
                     b = "yes";
                     msg = item.SubItems[4].Text;
                     if (ip)
                     {
                         read0(msg);
                     }
                     else {
                         System.Diagnostics.Process.Start(@"d:\1.vbs");
                     
                     }

                }
            }
            return b;
        }
        bool ip = true;
        private void read0(string str)
        {
            string readstring = "Const SSFMCreateForWrite = 3\n";
            //readstring+="strText =" +"\""姓名:郭佔慧,簽到時間:8點10分12秒,狀態為:遲到;姓名:吳超,簽到時間:8點10分12秒,狀態為:遲到;這兩個人是兩口子"\""+"\n";
            readstring += "strText=" + "\"" + str + "\"" + "\n";
            readstring += "Set objVoice = CreateObject(" + "\"SAPI.SpVoice\"" + ")\n";
            readstring += "objvoice.Speak strText\n";
            readstring += "Set objFile = CreateObject(" + "\"SAPI.SpFileStream.1\"" + ")\n";
            readstring += "objFile.Open " + "\"D:\\21.wav\"" + "," + "SSFMCreateForWrite\n";
            readstring += "Set objVoice.AudioOutputStream = objFile\n";
            readstring += "objVoice.Speak strText\n";

            string path = @"D:";
            StreamWriter sw = new StreamWriter(path + "\\" + "1.vbs", false, Encoding.Unicode);//GetEncoding("gb2312 "));
            sw.WriteLine(readstring);
            sw.Close();
            sw.Dispose();
            ip = false;
        }
        

    }
}

 

相關文章