Winform 随机抽奖小程序
效果图:
主要代码:
Form1.cs

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
static string path = "抽奖.txt";
string[] content = File.ReadAllLines(path);
public Form1()
{
InitializeComponent();
//控制名称变化时闪烁问题
this.DoubleBuffered = true;//设置本窗体
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
}
private void Form1_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(Form1_Resize);
X = this.Width;
Y = this.Height;
setTag(this);
Form1_Resize(new object(), new EventArgs());
loadBackImage();
//this.timer1.Interval = 2000;
//设置按钮背景透明、无边框
this.btn_BeginEnd.BackColor = Color.Transparent;
this.btn_BeginEnd.FlatStyle = FlatStyle.Flat;
this.btn_BeginEnd.FlatAppearance.BorderSize = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
Random r = new Random();
int i = r.Next(0, content.Length);
label1.Text = content[i];
}
catch (Exception ex)
{
timer1.Stop();
MessageBox.Show(ex.Message);
btn_BeginEnd.Text = "开始";
return;
}
}
private void btn_BeginEnd_Click(object sender, EventArgs e)
{
if (btn_BeginEnd.Text == "开始")
{
timer1.Start();
btn_BeginEnd.Text = "抽奖中...";
}
else
{
timer1.Stop();
btn_BeginEnd.Text = "开始";
}
}
/// <summary>
/// 背景图自适应窗体
/// </summary>
private void loadBackImage()
{
string fbImage = "背景.jpg";
var bm = new Bitmap(fbImage); //fbImage图片路径
this.BackgroundImage = bm;//设置背景图片
this.BackgroundImageLayout = ImageLayout.Stretch;//设置背景图片自动适应
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
loadBackImage();
}
#region 控件跟随窗体自适应大小
private float X;
private float Y;
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
{
setTag(con);
}
}
}
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
//双缓冲,控制窗体改变大小时闪烁问题
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0xB;
private void Form1_Resize(object sender, EventArgs e)
{
SendMessage(this.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
float newx = (this.Width) / X;
float newy = this.Height / Y;
setControls(newx, newy, this);
SendMessage(this.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
this.Invalidate(true);
}
/// <summary>
/// 设置label自适应窗体居中显示
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int x = (int)(0.5 * (this.Width - label1.Width));
int y = label1.Location.Y;
label1.Location = new System.Drawing.Point(x, y);
}
#endregion
#region 监听鼠标状态,改变按钮样式
private void btn_BeginEnd_MouseLeave(object sender, EventArgs e)
{
this.btn_BeginEnd.ForeColor = Color.White;
this.btn_BeginEnd.Font = new Font(btn_BeginEnd.Font, FontStyle.Regular);
}
private void btn_BeginEnd_MouseMove(object sender, MouseEventArgs e)
{
this.btn_BeginEnd.FlatAppearance.MouseOverBackColor = Color.Transparent;
this.btn_BeginEnd.Font = new Font(btn_BeginEnd.Font, FontStyle.Bold);
}
private void btn_BeginEnd_MouseClick(object sender, MouseEventArgs e)
{
this.btn_BeginEnd.FlatAppearance.MouseDownBackColor = Color.Transparent;
}
private void btn_BeginEnd_MouseDown(object sender, MouseEventArgs e)
{
this.btn_BeginEnd.FlatAppearance.MouseDownBackColor = Color.Transparent;
}
#endregion
}
}
修改为可作弊的抽奖小程序:

//修改代码:
private void btn_BeginEnd_Click(object sender, EventArgs e)
{
if (btn_BeginEnd.Text == "开始")
{
timer1.Start();
btn_BeginEnd.Text = "抽奖中...";
//this.txt_MaxValue.Enabled = false;
}
else
{
//timer1.Stop();
//btn_BeginEnd.Text = "开始";
//this.txt_MaxValue.Enabled = true;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.D1 || keyData == Keys.NumPad1)
{
timer1.Stop();
label1.Text = ConfigurationManager.AppSettings["First"];
btn_BeginEnd.Text = "开始";
}
if (keyData == Keys.D2 || keyData == Keys.NumPad2)
{
timer1.Stop();
label1.Text = ConfigurationManager.AppSettings["Second"];
btn_BeginEnd.Text = "开始";
}
if (keyData == Keys.D3 || keyData == Keys.NumPad3)
{
timer1.Stop();
label1.Text = ConfigurationManager.AppSettings["Third"];
btn_BeginEnd.Text = "开始";
}
return true;
}
txt内容:
App.config内容:
程序链接:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!