using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace LotteryApplication
{ public partial class MainForm : Form
{ private readonly List<string> participants = new List<string>
{ "张三", "李四", "王五", "赵六", "陈七",
"林八", "黄九", "周十", "吴十一", "郑十二"
};
private readonly Random random = new Random();
private bool isDrawing = false;
public MainForm()
{ InitializeComponent();
InitializeUI();
}
private void InitializeUI()
{ // 窗体设置
this.Text = "电子抽奖系统";
this.Size = new Size(600, 400);
this.BackColor = Color.WhiteSmoke;
// 抽奖显示标签
var lblResult = new Label
{ Font = new Font("微软雅黑", 24, FontStyle.Bold), ForeColor = Color.DarkSlateBlue,
Size = new Size(500, 100),
Location = new Point(50, 50),
TextAlign = ContentAlignment.MiddleCenter
};
// 控制按钮
var btnStart = new Button
{ Text = "开始抽奖",
Font = new Font("微软雅黑", 12), Size = new Size(120, 40),
Location = new Point(150, 200),
BackColor = Color.LimeGreen
};
var btnStop = new Button
{ Text = "停止",
Font = new Font("微软雅黑", 12), Size = new Size(120, 40),
Location = new Point(330, 200),
BackColor = Color.OrangeRed,
Enabled = false
};
// 定时器设置
var timer = new Timer { Interval = 50 };
// 事件绑定
btnStart.Click += (s, e) =>
{ isDrawing = true;
btnStart.Enabled = false;
btnStop.Enabled = true;
timer.Start();
};
btnStop.Click += (s, e) =>
{ isDrawing = false;
btnStart.Enabled = true;
btnStop.Enabled = false;
timer.Stop();
lblResult.ForeColor = Color.DarkRed;
};
timer.Tick += (s, e) =>
{ if (isDrawing)
{ int index = random.Next(participants.Count);
lblResult.Text = participants[index];
}
};
// 添加控件
this.Controls.Add(lblResult);
this.Controls.Add(btnStart);
this.Controls.Add(btnStop);
}
}
}