9/26第二週 製作紅綠燈+圖片+音樂
紅綠燈
首先,先進入表單 在表單程式輸入 int c = 0 , r ;
再來利用工具箱建立四個button與一個label
進入button2輸入開關所需條件
時間器Timer
條件輸入完後在表單上建立一時間器Timer
進入Timer將剛才的程式複製進去
回到表單點選Timer的屬性視窗
將Enabled打開,另外,Interval一秒為1000單位
圖片
選擇button1屬性的Image功能,點選本機資源-->匯入,並選擇想要的圖片
按下確定即完成圖片設定
音樂
Ex:
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
int c = 0, r;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;
label1.Text = "r=" + r;
if (r == 0)
{
button1.Location = new Point(10, 30);
button2.BackColor = Color.Red;
button3.BackColor = Color.Black;
button4.BackColor = Color.Black;
}
else if (r == 1)
{
button1.Location = new Point(0, 30);
button2.BackColor = Color.Black;
button3.BackColor = Color.Yellow;
button4.BackColor = Color.Black;
}
else
{
button1.Location = new Point(0, 0);
button2.BackColor = Color.Black;
button3.BackColor = Color.Black;
button4.BackColor = Color.Green;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
c = c + 1;
r = c % 3;
label1.Text = "r=" + r;
if (r == 0)
{
button1.Location = new Point(10, 30);
button2.BackColor = Color.Red;
button3.BackColor = Color.Black;
button4.BackColor = Color.Black;
}
else if (r == 1)
{
button1.Location = new Point(0, 30);
button2.BackColor = Color.Black;
button3.BackColor = Color.Yellow;
button4.BackColor = Color.Black;
}
else
{
button1.Location = new Point(0, 0);
button2.BackColor = Color.Black;
button3.BackColor = Color.Black;
button4.BackColor = Color.Green;
}
}
}
}
沒有留言:
張貼留言