首页 > 代码库 > 随机抽奖软件

随机抽奖软件

Form类

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Tools;
namespace Randomdraw
{
public partial class RandomForm : Form
{
ConfigEntity config;
bool isStart = true;
List<string> nameList;
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
Random random = new Random();
int nameIndex = 0;
public RandomForm()
{
InitializeComponent();

//配置
config = loadConfig();//读取配置文件
if (File.Exists(config.Backgroundfile))//设置背景图片
{
Image image = Image.FromFile(config.Backgroundfile);
this.BackgroundImage = image;
};
if (config.FullScreen)//是否全屏
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}//if
//设置文本字体
lblName.BackColor = Color.Transparent;
lblName.Font = config.Font;
lblName.ForeColor = config.FontColor;
loadList();//读取人员名单
timer.Interval = config.Span;//定时器每Tick一次的间隔时间
timer.Tick += new EventHandler(timer_Tick);//绑定Timer每Tick一下时调用的处理方法(签)名(Method)
}//Form1

/// <summary>
/// 按下键盘后的处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Escape:
case (char)Keys.Enter:
case (char)Keys.Back:
case (char)Keys.Delete:
case (char)Keys.End:
System.Environment.Exit(0);
break;
case (char)Keys.Space:
working();
break;
}//switch

}//Form1_KeyPress
/// <summary>
/// 鼠标点击后的处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RandomForm_MouseClick(object sender, MouseEventArgs e)
{
working();
}//RandomForm_MouseClick

/// <summary>
/// 读取配置
/// </summary>
ConfigEntity loadConfig()
{
ConfigEntity configEn = new ConfigEntity();

if (File.Exists("config.ini")==false) {
MessageBox.Show("配置文件config.ini不存在,使用默认配置.");
return configEn;
}//if
Ini ini = new Ini("config.ini");
string strAgent;
strAgent=ini.ReadValue("REMOVE_WINNER", "BASE");//是否移除获奖者
if (strAgent != null && strAgent.Trim().Equals("") == false) {
if (strAgent.Trim().ToUpper().Equals("TRUE") == false)
configEn.RemoveWinner = false;
}//if
strAgent = ini.ReadValue("FULLSCREEN", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
if (strAgent.Trim().ToUpper().Equals("TRUE") == false)
configEn.FullScreen = false;
}//if
strAgent = ini.ReadValue("BACKGROUND_FILE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
configEn.Backgroundfile = strAgent.Trim();
}//if

int span = 1;
strAgent = ini.ReadValue("SPAN", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
bool isOk = int.TryParse(strAgent.Trim(), out span);
if (isOk == false)
span = 1;
}//if
configEn.Span = span;

float fontSize=100;
strAgent = ini.ReadValue("FONT_SIZE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
bool isOk = float.TryParse(strAgent.Trim(), out fontSize);
if (isOk == false)
fontSize = 100;
}//if

strAgent = ini.ReadValue("FONT_COLOR", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
configEn.FontColor= Color.FromName(strAgent.Trim());
}//if
string fontFamily = "宋体";
strAgent = ini.ReadValue("FONT_FAMILY", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
fontFamily = strAgent.Trim();
}//if

FontStyle fontStyle= FontStyle.Regular;
strAgent = ini.ReadValue("FONT_STYLE", "BASE");
if (strAgent != null && strAgent.Trim().Equals("") == false)
{
try
{
fontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), strAgent);
}
catch (Exception e) {
Console.WriteLine(e.ToString());
fontStyle = FontStyle.Regular;
}//catch
}//if
configEn.Font = new Font(fontFamily, fontSize, fontStyle);//字体

return configEn;
}//loadConfig

/// <summary>
/// 定时器每Tick一下的处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void timer_Tick(object sender, EventArgs e)
{
if (nameList != null && nameList.Count > 0)
{
int next = random.Next(0, nameList.Count);
lblName.Text = nameList[next];
nameIndex = next;
}//if
}//timer_Tick
/// <summary>
/// 运行(暂停或开始)
/// </summary>
void working()
{
if (isStart)
start();
else
stop();
}//working
/// <summary>
/// 开始
/// </summary>
void start()
{
isStart = false;
timer.Start();
}//start

/// <summary>
/// 停止
/// </summary>
void stop()
{
isStart = true;
timer_Tick(null, null);//再随机一次后暂停
timer.Stop();
if(config.RemoveWinner)
RemoveWinner();
}//stop

/// <summary>
/// 移除获奖者
/// </summary>
void RemoveWinner()
{
if (nameList != null && nameList.Count > 0)
{
try
{
nameList.RemoveAt(nameIndex);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
MessageBox.Show("移除名单数组中Index为"+nameIndex+"的数据失败.");
}//catch
}//if
}//RemoveWinner

/// <summary>
/// 读取人员名单
/// </summary>
void loadList()
{
if (File.Exists("list.txt") == false)
{
return;
}//if
try
{
nameList = null;
string[] names;
names = System.IO.File.ReadAllLines("list.txt");
lblName.Text = "Ready";
nameList = new List<string>();
foreach (String name in names)
{
nameList.Add(name.Replace(" ",""));
}//foreach
}
catch (Exception e)
{
lblName.Text = "读取名单失败:" + e.Message;
}//
}//loadList


}//Form1
}

 

ConfigEntity类

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Randomdraw
{
class ConfigEntity
{
private string backgroundfile= "backgroud.jpg";
private bool removeWinner=true;
private bool fullScreen=true;
private System.Drawing.Font font = new System.Drawing.Font("宋体", 100);
private string readyWord="Ready";
private int span=1;
private Color fontColor = Color.Black;

public string Backgroundfile
{
get
{
return backgroundfile;
}

set
{
backgroundfile = value;
}
}

public bool RemoveWinner
{
get
{
return removeWinner;
}

set
{
removeWinner = value;
}
}

public bool FullScreen
{
get
{
return fullScreen;
}

set
{
fullScreen = value;
}
}

public Font Font
{
get
{
return font;
}

set
{
font = value;
}
}

public string ReadyWord
{
get
{
return readyWord;
}

set
{
readyWord = value;
}
}

public int Span
{
get
{
return span;
}

set
{
span = value;
}
}

public Color FontColor
{
get
{
return fontColor;
}

set
{
fontColor = value;
}
}
}//ConfigEntity
}//Randomdraw

 

ini文件操作类:(引用参考:http://www.cnblogs.com/uu102/p/4083501.html)

ini.cs

随机抽奖软件