源码 .cs
----------------------------------------------------------------------------
using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
using System.Threading;
using System.Collections.Generic;
///
/// 2014.3 PZ
///
namespace 照片抽奖
{
public partial class Form1 : Form
{
DataSet config = new DataSet(); //用来存config.xml读取出来的参数
bool is_keypress = true; //是否按下暂停
List<FileInfo> imglist = new List<FileInfo>(); //存放所有图片位置信息
string winlist = string.Empty; //中奖名单
string savlist = string.Empty; //保存的中奖名单
SoundPlayer sp; //声音播放
DateTime dt; //时钟
Thread th; //抽奖线程
int random; //随机抽到的数字
string savfilename = "lucky.sav"; //结果保存文件
/// <summary>
/// 默认参数,config.xml不存在时使用
/// </summary>
int Bg_Width = 1024;
int Bg_Height = 768;
int Photo_Width = 640;
int Photo_Height = 426;
int Photo_Position_X = 179;
int Photo_Position_Y = 180;
string Bg_Path = "bg\\bg.jpg";
string Photo_Path = "photos";
string Sound_Run_Path = "sounds\\music.wav";
string Sound_Win_Path = "sounds\\result.wav";
bool Save_Result = true;
int Timespan_Keypress = 3;
public Form1()
{
InitializeComponent();
if (new FileInfo("config.xml").Exists) //读取config.xml参数,不存在使用上面的默认值
{
config.ReadXml("config.xml");
ReadConfig(config);
}
else
{
MessageBox.Show("配置文件不存在,使用默认设置执行!");
}
this.Width = Bg_Width; //背景宽
this.Height = Bg_Height; //背景高
this.BackgroundImage = Image.FromFile(Application.StartupPath + "\\" + Bg_Path); //背景图片
sp = new SoundPlayer(Sound_Run_Path);
sp.PlayLooping(); //音乐循环播放
if (Save_Result && new FileInfo(savfilename).Exists) //如果是保存中奖名单,并且名单中有信息,则读取出来确保这些信息不再中奖
{
StreamReader sr = new StreamReader(savfilename, Encoding.Default);
savlist = sr.ReadToEnd().Replace("\r\n", "");
sr.Close();
}
winlist = savlist; //保存过的中奖信息一起加入中奖名单
FindImg(Photo_Path + "\\", savlist); //找到目录下所有照片,不支持子目录
th = new Thread(new ThreadStart(DoLoop));
th.Start(); //执行抽奖线程
}
private void ReadConfig(DataSet ds) //此方法读xml参数
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
switch (ds.Tables[0].Rows[i]["Name"].ToString())
{
case "Bg_Width": Bg_Width = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Bg_Height": Bg_Height = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Photo_Width": Photo_Width = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Photo_Height": Photo_Height = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Photo_Position_X": Photo_Position_X = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Photo_Position_Y": Photo_Position_Y = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Bg_Path": Bg_Path = ds.Tables[0].Rows[i]["Content"].ToString(); break;
case "Photo_Path": Photo_Path = ds.Tables[0].Rows[i]["Content"].ToString(); break;
case "Sound_Run_Path": Sound_Run_Path = ds.Tables[0].Rows[i]["Content"].ToString(); break;
case "Sound_Win_Path": Sound_Win_Path = ds.Tables[0].Rows[i]["Content"].ToString(); break;
case "Save_Result": Save_Result = bool.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
case "Timespan_Keypress": Timespan_Keypress = int.Parse(ds.Tables[0].Rows[i]["Content"].ToString()); break;
}
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (int)Keys.Escape) // 按ESC提示退出?
{
sp.Stop();
is_keypress = false; //暂停
if (MessageBox.Show("退出软件?", "确认?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) //是,退出
{
th.Abort(); //终止抽奖线程
this.Close(); //关闭窗体
}
else //否,继续
{
is_keypress = true;
sp = new SoundPlayer(Sound_Run_Path);
sp.PlayLooping();
}
}
if (e.KeyChar == (int)Keys.Space) //按空格抽奖
{
if (dt == null) //为空的话初始化一个当前时间
{
dt = DateTime.Now;
}
else
{
TimeSpan ts = DateTime.Now - dt; //计算间隔时间
if (ts.Seconds >= Timespan_Keypress) //超过规定间隔才会生效,太短不行,避免连续按键
{
if (is_keypress == true) //如果按下空格时是运行的,那么暂停
{
is_keypress = false;
winlist += imglist[random].Name + ","; //把中奖的编号加入序列
imglist.RemoveAt(random); //把中奖的照片清除,确保不会重复中奖
sp.Stop();
sp = new SoundPlayer(Sound_Win_Path); //播放中奖音乐
sp.Play();
if (Save_Result)
{
StreamWriter sw = new StreamWriter(savfilename, false, Encoding.Default);
sw.WriteLine(winlist); //中奖结果保存到文件
sw.Flush();
sw.Close();
}
else
{
// 参数设置不保存,此处不做操作
}
}
else //如果按下空格时是暂停的,那么继续
{
is_keypress = true;
sp = new SoundPlayer(Sound_Run_Path);
sp.PlayLooping();
}
dt = DateTime.Now; //把中奖时间记下来
}
else
{
//Undo
}
}
}
}
private void DoLoop()
{
while (true) //永远循环
{
while (is_keypress) //按下暂停,永远在暂停和运行间切换
{
this.Invoke(new MethodInvoker(delegate()
{
if (imglist.Count > 0)
{
int guidseed = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0); //使用Guid作为种子,随机效果好
Random r = new Random(guidseed);
random = r.Next(0, imglist.Count); //抽取不大于照片数量的一个随机数
pb1.SizeMode = PictureBoxSizeMode.CenterImage;
Bitmap MyImage = new Bitmap(imglist[random].FullName);
pb1.ClientSize = new Size(Photo_Width, Photo_Height);
pb1.Location = new Point(Photo_Position_X, Photo_Position_Y);
pb1.Image = (Image)MyImage;
}
else
{
MessageBox.Show("全部中奖啦,不用抽了,结束!");
th.Abort(); //终止抽奖线程
this.Close();
}
}));
Thread.Sleep(1);
}
}
}
private List<FileInfo> FindImg(string dir, string win) //win是已经中奖的信息,在此要略过
{
DirectoryInfo di = new DirectoryInfo(dir);
FileInfo[] fi = di.GetFiles("*.jpg");
foreach (FileInfo i in fi)
{
if (win.Contains(i.Name)) //中奖名单里已经包含了
{
//略过
}
else
{
imglist.Add(i); //找到目录下所有照片,不支持子目录
}
}
return imglist;
}
}
}
配置文件 config.xml
------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Xml>
<Para>
<Name>Bg_Width</Name>
<Content>1024</Content>
</Para>
<Para>
<Name>Bg_Height</Name>
<Content>768</Content>
</Para>
<Para>
<Name>Photo_Width</Name>
<Content>640</Content>
</Para>
<Para>
<Name>Photo_Height</Name>
<Content>426</Content>
</Para>
<Para>
<Name>Photo_Position_X</Name>
<Content>179</Content>
</Para>
<Para>
<Name>Photo_Position_Y</Name>
<Content>180</Content>
</Para>
<Para>
<Name>Bg_Path</Name>
<Content>bg\bg.jpg</Content>
</Para>
<Para>
<Name>Photo_Path</Name>
<Content>photos</Content>
</Para>
<Para>
<Name>Sound_Run_Path</Name>
<Content>sounds\music.wav</Content>
</Para>
<Para>
<Name>Sound_Win_Path</Name>
<Content>sounds\result.wav</Content>
</Para>
<Para>
<Name>Save_Result</Name>
<Content>False</Content>
</Para>
<Para>
<Name>Timespan_Keypress</Name>
<Content>3</Content>
</Para>
</Xml>