最近班上寫了一個關於winform的實訓小項目(抽獎系統),90%的代碼都在下面,感覺還不錯,所以貼出來分享一下,希望能幫倒大家
所有界面如下:
這是自己寫的一個公共類:

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; } } }
大圖片的后台代碼(用戶控件):

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; } } } }
小圖片的后台代碼(用戶控件):

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; } } } }
幸運獎的后台代碼(用戶控件):

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(); } } } }
一等獎窗體的后台代碼:

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; } } } }
二、三等獎后台類似,就不貼出來了,下面是幸運獎窗體的后台代碼:

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(); } } } } }