1、工程建立

(1)新建一个C#窗体工程:跑马灯,将工具箱中的“timer”控件添加到FORM窗体上,更改“timer"控件的属性,将Enabled:false改为True;Interval:表示运行事件的频率,以毫秒为单位。1000毫秒=1秒。

C#实现跑马灯实例---timer控件C#实现跑马灯实例---timer控件

(2)为“timer”控件添加Tick事件,编辑Form1.cs代码:

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;

namespace 跑马灯
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            MessageBox.Show("你中病毒了");	//每隔一秒弹出一个对话框
        }
    }
}

运行效果:

C#实现跑马灯实例---timer控件

(3)在FORM1窗体上添加label控件,其Text改为“abcde”

C#实现跑马灯实例---timer控件

编辑Form1.cs代码:

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;

namespace 跑马灯
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //abcde-->bcdea--->cdeab--->deabc--->eabcd--->abcde
            label1.Text=label1.Text.Substring(1) + label1.Text.Substring(0, 1);	//每隔1秒变换一次
        }
    }
}

运行结果:

C#实现跑马灯实例---timer控件C#实现跑马灯实例---timer控件

(4)将label的Text改为“★☆★☆★☆★☆★☆”

C#实现跑马灯实例---timer控件

编辑Form1.cs代码:

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;

namespace 跑马灯
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //★☆★☆★☆★☆★☆
            label1.Text=label1.Text.Substring(1) + label1.Text.Substring(0, 1);	//每隔1秒变换一次
        }
    }
}
运行结果

C#实现跑马灯实例---timer控件C#实现跑马灯实例---timer控件


闹钟程序实现

1、新建一个FORM窗体,窗体上添加一个label控件,其(Name)改为lb_timer;将工具箱中的“timer”控件添加到FORM窗体上,更改“timer"控件的属性,将Enabled:false改为True;Interval:1000。

2、为窗体FORM1添加Load事件,引用SoundPlayer播放音乐。

编辑Form1.cs代码:

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.Media; //音乐命名空间
namespace 闹钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //每隔一秒钟就把当前的时间赋值给label1
        private void timer1_Tick(object sender, EventArgs e)
        {
            lb_timer.Text = DateTime.Now.ToString();
            //20:51分播放音乐
            if (DateTime.Now.Hour==20 &&DateTime.Now.Minute==51)
            {
                //播放音乐
                SoundPlayer sp = new SoundPlayer();
                sp.SoundLocation = @"F:\1.wav";
                sp.Play();

            }
        }

        //窗体加载的时候,将当前系统的时间赋值给label
        private void Form1_Load(object sender, EventArgs e)
        {
            lb_timer.Text = DateTime.Now.ToString();

        }
    }
}

运行结果:

C#实现跑马灯实例---timer控件

到时间就播放音乐。

SoundPlayer 只能播放“.wav”格式的音乐文件

相关文章: