#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. 小技巧--解决eclipse导入的jar文件后,无法使用默认包中的方法问题

    问题:我已经导入了stdlib的jar文件,但是由于包是(default package)所以无法使用包中的方法 解决方法: 1.新建一个项目 2.新建一个文件夹 3.打开项目,新建一个包,然后导入j ...

  2. windows 共享网络

    windows 共享网络 今天单位的网络突然断了,光猫LOS亮红灯,宽带报修.等了半天还没来,下面科室要上报资料,急着用网, 通过windows的共享网络+无线网卡暂时用我的手机流量. 找了一台电脑插 ...

  3. NIO Channel 管道

    Java NIO的通道类似流,但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先读到一个Buffer,或者总是要从一个Bu ...

  4. 刷题记录:Shrine

    目录 刷题记录:Shrine 刷题记录:Shrine 题目复现链接:https://buuoj.cn/challenges 参考链接:Shrine 解此题总结一下flask的SSTI:CTF SSTI ...

  5. Real-time Multiple People Tracking with Deeply Learned Candidate Selection and Person Re-identification

    Real-time Multiple People Tracking with Deeply Learned Candidate Selection and Person Re-identificat ...

  6. springcloud添加自定义的endpoint来实现平滑发布

    在我之前的文章  springcloud如何实现服务的平滑发布 里介绍了基于pause的发布方案. 平滑发布的核心思想就是:所有服务的调用者不再调用该服务了就表示安全的将服务kill掉. 另外actu ...

  7. Java基础 switch 简单示例

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  8. Python3基础 yield send 获得生成器后,需要先启动一次

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  9. ROS tf-增加坐标系

    博客参考:https://www.ncnynl.com/archives/201702/1312.html ROS与C++入门教程-tf-增加坐标系 说明: 介绍如何为TF增加额外固定的坐标系 为何增 ...

  10. redis-查看日志

    转: redis-查看日志 redis在默认情况下,是不会生成日志文件的,所以需要配置 配置方法: 1.首先找到redis的配置文件 2.打开配置文件,找到logfile(可能有多个logfile,认 ...