1、抽奖法式结果示范
运行法式后,窗体显现抽奖名单、抽奖成果标签、开端按钮战中断按钮。
面打“开端抽奖”按钮,标签会显现不竭变革的随机名字,模仿抽奖动绘。
面打“中断抽奖”按钮,抽奖中断,并显现终极成果。
2、用DeepSeek写一个WinForm抽奖法式
将天生的代码拷贝到Visual Studio 2022中
3、DeepSeek代码完毕using System;using System.Collections.Generic;using System.Drawing;using System.Threading;using System.Windows.Forms;
namespace LotteryAppCodeOnly{ public class LotteryForm : Form { private ListBox listBoxCandidates; // 抽奖名单 private Label labelResult; // 显现抽奖成果 private Button buttonStart; // 开端抽奖按钮 private Button buttonStop; // 中断抽奖按钮
private bool isRunning = false; // 抽奖形状 private Random random = new Random(); // 随机数天生器 private Thread lotteryThread; // 抽奖线程 private List<string> candidates = new List<string>(); // 抽奖名单
public LotteryForm() { // 树立窗体属性 this.Text = "抽奖法式"; this.Size = new Size(400, 300); this.FormClosing += LotteryForm_FormClosing;
// 初初化抽奖名单 InitializeCandidates();
//静态 创立控件 CreateControls(); }
// 初初化抽奖名单 private void InitializeCandidates() { candidates.Add("弛三"); candidates.Add("李四"); candidates.Add("王五"); candidates.Add("赵六"); candidates.Add("孙七"); candidates.Add("周八"); candidates.Add("吴九"); candidates.Add("郑十"); }
//静态 创立控件 private void CreateControls() { //创立 ListBox控件 listBoxCandidates = new ListBox { Location = new Point(20, 20), Size = new Size(150, 200), DataSource = candidates // 绑定抽奖名单 }; this.Controls.Add(listBoxCandidates);
//创立 Label控件 labelResult = new Label { Location = new Point(200, 20), Size = new Size(150, 30), Text = "等候抽奖...", BorderStyle = BorderStyle.FixedSingle, TextAlign = ContentAlignment.MiddleCenter }; this.Controls.Add(labelResult);
//创立 开端按钮 buttonStart = new Button { Location = new Point(200, 70), Size = new Size(100, 30), Text = "开端抽奖" }; buttonStart.Click += ButtonStart_Click; this.Controls.Add(buttonStart);
//创立 中断按钮 buttonStop = new Button { Location = new Point(200, 120), Size = new Size(100, 30), Text = "中断抽奖", Enabled = false // 初初时不成用 }; buttonStop.Click += ButtonStop_Click; this.Controls.Add(buttonStop); }
// 开端抽奖按钮面打工作 private void ButtonStart_Click(object sender, EventArgs e) { if (!isRunning) { isRunning = true; buttonStart.Enabled = false; buttonStop.Enabled = true;
// 启用抽奖线程 lotteryThread = new Thread(StartLottery); lotteryThread.Start(); } }
// 中断抽奖按钮面打工作 private void ButtonStop_Click(object sender, EventArgs e) { if (isRunning) { isRunning = false; buttonStart.Enabled = true; buttonStop.Enabled = false; } }
// 抽奖逻辑 private void StartLottery() { while (isRunning) { // 随机挑选一个名字 int index = random.Next(candidates.Count); string selectedName = candidates[index];
// 革新UI this.Invoke((MethodInvoker)delegate { labelResult.Text = selectedName; });
// 停息一段时间以模仿抽奖动绘 Thread.Sleep(100); }
// 抽奖完毕后显现终极成果 this.Invoke((MethodInvoker)delegate { labelResult.Text = "祝贺中奖者: " + labelResult.Text; }); }
// 窗体封闭时保证线程停止 private void LotteryForm_FormClosing(object sender, FormClosingEventArgs e) { if (isRunning) { isRunning = false; lotteryThread.Join(); } }
//顺序 进口 [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new LotteryForm()); } }}
4、代码分析
控件静态创立:
使用new枢纽字静态创立ListBox、Label、Button等控件。
树立控件的职位、巨细、文原等属性。
使用Controls.Add办法将控件增加到窗体中。
抽奖逻辑:
使用Random类从抽奖名单中随机挑选一个名字。
使用Thread类创立一个新线程去施行抽奖逻辑,制止壅闭UI线程。
使用Invoke办法正在UI线程上革新控件。
窗体封闭处置:
法式进口:
5、扩大功用
静态增加名单:能够增加一个TextBox战Button,许可用户静态增加抽奖名单。
音效撑持:正在抽奖过程当中播搁音效,增加爱好性。
多奖项撑持:撑持抽与多个奖项(如一等奖、两等奖等)。
那是一个完整颠末代码完毕的WinForm抽奖法式,适宜进修战扩大。假设有所有成就,欢送持续提问!
以为有收获 ?无妨分享让更多的支益
存眷【胡师长教师的代码工坊】 |