Home [C# 筆記][WinForm] Timer
Post
Cancel

[C# 筆記][WinForm] Timer

Timer

timer: 在指定的時間間隔內做一件指定的事
Interval:1000毫秒=1秒
timer1.Enabled = true;

跑馬指效果

原理:截取字串,將第一個字元放到最後一個

label1.Text = “☆★☆★☆★☆★☆★☆★☆★☆★☆★”;
timer1.Enabled = true;

1
2
3
4
5
6
private void timer1_Tick(object sender, EventArgs e)
{
    //abcde:截取bcde+a製造跑馬燈效果
    //原理:截取字串,將第一個字元放到最後一個
    label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1);
}

鬧鐘小程式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <summary>
/// 當視窗載入時,將當前系統的時間賦值給label
/// </summary>
private void Form1_Load(object sender, EventArgs e) {
    lblDateTime.Text = DateTime.Now.ToString();
}
/// <summary>
/// 每隔一秒鐘就把當前的時間賦值給label
/// </summary>
private void timer2_Tick(object sender, EventArgs e)
{
    lblDateTime.Text = DateTime.Now.ToString();
    //15:32播放音樂叫我起床
    if (DateTime.Now.Hour == 15 && DateTime.Now.Minute == 32 && DateTime.Now.Second == 50)
    {
        //播放音樂
        SoundPlayer sp = new SoundPlayer();
        sp.SoundLocation = @"C:\Users\rivalin\Desktop\hi.wav";
        sp.Play();
    }
}

每隔一秒鐘換一張照片

取得文件裡的每一個文件的完整路徑Directory.GetFiles()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private void Form1_Load(object sender, EventArgs e)
{
    //播放音樂
    SoundPlayer sp = new SoundPlayer();
    sp.SoundLocation = @"C:\Users\rivalin\Desktop\hi.wav";
    sp.Play();

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage;
    //視窗載入時,給每一個pictureBox賦值圖片路徑
    pictureBox1.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
    pictureBox2.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
    pictureBox3.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
    pictureBox4.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
    pictureBox5.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
    pictureBox6.Image = Image.FromFile(@"C:\Users\rivalin\Desktop\images\1.jpg");
}

//取得文件裡的每一個文件的完整路徑
string[] path = Directory.GetFiles(@"C:\Users\rivalin\Desktop\images\");
int i = 0; //記錄path的index
Random r = new Random();//建立產生隨機數物件
private void timer1_Tick(object sender, EventArgs e)
{
    //每一秒鐘 換一張圖片
    i++;
    if (i == path.Length) i = 0;
    pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]);//(path[i]);
    pictureBox2.Image = Image.FromFile(path[r.Next(0, path.Length)]);
    pictureBox3.Image = Image.FromFile(path[r.Next(0, path.Length)]);
    pictureBox4.Image = Image.FromFile(path[r.Next(0, path.Length)]);
    pictureBox5.Image = Image.FromFile(path[r.Next(0, path.Length)]);
    pictureBox6.Image = Image.FromFile(path[r.Next(0, path.Length)]);
}
This post is licensed under CC BY 4.0 by the author.