重庆海天软件工程学院的WINFORM实训项目-抽奖系统

最近班上写了一个关于winform的实训小项目(抽奖系统),90%的代码都在下面,感觉还不错,所以贴出来分享一下,希望能帮倒大家

所有界面如下:

这是自己写的一个公共类:

View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WindowsApplication1
{
    class NumberManage
    {
        public static int K = 1;//自保留参数,用来实现特殊功能
     //幸运奖号码文件保存位置
     private static string lukyNum = "c:\\luky.txt";        
     public static string LUKYNUM 
        {
            get { return NumberManage.lukyNum; }           
        }
        //一,二,三等奖号码文件保存位置
        private static string wondofulNum = "c:\\wondful.txt";
        public static string WONDOFULNum
        {
            get { return NumberManage.wondofulNum; }
            set { NumberManage.wondofulNum = value; }
        }
        /// <summary>
        /// 输出一个三位数的数字,不足三位前面补零
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string FormatNum(int a)
        {
            if (a >= 1000 || a < 0)
            {
                return "000";
            }
            if (a >= 100)
            {
                return a.ToString();
            }
            else if (a >= 10 && a < 100)
            {
                return "0" + a;
            }
            else
            {
                return "00" + a;
            }
        }

        /// <summary>
        /// 在指定范围内随机获得一个不重复的幸运数,此数将会被保存在lukyNum路径处
     /// </summary>
        /// <returns></returns>
        public static int GetLukyNumber()
        {
            string numstr;//此变量用来存要写入文件中的值

       if (!File.Exists(lukyNum))
            {
                //创建一个文本文件之后马上关闭流
                File.CreateText(lukyNum).Close();
       }

            StreamReader sr = new StreamReader(lukyNum);
            string ln = sr.ReadToEnd(); 
            sr.Close();

            Random r = new Random();
            int temp = r.Next(1, 481);


            if (ln == "")
            {
                numstr = temp.ToString();
            }
            else
            {
                string[] num = ln.Split(',');
                //当文件中已经存在480个不同的数时,则返回-1,表示无数可取
                if (num.Length > 480)
                    return 0;

                List<string> list = new List<string>(num);
//利用List的Contains方法判断一个值是否存在,不知道数组是否有类似的方法?
                while (list.Contains(temp.ToString()))
         {
                    temp = r.Next(1, 481);
                }
                numstr = "," + temp;//非文件中第一个数时,要用","隔开
            }

            StreamWriter sw = new StreamWriter(lukyNum, true);
            sw.Write(numstr);
            sw.Close();
            return temp;
        }

        /// <summary>
        /// 在指定范围内随机获得一个不重复的数,此数将会被保存在wondofulNum路径处,这是用来获得一,二,三等奖的号码
     /// </summary>
        /// <returns></returns>
        public static int GetWondfulNumber(int level)
        {
            string numstr;//此变量用来存要写入文件中的值           
       if (!File.Exists(wondofulNum))
            {
                //创建一个文本文件之后马上关闭流
                File.CreateText(wondofulNum).Close();
       }
            StreamReader sr = new StreamReader(wondofulNum);
            string ln = sr.ReadToEnd();
            sr.Close();

            Random r = new Random();
            int temp = r.Next(1, 481);
           
       if (ln == "")
            {
                numstr = temp.ToString();
            }
            else
            {
                string[] num = ln.Split(',');
                //当文件中已经存在480个不同的数时,则返回-1,表示无数可取
                if (num.Length > 480)
                    return 0;
                List<string> list = new List<string>(num);
               //利用List的Contains方法判断一个值是否存在
                while (list.Contains(temp.ToString()))
         {
                    temp = r.Next(1, 481);
                }
                numstr = "," + temp;//非文件中第一个数时,要用","隔开
       }

            StreamWriter sw = new StreamWriter(wondofulNum, true);
            sw.Write(numstr);
            sw.Close();
            return temp;
        }

        /// <summary>
        /// 分解数字, 获得num这个数wei位上的数字,暂时简单取一个数的个位,十位和百位,其他以后完善
        /// </summary>
        /// <param name="num">要操作的数</param>
        /// <param name="wei">要取的位数</param>
        /// <returns></returns>
        public static int AnalyzeNum(int num, int wei)
        {
            switch (wei)
            {
                case 1://个位
                    break;                   
                case 2://十位
                    num = num / 10;
                    break;
                case 3://百位
                    num = num / 100;
                    break;
            }
            return num % 10;
        }

    }
}

