年会抽奖程序代码讲解(c#版)
抽獎(jiǎng)程序源代碼下載地址:http://www.cnblogs.com/liuzhengdao/archive/2010/12/13/1904449.html
1抽獎(jiǎng)程序效果圖
2抽獎(jiǎng)程序功能設(shè)計(jì)
1、人員清單、獎(jiǎng)項(xiàng)、抽獎(jiǎng)記錄等數(shù)據(jù)均在存儲(chǔ)在Excel中,易于維護(hù);2、抽獎(jiǎng)結(jié)束時(shí)即可打印中獎(jiǎng)人員清單及紀(jì)念獎(jiǎng)清單,數(shù)據(jù)可排序、可篩選;3、在Excel中即可定義獎(jiǎng)項(xiàng)數(shù)量、一次抽取數(shù)量、獎(jiǎng)品圖片等;4、可臨時(shí)追加抽獎(jiǎng)人數(shù);5、每個(gè)獎(jiǎng)項(xiàng)抽取完成后可顯示當(dāng)前獎(jiǎng)項(xiàng)中獎(jiǎng)人員清單;6、可自定義背景圖片;7、限定每次按鍵間隔時(shí)間約5秒鐘,防止誤操作;8、限制抽獎(jiǎng)數(shù)量不超過(guò)設(shè)定數(shù)量,并在屏幕上同步顯示已抽取數(shù)量;9、獎(jiǎng)品圖片展示。
3 抽獎(jiǎng)原理
人員名單來(lái)自人事,按工號(hào)升序排序,首次讀入程序后,以隨機(jī)方式打亂順序,在此基礎(chǔ)上,隨機(jī)抽取。開(kāi)始抽獎(jiǎng)時(shí),程序每隔0.01秒隨機(jī)取得一個(gè)號(hào),屏幕上每0.1秒顯示一次當(dāng)前隨機(jī)取得的號(hào),當(dāng)停止抽獎(jiǎng)時(shí),程序隨機(jī)抽取一個(gè)號(hào)作為中獎(jiǎng)號(hào)碼,并從人員清單中刪除。
4運(yùn)行環(huán)境
開(kāi)發(fā)語(yǔ)言c#,開(kāi)發(fā)工具VS2008,運(yùn)行環(huán)境windows XP/VISTA、office2007、.netFrameWork3.5。
5部分代碼講解
5.1取得系統(tǒng)路徑
String path = System.Windows.Forms.Application.StartupPath + "\\";
5.2取得電腦屏幕的寬度和高度
int screenWidth = System.Windows.Forms.Screen.GetBounds(this).Width;
int screenHeight = System.Windows.Forms.Screen.GetBounds(this).Height;
5.3設(shè)置背景圖片,并設(shè)置圖片自動(dòng)伸縮
this.BackgroundImage = Image.FromFile(path + "bg.jpg");
this.BackgroundImageLayout = ImageLayout.Stretch;
5.4以RGB方式設(shè)置背景顏色
this.BackColor = Color.FromArgb(196, 14, 10);
5.5讀取setting.txt中顏色的RGB值,setting.txt內(nèi)容是color=196,14,10
String color = "";
try
{
using (StreamReader sr = new StreamReader(path + "setting.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("color"))
color = line;
}
}
}
catch (Exception ex1) { MessageBox.Show("未找到文件setting.txt"); }
5.6將字符串拆分成數(shù)組,例如color=196,14,10
color = color.Substring(6);
String[] arr = color.Split(',');
//arr[0],arr[1],arr[2]
5.7設(shè)置label背景透明,即采用Form背景,以免快速變換時(shí)出現(xiàn)白色區(qū)域
this.labelMsg.BackColor = Color.Transparent;
設(shè)置label大小隨文字大小變化
this.labelMsg.AutoSize = true;
this.labelMsg.Text = "";
設(shè)置label字體
this.labelMsg.Font = new System.Drawing.Font("LiSu", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
5.8設(shè)置窗體Form居中
this.Location = new System.Drawing.Point(Convert.ToInt32(screenWidth / 2) - Convert.ToInt32(this.Width / 2), Convert.ToInt32(screenHeight / 2) - Convert.ToInt32(this.Height / 2));
5.9操作Excel
首先引用
using Excel = Microsoft.Office.Interop.Excel;
定義變量
private Excel.Application excel;
private Excel.Workbook wb;//data.xls文件
private Excel.Worksheet renyuanWs, jiangxiangWs, rizhiWs, liwaiWs;
private String dataName = "";
private Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;//2003版本
打開(kāi)Excel進(jìn)程
excel = new Excel.Application();
不顯示Excel界面
excel.Visible = false;
不顯示提示或警告
excel.DisplayAlerts = false;
打開(kāi)data.xls
wb = excel.Workbooks.Add(path + "data.xls");
定義多個(gè)Excel區(qū)域,
Excel.Range range = null;
取得第一個(gè)Sheet
Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[1];
從第二行讀取
for (int i = 2; i < s.UsedRange.Rows.Count + 1; i++)
{
取得第一個(gè)單元格或區(qū)域
range = s.get_Range(s.Cells[i, 1], s.Cells[i, 1]);
range.Value2即是range中的內(nèi)容
if (range.Value2 !=null && range.Value2.ToString() != "")
MessageBox.Show(range.Value2.ToString());
else
break;
}
按順序設(shè)置工作表的單元格內(nèi)容
String now = DateTime.Now.ToLocalTime().ToString();
int r = rizhiWs.UsedRange.Rows.Count;
ws.Cells[r + 1, 1] = now;
保存Excel
wb.SaveAs(dataName, version, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
5.10取得隨機(jī)數(shù)
new Random(new Random().Next(100)).Next(100);
5.11動(dòng)態(tài)創(chuàng)建圖片按鈕
for (int i = 0; i < 10; i++)
{
System.Windows.Forms.PictureBox btn = new System.Windows.Forms.PictureBox();
每行顯示6個(gè)按鈕
if (i > 0 && i % 6 == 0)
level++;
btn.Location = new System.Drawing.Point(30 + (i - 6 * level) * 93, 30 + level * 38);
加上btn前綴便于識(shí)別
btn.Name = "btn" + i.ToString();
btn.Size = new System.Drawing.Size(90, 34);
設(shè)置圖片按鈕
btn.Image = Image.FromFile(path + "001.jpg");
btn.SizeMode = PictureBoxSizeMode.StretchImage;
btn.BackColor = Color.Transparent;
注冊(cè)單擊事件
btn.Click += new System.EventHandler(btn_Click);
btn.Cursor = Cursors.Hand;
this.Controls.Add(btn);
}
在btn_Click中取得傳遞過(guò)來(lái)的對(duì)象
private void btn_Click(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender;
}
5.12填充字符串到一定長(zhǎng)度,便于顯示整齊
String str = "abc"
str = new String(' ', 10 - str.Length);
5.13窗體全屏
private void mFullScreen()
{
try
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.Sizable;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
5.14注冊(cè)按鍵事件
比如按空格鍵開(kāi)始或停止抽獎(jiǎng),這里我設(shè)置通過(guò)PageUp或是PageDown按鍵,這樣就可通過(guò)翻頁(yè)筆來(lái)控制
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 32 || e.KeyValue == 33 || e.KeyValue == 34)
{
;//抽獎(jiǎng)代碼
}
}
5.15取得納秒
long l = DateTime.Now.Ticks;
5.16顯示或隱藏圖片按鈕
private void mShowCon(bool show)
{
try
{
foreach (Control con in this.Controls)
{
if (con.Name.StartsWith("btn") || con.Name.StartsWith("img"))
{
if (con.Visible == show)
break;
con.Visible = show;
}
}
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
5.17打開(kāi)第二個(gè)窗體
Form f = new Form2();
可以傳遞參數(shù)
Form f = new Form2("5");
f.Show();
在Form2中接收傳遞過(guò)來(lái)的值
public Form2(String str)
{
InitializeComponent();
Console.WrietLine(str);
}
總結(jié)
以上是生活随笔為你收集整理的年会抽奖程序代码讲解(c#版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电视上的k歌软件哪个好
- 下一篇: 笔记本和平板电脑怎么选平板和笔记本电脑选