#region 第一部分界面设计
const int N = ;
Button button = new Button();
Image[] images = new Image[N];
PictureBox[] pictures = new PictureBox[N];
int[] dx = new int[N] { , , , , -, -, , };
int[] dy = new int[N] { , , , , , , -, - };
private void Form1_Load(object sender, EventArgs e)
{
InitialImage();
InitialSurface();//初始化窗体界面
}
//添加图片
private void InitialImage()
{
images[] = Resources.保温杯;
images[] = Resources.体重秤;
images[] = Resources.x1;
images[] = Resources.台灯;
images[] = Resources.耳机;
images[] = Resources.x2;
images[] = Resources.电动牙刷;
images[] = Resources.时钟;
this.BackgroundImage = Resources.formbg;
}
//初始化界面
private void InitialSurface()
{
Panel pa = new Panel();
pa.Size = new System.Drawing.Size(, );
pa.BackgroundImage = Resources.bga1;
pa.BackgroundImageLayout = ImageLayout.Stretch;
pa.Location = new Point(, );
pa.BackColor = Color.Transparent;
pa.TabIndex = ;
this.Controls.Add(pa);
int x = , y = ;
for (int i = ; i < N; i++)
{
x += dx[i];
y += dy[i];
PictureBox box = new PictureBox();
box.Size = new Size(, );
box.Location = new Point(x, y);
box.Image = images[i];
box.SizeMode = PictureBoxSizeMode.StretchImage;
box.BorderStyle = BorderStyle.FixedSingle;
box.Padding = new Padding();
box.BackColor = Color.White;
pictures[i] = box;
pa.Controls.Add(box);
}
button.Text = "开始" + "\r\n" + "抽奖";
button.Font = new Font("宋体", , FontStyle.Bold);
button.Size = new Size(, );
button.Location = new Point(, );
button.Click += Button_Click;
pa.Controls.Add(button); PictureBox box1 = new PictureBox();
box1.Image = Resources.bgGIF;
box1.SizeMode = PictureBoxSizeMode.StretchImage;
box1.BorderStyle = BorderStyle.None;
box1.Size = new Size(, );
box1.Location = new Point(, );
this.Controls.Add(box1);
this.MaximizeBox = false;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
}
#endregion

第一部分界面设计

界面效果如下:

实现旋转是使用的改变控件大小,背景颜色(把padding设置4可以看见背景颜色)。

 Random random = new Random();
SoundPlayer sp = new SoundPlayer(Resources.Untitled_Project);
int circle;
int index;
int stopNum;
bool flag = true;//奇数和偶数的标志,为true表示偶数
private void Button_Click(object sender, EventArgs e)
{
circle = random.Next(, );//随机产生旋转的圈数
stopNum = random.Next(, );//随机确定选择抽中奖品的下标
button.Enabled = false;
time.Interval = ;
index = ;
flag = true;
sp.Play();
time.Start();
} private void time_Tick(object sender, EventArgs e)
{
if (flag)//放大picturebox,实现转动效果
{
pictures[index].Size = new Size(, );
pictures[index].BackColor = Color.YellowGreen;
flag = false;
}
else if (flag == false)//将原来的picturebox缩放回原来的尺寸
{
pictures[index].Size = new Size(, );
pictures[index].BackColor = Color.White;
index++;
flag = true;
}
if (index == )
{
index = ;
circle -= circle > ? : ;//每次转动8次,旋转的圈数-1
}
//倒数两圈慢
time.Interval = circle > ? : ;
if (circle == && index == stopNum)
{
pictures[index].Size = new Size(, );
pictures[index].BackColor = Color.YellowGreen;
pictures[index].Location = new Point(pictures[index].Location.X - , pictures[index].Location.Y - );
StopRotate(index);
pictures[index].Size = new Size(, );
pictures[index].BackColor = Color.White;
pictures[index].Location = new Point(pictures[index].Location.X + , pictures[index].Location.Y + );
}
}

旋转代码

停止再加上一个简单的概率会有点不了不流畅。

  private void StopRotate(int sum)
{
string message = null;
switch (index)
{
case : if (random.Next(, ) == stopNum) message = "恭喜获取保温杯一个"; else { circle = ; stopNum = ; } break;//1/2 14 7%
case : if (random.Next(, ) == stopNum) message = "恭喜获取体重秤一个"; else { circle = ; stopNum = ; } break;//1/3 21 4%
case : message = "谢谢惠顾"; break;
case : if (random.Next(, ) == stopNum) message = "恭喜获取台灯一个"; else { circle = ; stopNum = ; } break;//1/4 28 3%
case : if (random.Next(, ) == stopNum) message = "恭喜获取耳机一个"; else { circle = ; stopNum = ; } break;//1/4 28 3%
case : message = "谢谢惠顾"; break;
case : if (random.Next(, ) == stopNum) message = "恭喜获取电动牙刷一个"; else { circle = ; stopNum = ; } break;//1/2 1/14 7&
case : if (random.Next(, ) == stopNum) message = "恭喜获取时钟一个"; else { circle = ; stopNum = ; } break;//1/3 1/21 4%
}
if (message != null)
{
sp.Stop();
time.Stop();//先关闭定时器
MessageBox.Show(message);
this.button.Enabled = true;
}
}

停止代码

简单的旋转程序就完成了。

c#自制抽奖小程序的更多相关文章

  1. 用jquery实现抽奖小程序

    用jquery实现抽奖小程序 这些日子,到处都可以看到关于微信小程序的新闻或报到,在博客园中写关于微信小程序的也不少.但是今天我要说的不是微信小程序,而是用简单的jquery写的一个好玩的抽奖小程序. ...

  2. 用 python 写一个年会抽奖小程序

    使用 pyinstaller 打包工具常用参数指南 pyinstaller -F demo.py 参数 含义 -F 指定打包后只生成一个exe格式的文件 -D –onedir 创建一个目录,包含exe ...

  3. 抽奖小程序,js,canvas

    js写的网页抽奖小程序,先上截图 源码地址:https://github.com/xiachaoxulu/raffle

  4. Winform 随机抽奖小程序

    效果图: 主要代码: Form1.cs using System; using System.Drawing; using System.IO; using System.Runtime.Intero ...

  5. Excel VBA活动抽奖小程序

    在活动中,我们常会有抽奖,抽奖箱准备繁琐,现在多采用线上抽奖方式,下面用Excel VBA写了一个简单的抽奖小程序 简单测试效果如下,可实现: 多次抽奖,且每次抽奖都不重复 抽奖界面滚动人员信息,点击 ...

  6. 自制微信小程序 提示插件 -- noticeUitis.js

    /* noticeMsg.js by: FEer_llx Modify 2016/08/24 */ function weNotice(obj) { this.fadeFlag = true; thi ...

  7. Java抽奖小程序

    package com.test; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; im ...

  8. python——公司年会抽奖小程序

    张三科技有限公司有300名员工,开年会抽奖,奖项如下一等奖3名 : 泰国五日游二等奖6名 :iphone手机三等奖30名 :避孕套一盒规则:1.一共抽3次,第一次抽3等奖,第二次抽2等奖,第三次压轴抽 ...

  9. 基于vs2012的C# winform抽奖小程序的总结

    哈希表的使用 Hashtable hashtable = new Hashtable(); hashtable.ContainsValue(tmp);//判断哈希表中有没有tmp hashtable. ...

随机推荐

  1. Js 之正则验证手机号、QQ、身份证等

    // 常见的 正则表达式 校验 // QQ号.手机号.Email.是否是数字.去掉前后空格.是否存在中文.邮编.身份证.URL.日期格式.IP let myRegExp = { // 检查字符串是否为 ...

  2. Wireshark常用快捷键&&过滤器语法

    目录 一.快捷键 二.过滤器语法 1.捕获过滤器的BPF(Berkeley Packet Filter)语法 2.显示过滤器 一.快捷键 Ctrl+M 标记/取消标记 shift+ctrl+N/B 下 ...

  3. [Beta]第六次 Scrum Meeting

    [Beta]第六次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/14 22:00 30min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 (两人回 ...

  4. Cesium的Property机制总结[转]

    https://www.jianshu.com/p/f0b47997224c 前言 Cesium官方教程中有一篇叫<空间数据可视化>(Visualizing Spatial Data).该 ...

  5. http statusCode(状态码)请求URL返回状态值的含义

    http statusCode(状态码)请求URL返回状态值的含义 请求URL浏览器返回状态码的含义(http statusCode): 201-206都表示服务器成功处理了请求的状态代码,说明网页可 ...

  6. 如何开发一个npm包并发布到npm中央仓库

    转自: https://liaolongdong.com/2019/01/24/publish-public-npm.html 如何开发一个npm包并发布到npm中央仓库需求背景:平时在项目工作中可能 ...

  7. 调用百度API接口 正解析地址和逆解析

    地址解析(结构化地址 解析得到 经纬度): public void SaveLocation(DataRequest<Location> request, DataResponse< ...

  8. Spring整合Redis,并配置Jedis连接池

    目录 只言片语 创建redis连接池的配置文件 单机版 spring整合redis(使用JedisPool) 项目中使用示例 集群版 spring整合redis(使用JedisCluster) 项目中 ...

  9. windows系统下同时安装mysql5.5和8.0.11

    前提:电脑已安装5.5,增安装8.0.11 zip版本 1.官网下载mysql10.8.0.11 —找到mysql community server 为下载页面URL:https://dev.mysq ...

  10. SAP 更新模块1

    RSM13000 / RSM13000 / 5.747FORM / VB_CALL_FUNC CALL 'ThVBCall' ID 'OPCODE' FIELD vb_update_modul_pro ...