大图片的后台代码(用户控件):

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class ScrollNumBig : UserControl
    {
        private int num=0;//最后显示的数
        private int degree;//定义滚动次数
        private int maxImgIndex = 9;//要显示的数字最大值(0-maxImgIndex)
        private int step;//滚动的步长

        public int MaxImgIndex
        {
            set { maxImgIndex = value; }
        }
        public int Num
        {
            set { num = value; }
        }
        public ScrollNumBig()
        {
            step = new Random().Next(30, 50);//步长为随机数
            InitializeComponent();
            this.pbNumber.Image = imgNumber.Images[0];//设置图片框的初始图片为0
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pbNumber.Location =new Point(pbNumber.Location.X,pbNumber.Location.Y + step) ;//每次移动step
            if (pbNumber.Location.Y > this.Size.Height)//如果图片全部移出界面,则把图片放到最上面
            {
                pbNumber.Location = new Point(pbNumber.Location.X, -pbNumber.Size.Height);
                degree--;//减少滚动次数
                if (degree < 7)
                {
                    timer1.Interval += 5;//最后6圈开始减速
                }
                if (degree > 1)
                {
                    //换图
                    Random r = new Random();
                    int imgIndex = r.Next(0, maxImgIndex+1);
                    this.pbNumber.Image = imgNumber.Images[imgIndex];
                }
                else
                {
                    this.pbNumber.Image=imgNumber.Images[num];//最后一次显示实际数字
                    timer1.Stop();//停止timer1,timer2开始接管跑最后一圈
                   
                    timer2.Start();
                    timer2.Interval = timer1.Interval;
                }
            }           
        }
        /// <summary>
        /// 图片开始滚动,自动停止
        /// </summary>
        public void Start()
        {
            Random r = new Random();
            
            degree = new Random().Next(50,60);//设置滚动次数(50-60)之间
            timer1.Start();
        }
        public void Stop()
        {
            
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (pbNumber.Location.Y > 0)
            {
                pbNumber.Location = new Point(pbNumber.Location.X, 1);
                timer2.Stop();
            }
            else
            {
                pbNumber.Location = new Point(pbNumber.Location.X, pbNumber.Location.Y + step-8);
                timer2.Interval += 5;
            }
        }
    }
}

小图片的后台代码(用户控件):

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class ScrollNumSmall : UserControl
    {
              private int num=0;//最后显示的数
        private int degree;//定义滚动次数
        private int maxImgIndex = 9;//要显示的数字最大值(0-maxImgIndex)
        private int step;//滚动的步长

        public int MaxImgIndex
        {
            set { maxImgIndex = value; }
        }
        public int Num
        {
            set { num = value; }
        }
        public ScrollNumSmall()
        {
            step = new Random().Next(40, 50);//步长为随机数,让图片滚动不同步
            InitializeComponent();
            this.pbNumber.Image = imgNumber.Images[0];//设置图片框的初始图片为0
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pbNumber.Location =new Point(pbNumber.Location.X,pbNumber.Location.Y + step) ;//每次移动20
            if (pbNumber.Location.Y > this.Size.Height)//如果移到控件处,则把图片放到最上面
            {
                pbNumber.Location = new Point(pbNumber.Location.X, -pbNumber.Size.Height);
                degree--;//减少滚动次数
                if (degree < 7)
                {
                    timer1.Interval += 5;//最后6圈开始减速,移动时间每次多加6ms
                }
                if (degree > 1)
                {
                    //换图
                    Random r = new Random();
                    int imgIndex = r.Next(0, maxImgIndex+1);
                    this.pbNumber.Image = imgNumber.Images[imgIndex];
                }
                else
                {
                    this.pbNumber.Image=imgNumber.Images[num];//最后一次显示实际数字
                    timer1.Stop();//停止timer1,timer2开始接管跑最后一圈
                   
                    timer2.Start();
                    timer2.Interval = timer1.Interval;
                }
            }          
        }
        /// <summary>
        /// 图片开始滚动,自动停止
        /// </summary>
        public void Start()
        {
            Random r = new Random();
            
            degree = new Random().Next(50,60);//设置滚动次数
            timer1.Start();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (pbNumber.Location.Y > 0)
            {
                pbNumber.Location = new Point(pbNumber.Location.X, 1);
                timer2.Stop();
            }
            else
            {
                pbNumber.Location = new Point(pbNumber.Location.X, pbNumber.Location.Y + step-8);
                timer2.Interval += 5;
            }
        }
    }
}

幸运奖的后台代码(用户控件):

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    /// <summary>
    /// 变换的数字
    /// </summary>
    public partial class JupNumber : UserControl
    {
        private int num = 0;//定义最终的数字,默认为0
        private int degree = 80;

        public int Degree
        {
            set { degree = value; }
        }
        public int Num
        {
            set { num = value; }
        }
        private string headNum = "53500";//数字前面固定部位
        public JupNumber()
        {
            degree = new Random().Next(80, 100);
            InitializeComponent();
        }

        /// <summary>
        /// 开始跳动
        /// </summary>
        public void Start()
        {
            timer1.Stop();
            timer1.Start();
            this.Enabled = false;
        }
        /// <summary>
        /// 停止跳动,将显示数字定位在最终数字上
        /// </summary>
        public void Stop()
        {
            timer1.Stop();
            this.lblNumber.Text = headNum + NumberManage.FormatNum(num);
            this.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            lblNumber.Text = headNum + NumberManage.FormatNum(r.Next(1, 481));
            degree--;
            if (degree < 0)
            {
                this.Stop();
            }
        }

        private void lblNumber_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("重摇此数?", "重置提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.num = NumberManage.GetLukyNumber();
                this.Degree = 70;
                this.Start();
            }
        }
    }
}

一等奖窗体的后台代码:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class FirstAwardForm : Form
    {
        public FirstAwardForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GoGoGo();
        }

        /// <summary>
        /// 开始摇奖
        /// </summary>
        private void GoGoGo()
        {
            btnStart.Enabled = false;
            btnRestar.Enabled = true;

            int lukyNum = NumberManage.GetWondfulNumber(1);//得到一个幸运数
            //把幸运数分解出个位,十位和百位
            int one = NumberManage.AnalyzeNum(lukyNum, 1);//个位
            int two = NumberManage.AnalyzeNum(lukyNum, 2);//十位
            int three = NumberManage.AnalyzeNum(lukyNum, 3);//百位

            snFirstNum.Num = one;
            snSecondNum.Num = two;
            snThirdNum.Num = three;
            snThirdNum.MaxImgIndex = 4;//百位上的数最大为4

            snFirstNum.timer1.Interval = 3;
            snSecondNum.timer1.Interval = 5;
            snThirdNum.timer1.Interval = 7;

            snFirstNum.Start();
            snSecondNum.Start();
            snThirdNum.Start();
        }

        private void btnRestar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定放弃此号码,重新摇一次?", "重置提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                btnStart.Enabled = true;
                btnRestar.Enabled = false;
            }
        }
    }
}

二、三等奖后台类似,就不贴出来了,下面是幸运奖窗体的后台代码:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class LukyForm : Form
    {
        private int[] lukyNum = new int[15];//15个号码
        public LukyForm()
        {
            InitializeComponent();
            //LukyNumInit();
            //JupNumberInit();
        }

        /// <summary>
        /// 初始化幸运数字数组
        /// </summary>
        private void LukyNumInit()
        {
            for (int i = 0; i < lukyNum.Length; i++)
            {
                lukyNum[i] = NumberManage.GetLukyNumber();                
            }

        }

        /// <summary>
        /// 初始化所有JupNumber要显示的数字
        /// </summary>
        private void JupNumberInit()
        {
            int i = 0;
            foreach (Control jup in this.Controls)
            {
                if (jup is JupNumber)
                {
                    ((JupNumber)jup).Num = lukyNum[i];
                    i++;
                }
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            LukyNumInit();           
            JupNumberInit();
            foreach (Control jup in this.Controls)
            {
                if (jup is JupNumber)
                {
                    ((JupNumber)jup).Start();                    
                }

            }
        }
    }
}

 

 

posted @ 2012-05-25 20:58  不哼不哈  阅读(1558)  评论(8编辑  收藏  举报